Skip to content

Commit dadd9fa

Browse files
committed
Add optional SCD41 CO2 sensor to Enviro Indoor
1 parent 832162c commit dadd9fa

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

enviro/boards/indoor.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import math
2+
import time
23
from breakout_bme68x import BreakoutBME68X
34
from breakout_bh1745 import BreakoutBH1745
45

5-
from enviro import i2c
6+
from enviro import i2c, i2c_devices
67

8+
# Detect whether an SCD41 CO2+ sensor is connected
9+
have_scd41 = 98 in i2c_devices
10+
if have_scd41:
11+
import breakout_scd41
12+
13+
breakout_scd41.init(i2c)
14+
breakout_scd41.start()
15+
716
bme688 = BreakoutBME68X(i2c, address=0x77)
817

918
bh1745 = BreakoutBH1745(i2c)
@@ -54,14 +63,31 @@ def get_sensor_readings(seconds_since_last):
5463

5564
bh1745.measurement_time_ms(160)
5665
r, g, b, c = bh1745.rgbc_raw()
66+
67+
co2 = 0
68+
if have_scd41:
69+
retries = 25
70+
while retries > 0 and not breakout_scd41.ready():
71+
time.sleep(0.2)
72+
retries -= 1
73+
74+
if retries != 0:
75+
co2, temp_scd, hum_scd = breakout_scd41.measure()
76+
77+
# Prefer the SCD41 temperature as it is further from the potentially warm Pico
78+
temperature = round(temp_scd, 2)
5779

5880
from ucollections import OrderedDict
59-
return OrderedDict({
81+
d = OrderedDict({
6082
"temperature": temperature,
6183
"humidity": humidity,
6284
"pressure": pressure,
6385
"gas_resistance": gas_resistance,
6486
"aqi": aqi,
6587
"luminance": lux_from_rgbc(r, g, b, c),
6688
"color_temperature": colour_temperature_from_rgbc(r, g, b, c)
67-
})
89+
})
90+
if co2 > 0:
91+
d["CO2"] = co2
92+
93+
return d

0 commit comments

Comments
 (0)