Skip to content

Commit f0c035f

Browse files
committed
clean up example output
slow down i2c to 1khz (works on ft232h now) add inter-command delays (5ms)
1 parent 259e369 commit f0c035f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

adafruit_scd30.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"""
2929

3030
# imports
31-
from time import sleep
31+
import time
3232
from struct import unpack_from, unpack
3333
import adafruit_bus_device.i2c_device as i2c_device
3434
from micropython import const
@@ -107,7 +107,7 @@ def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR):
107107
def reset(self):
108108
"""Perform a soft reset on the sensor, restoring default values"""
109109
self._send_command(_CMD_SOFT_RESET)
110-
sleep(0.1) # not mentioned by datasheet, but required to avoid IO error
110+
time.sleep(0.1) # not mentioned by datasheet, but required to avoid IO error
111111

112112
@property
113113
def measurement_interval(self):
@@ -147,7 +147,7 @@ def self_calibration_enabled(self):
147147
def self_calibration_enabled(self, enabled):
148148
self._send_command(_CMD_AUTOMATIC_SELF_CALIBRATION, enabled)
149149
if enabled:
150-
sleep(0.01)
150+
time.sleep(0.01)
151151

152152
@property
153153
def data_available(self):
@@ -278,13 +278,15 @@ def _send_command(self, command, arguments=None):
278278

279279
with self.i2c_device as i2c:
280280
i2c.write(self._buffer, end=end_byte)
281+
time.sleep(0.05) # 3ms min delay
281282

282283
def _read_register(self, reg_addr):
283284
self._buffer[0] = reg_addr >> 8
284285
self._buffer[1] = reg_addr & 0xFF
285286
with self.i2c_device as i2c:
286287
i2c.write(self._buffer, end=2)
287288
# separate readinto because the SCD30 wants an i2c stop before the read (non-repeated start)
289+
time.sleep(0.005) # min 3 ms delay
288290
with self.i2c_device as i2c:
289291
i2c.readinto(self._buffer, end=2)
290292
return unpack_from(">H", self._buffer)[0]

examples/scd30_simpletest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
import time
55
import board
66
import adafruit_scd30
7+
import busio
78

8-
i2c = board.I2C() # uses board.SCL and board.SDA
9+
# SCD-30 has tempremental I2C with clock stretching, and delays
10+
# It's best to start using I2C clock 1000 Hz and then you can increase it
11+
# until the sensor stops responding (NAK fails, etc)
12+
i2c = busio.I2C(board.SCL, board.SDA, frequency=1000)
913
scd = adafruit_scd30.SCD30(i2c)
1014

1115
while True:
1216
# since the measurement interval is long (2+ seconds) we check for new data before reading
1317
# the values, to ensure current readings.
1418
if scd.data_available:
1519
print("Data Available!")
16-
print("CO2:", scd.CO2, "PPM")
17-
print("Temperature:", scd.temperature, "degrees C")
18-
print("Humidity:", scd.relative_humidity, "%%rH")
20+
print("CO2: %d PPM" % scd.CO2)
21+
print("Temperature: %0.2f degrees C" % scd.temperature)
22+
print("Humidity: %0.2f %% rH" % scd.relative_humidity)
1923
print("")
2024
print("Waiting for new data...")
2125
print("")

0 commit comments

Comments
 (0)