Merge pull request #792 from pimoroni/patch/co2-examples

SCD41: update examples
pull/796/head
Hel Gibbons 2023-06-23 16:13:54 +01:00 zatwierdzone przez GitHub
commit 40f0554259
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 76 dodań i 8 usunięć

Wyświetl plik

@ -1,12 +1,9 @@
import breakout_scd41
from pimoroni_i2c import PimoroniI2C
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
import time
import pimoroni_i2c
import breakout_scd41
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
i2c = pimoroni_i2c.PimoroniI2C(**PINS_PICO_EXPLORER)
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS) # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
breakout_scd41.init(i2c)
breakout_scd41.start()

Wyświetl plik

@ -0,0 +1,71 @@
import plasma
from plasma import plasma2040
import breakout_scd41 as scd
from pimoroni_i2c import PimoroniI2C
from pimoroni import RGBLED
import time
"""
Reads CO2 level from an SCD41 breakout...
... and changes the LED strip an appropriate colour.
https://shop.pimoroni.com/products/scd41-co2-sensor-breakout
"""
# set how many LEDs you have
NUM_LEDS = 50
BRIGHTNESS = 1.0
# the range of readings to map to colours
# https://www.kane.co.uk/knowledge-centre/what-are-safe-levels-of-co-and-co2-in-rooms
MIN = 400
MAX = 2000
# pick what bits of the colour wheel to use (from 0-360°)
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
HUE_START = 100 # green
HUE_END = 0 # red
# Pick *one* LED type by uncommenting the relevant line below:
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
# APA102 / DotStar™ LEDs
# led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK)
# set up I2C
i2c = PimoroniI2C(plasma2040.SDA, plasma2040.SCL)
# set up onboard LED
led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
led.set_rgb(0, 0, 0)
# Start updating the LED strip
led_strip.start()
# set up SCD41 breakout
scd.init(i2c)
scd.start()
print("Waiting for SCD41 to be ready")
while True:
if scd.ready():
co2, temperature, humidity = scd.measure()
print(f"""
CO2: {co2} ppm
Temperature: {temperature:.2f} °C
Humidity: {humidity:.2f} %""")
# calculates a colour
hue = max(0, HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN)))
# set the leds
for i in range(NUM_LEDS):
led_strip.set_hsv(i, hue / 360, 1.0, BRIGHTNESS)
# flash the onboard LED so you can tell when the script is running
led.set_rgb(0, 255, 0)
time.sleep(0.05)
led.set_rgb(0, 0, 0)

Wyświetl plik

@ -48,7 +48,7 @@ Temperature: {temperature:.2f} °C
Humidity: {humidity:.2f} %""")
# calculates a colour
hue = HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN))
hue = max(0, HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN)))
# set the leds
for i in range(NUM_LEDS):