Post

CircuitPython Sensors via Lambda

Read I2C and GPIO sensors directly on the Raspberry Pi using CircuitPython in Krill Lambda scripts

CircuitPython Sensors via Lambda

Reading Sensors Directly on the Pi with Lambda Scripts

The Lambda executor runs Python scripts on your Krill server. Combined with Adafruit Blinka (CircuitPython for Linux), you can read I2C, SPI, and GPIO sensors wired directly to your Raspberry Pi’s header pins — no external microcontroller needed.

This is useful for sensors that connect directly to the Pi via I2C (color sensors, temperature/humidity, accelerometers, etc.) rather than over serial USB.

Setup: Install CircuitPython Libraries System-Wide

Krill runs as the krill system user, which does not have access to per-user pip installations. Libraries must be installed system-wide so the krill user can import them.

1
2
3
4
5
6
7
# Install the base Blinka library (provides 'board' and 'busio' modules)
sudo pip3 install --break-system-packages adafruit-blinka

# Install the library for your specific sensor, e.g.:
sudo pip3 install --break-system-packages adafruit-circuitpython-tcs34725   # Color sensor
sudo pip3 install --break-system-packages adafruit-circuitpython-sht31d     # Temp/humidity
sudo pip3 install --break-system-packages adafruit-circuitpython-bmp280     # Barometric pressure

The --break-system-packages flag is required on newer Debian/Raspberry Pi OS versions that enforce PEP 668 externally-managed environments.

Verify the krill user can import the library:

1
cd /tmp && sudo -u krill python3 -c "import board; print('OK')"

If this prints OK, you’re ready. If you get a PermissionError about .lgd-nfy, make sure you’re running from a writable directory like /tmp (Krill handles this automatically).

Enable I2C on the Pi

If your sensor uses I2C (most Adafruit breakout boards do):

1
2
sudo raspi-config nonint do_i2c 0   # Enable I2C
sudo reboot

After reboot, verify your sensor is detected:

1
sudo i2cdetect -y 1

You should see a hex address (e.g., 0x29 for TCS34725 color sensor).

Write the Lambda Script

Create your script in /opt/krill/lambdas/. Here’s an example for a TCS34725 color sensor:

1
2
3
4
5
6
7
8
9
10
11
# /opt/krill/lambdas/color.py
import board
import adafruit_tcs34725

i2c = board.I2C()
sensor = adafruit_tcs34725.TCS34725(i2c)

sensor.gain = 4
sensor.integration_time = 100

print(sensor.color)

The script’s stdout output becomes the value written to the target DataPoint. Keep scripts simple — print one value.

Set permissions:

1
2
sudo chown krill:krill /opt/krill/lambdas/color.py
sudo chmod 755 /opt/krill/lambdas/color.py

Wire It Up in Krill

  1. Create a DataPoint (type: Double) to store the sensor reading
  2. Create a CronTimer trigger to read on a schedule (e.g., every 30 seconds: */30 * * * * *)
  3. Add a Lambda executor as a child of the CronTimer
  4. In the Lambda editor, set the filename to color.py
  5. Set the target to your DataPoint

The cron fires, the lambda runs, the color value lands in your DataPoint, and it flows through your Krill swarm like any other data.

Troubleshooting

ModuleNotFoundError: No module named 'board'

  • The library is installed for your user but not system-wide. Run: sudo pip3 install --break-system-packages adafruit-blinka

PermissionError: [Errno 13] Permission denied: '.lgd-nfy...'

  • The lgpio library (used by Blinka on Pi 5) creates temp files in the working directory. Update to the latest Krill version which uses /tmp as the working directory for lambda scripts.

RuntimeError: Group xy not available

  • Ensure the krill user is in the required hardware groups: sudo usermod -aG i2c,spi,gpio krill then restart the service: sudo systemctl restart krill

OSError: [Errno 121] Remote I/O error

  • The I2C device is not responding. Check wiring, verify with sudo i2cdetect -y 1, and ensure I2C is enabled via raspi-config.

Lambda enters ERROR state after failure

  • Reset the lambda node state via the Krill app (long-press the node, select Update, save). The next cron trigger will re-execute it.

Supported Sensors

Any Adafruit CircuitPython library that works on Raspberry Pi Linux via Blinka will work in a Krill Lambda. Popular options:

SensorLibrarypip package
TCS34725 (color)adafruit_tcs34725adafruit-circuitpython-tcs34725
SHT31-D (temp/humidity)adafruit_sht31dadafruit-circuitpython-sht31d
BMP280 (barometric)adafruit_bmp280adafruit-circuitpython-bmp280
BME680 (env sensor)adafruit_bme680adafruit-circuitpython-bme680
TSL2591 (light)adafruit_tsl2591adafruit-circuitpython-tsl2591
ADS1115 (ADC)adafruit_ads1x15adafruit-circuitpython-ads1x15

Browse the full catalog at circuitpython.org/libraries.


Last verified: 2026-04-05

This post is licensed under CC BY 4.0 by Sautner Studio, LLC.