Skip to content

Commit 40f0554

Browse files
authored
Merge pull request #792 from pimoroni/patch/co2-examples
SCD41: update examples
2 parents 8a6bb65 + d9064f0 commit 40f0554

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

micropython/examples/breakout_scd41/scd41_demo.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import time
2-
3-
import pimoroni_i2c
41
import breakout_scd41
2+
from pimoroni_i2c import PimoroniI2C
3+
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
4+
import time
55

6-
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
7-
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
8-
9-
i2c = pimoroni_i2c.PimoroniI2C(**PINS_PICO_EXPLORER)
6+
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS) # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
107

118
breakout_scd41.init(i2c)
129
breakout_scd41.start()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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)

micropython/examples/plasma_stick/co2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
Humidity: {humidity:.2f} %""")
4949

5050
# calculates a colour
51-
hue = HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN))
51+
hue = max(0, HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN)))
5252

5353
# set the leds
5454
for i in range(NUM_LEDS):

0 commit comments

Comments
 (0)