Skip to content

Commit 443285e

Browse files
authored
Merge pull request #2810 from adafruit/sen5x
sen5x adapter demos
2 parents 80ed5a3 + f401d4c commit 443285e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

SEN5x_Adapter_Demos/Python/code.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
from sensirion_i2c_driver import I2cConnection, LinuxI2cTransceiver
7+
from sensirion_i2c_sen5x import Sen5xI2cDevice
8+
9+
i2c = LinuxI2cTransceiver('/dev/i2c-1')
10+
device = Sen5xI2cDevice(I2cConnection(i2c))
11+
12+
# Print some device information
13+
print(f"Version: {device.get_version()}")
14+
print(f"Product Name: {device.get_product_name()}")
15+
print(f"Serial Number: {device.get_serial_number()}")
16+
17+
# Perform a device reset (reboot firmware)
18+
device.device_reset()
19+
# Start measurement
20+
device.start_measurement()
21+
time.sleep(1)
22+
23+
def read_data():
24+
try:
25+
# Wait until next result is available
26+
print("Waiting for new data...")
27+
while device.read_data_ready() is False:
28+
time.sleep(0.1)
29+
# Read measured values -> clears the "data ready" flag
30+
values = device.read_measured_values()
31+
print(values)
32+
# Access a specific value separately (see Sen5xMeasuredValues)
33+
# mass_concentration = values.mass_concentration_2p5.physical
34+
# ambient_temperature = values.ambient_temperature.degrees_celsius
35+
# Read device status
36+
status = device.read_device_status()
37+
print("Device Status: {}\n".format(status))
38+
except Exception as e: # pylint: disable = broad-except
39+
print(f"Error: {e}")
40+
41+
while True:
42+
read_data()
43+
time.sleep(5)

0 commit comments

Comments
 (0)