|
| 1 | +import plasma |
| 2 | +from plasma import plasma2040 |
| 3 | +import breakout_scd41 as scd |
| 4 | +from pimoroni_i2c import PimoroniI2C |
| 5 | +from pimoroni import RGBLED |
| 6 | +import time |
| 7 | + |
| 8 | +""" |
| 9 | +Reads CO2 level from an SCD41 breakout... |
| 10 | +... and changes the LED strip an appropriate colour. |
| 11 | +https://shop.pimoroni.com/products/scd41-co2-sensor-breakout |
| 12 | +""" |
| 13 | + |
| 14 | +# set how many LEDs you have |
| 15 | +NUM_LEDS = 50 |
| 16 | + |
| 17 | +BRIGHTNESS = 1.0 |
| 18 | + |
| 19 | +# the range of readings to map to colours |
| 20 | +# https://www.kane.co.uk/knowledge-centre/what-are-safe-levels-of-co-and-co2-in-rooms |
| 21 | +MIN = 400 |
| 22 | +MAX = 2000 |
| 23 | + |
| 24 | +# pick what bits of the colour wheel to use (from 0-360°) |
| 25 | +# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/ |
| 26 | +HUE_START = 100 # green |
| 27 | +HUE_END = 0 # red |
| 28 | + |
| 29 | +# Pick *one* LED type by uncommenting the relevant line below: |
| 30 | +# WS2812 / NeoPixel™ LEDs |
| 31 | +led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT) |
| 32 | + |
| 33 | +# APA102 / DotStar™ LEDs |
| 34 | +# led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK) |
| 35 | + |
| 36 | +# set up I2C |
| 37 | +i2c = PimoroniI2C(plasma2040.SDA, plasma2040.SCL) |
| 38 | + |
| 39 | +# set up onboard LED |
| 40 | +led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B) |
| 41 | + |
| 42 | +led.set_rgb(0, 0, 0) |
| 43 | + |
| 44 | +# Start updating the LED strip |
| 45 | +led_strip.start() |
| 46 | + |
| 47 | +# set up SCD41 breakout |
| 48 | +scd.init(i2c) |
| 49 | +scd.start() |
| 50 | + |
| 51 | +print("Waiting for SCD41 to be ready") |
| 52 | + |
| 53 | +while True: |
| 54 | + if scd.ready(): |
| 55 | + co2, temperature, humidity = scd.measure() |
| 56 | + print(f""" |
| 57 | +CO2: {co2} ppm |
| 58 | +Temperature: {temperature:.2f} °C |
| 59 | +Humidity: {humidity:.2f} %""") |
| 60 | + |
| 61 | + # calculates a colour |
| 62 | + hue = max(0, HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN))) |
| 63 | + |
| 64 | + # set the leds |
| 65 | + for i in range(NUM_LEDS): |
| 66 | + led_strip.set_hsv(i, hue / 360, 1.0, BRIGHTNESS) |
| 67 | + |
| 68 | + # flash the onboard LED so you can tell when the script is running |
| 69 | + led.set_rgb(0, 255, 0) |
| 70 | + time.sleep(0.05) |
| 71 | + led.set_rgb(0, 0, 0) |
0 commit comments