Skip to content

Commit

Permalink
Merge pull request #7 from KeithTheEE/add_one_shot_co2
Browse files Browse the repository at this point in the history
Add one shot co2 measurement
  • Loading branch information
FoamyGuy authored Jul 30, 2023
2 parents ad1dbd3 + ed8180c commit 22aba82
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions adafruit_scd4x.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
_SCD4X_PERSISTSETTINGS = const(0x3615)
_SCD4X_GETASCE = const(0x2313)
_SCD4X_SETASCE = const(0x2416)
_SCD4X_MEASURESINGLESHOT = const(0x219D)
_SCD4X_MEASURESINGLESHOTRHTONLY = const(0x2196)


class SCD4X:
Expand Down Expand Up @@ -147,6 +149,16 @@ def relative_humidity(self) -> float:
self._read_data()
return self._relative_humidity

def measure_single_shot(self) -> None:
"""On-demand measurement of CO2 concentration, relative humidity, and
temperature for SCD41 only"""
self._send_command(_SCD4X_MEASURESINGLESHOT, cmd_delay=5)

def measure_single_shot_rht_only(self) -> None:
"""On-demand measurement of relative humidity and temperature for
SCD41 only"""
self._send_command(_SCD4X_MEASURESINGLESHOTRHTONLY, cmd_delay=0.05)

def reinit(self) -> None:
"""Reinitializes the sensor by reloading user settings from EEPROM."""
self.stop_periodic_measurement()
Expand Down
43 changes: 43 additions & 0 deletions examples/scd41_single_shot_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: 2021 by Keith Murray, written for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
import time
import board
import adafruit_scd4x

i2c = board.I2C()
scd4x = adafruit_scd4x.SCD4X(i2c)
print("Serial number:", [hex(i) for i in scd4x.serial_number])

print("Waiting for single shot CO2 measurement from SCD41....")
scd4x.measure_single_shot()

sample_counter = 0
while sample_counter < 3:
if scd4x.data_ready:
print("CO2: %d ppm" % scd4x.CO2)
print("Temperature: %0.1f *C" % scd4x.temperature)
print("Humidity: %0.1f %%" % scd4x.relative_humidity)
print()
scd4x.measure_single_shot()
sample_counter += 1
else:
print("Waiting...")
time.sleep(1)


print("Waiting for single shot Humidity and Temperature measurement from SCD41....")
scd4x.measure_single_shot_rht_only()

sample_counter = 0
while sample_counter < 3:
if scd4x.data_ready:
print("CO2: %d ppm" % scd4x.CO2) # Should be 0 ppm
print("Temperature: %0.1f *C" % scd4x.temperature)
print("Humidity: %0.1f %%" % scd4x.relative_humidity)
print()
scd4x.measure_single_shot_rht_only()
sample_counter += 1
else:
print("Waiting...")
time.sleep(1)

0 comments on commit 22aba82

Please sign in to comment.