Skip to content

Commit 146c9cb

Browse files
committed
Adding code for CO2 logger
Adding code for the Disconnected CO2 Data Logger Learn Guide. Uses the AdaLogger FeatherWing for RTC and SD card, SCD40 for CO2
1 parent daa0947 commit 146c9cb

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

  • Disconnected_CO2_Data_Logger
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import time
2+
import board
3+
import digitalio
4+
import adafruit_scd4x
5+
import adafruit_sdcard
6+
import busio
7+
import storage
8+
import adafruit_pcf8523
9+
10+
# setup for I2C
11+
i2c = board.I2C()
12+
# setup for SCD40
13+
scd4x = adafruit_scd4x.SCD4X(i2c)
14+
# setup for RTC
15+
rtc = adafruit_pcf8523.PCF8523(i2c)
16+
# start measuring co2 with SCD40
17+
scd4x.start_periodic_measurement()
18+
# list of days to print to the text file on boot
19+
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
20+
21+
# SPI SD_CS pin
22+
SD_CS = board.D10
23+
24+
# SPI setup for SD card
25+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
26+
cs = digitalio.DigitalInOut(SD_CS)
27+
sdcard = adafruit_sdcard.SDCard(spi, cs)
28+
vfs = storage.VfsFat(sdcard)
29+
storage.mount(vfs, "/sd")
30+
31+
# to update the RTC, change set_clock to True
32+
# otherwise RTC will remain set
33+
# it should only be needed after the initial set
34+
# if you've removed the coincell battery
35+
set_clock = False
36+
37+
if set_clock:
38+
# year, mon, date, hour, min, sec, wday, yday, isdst
39+
t = time.struct_time((2021, 10, 31, 00, 00, 00, 0, -1, -1))
40+
41+
print("Setting time to:", t)
42+
rtc.datetime = t
43+
print()
44+
45+
# variable to hold RTC datetime
46+
t = rtc.datetime
47+
48+
time.sleep(1)
49+
50+
# initial write to the SD card on startup
51+
with open("/sd/co2.txt", "a") as f:
52+
# writes the date
53+
f.write('The date is {} {}/{}/{}\n'.format(days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
54+
# writes the start time
55+
f.write('Start time: {}:{}:{}\n'.format(t.tm_hour, t.tm_min, t.tm_sec))
56+
# headers for data, comma-delimited
57+
f.write('CO2,Time\n')
58+
# debug statement for REPL
59+
print("initial write to SD card complete, starting to log")
60+
61+
while True:
62+
# variable for RTC datetime
63+
t = rtc.datetime
64+
# append SD card text file
65+
with open("/sd/co2.txt", "a") as f:
66+
# read co2 data from SCD40
67+
co2 = scd4x.CO2
68+
# write co2 data followed by the time, comma-delimited
69+
f.write('{},{}:{}:{}\n'.format(co2, t.tm_hour, t.tm_min, t.tm_sec))
70+
# repeat every 30 seconds
71+
time.sleep(30)

0 commit comments

Comments
 (0)