Skip to content

Commit 0cd69ab

Browse files
committed
sen5x adapter demos
Adding CPython and Arduino examples for the SEN5x sensor adapter
1 parent 80ed5a3 commit 0cd69ab

File tree

2 files changed

+297
-0
lines changed

2 files changed

+297
-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 = bare-except
39+
print(f"Error: {e}")
40+
41+
while True:
42+
read_data()
43+
time.sleep(5)
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2021, Sensirion AG
2+
//
3+
// SPDX-License-Identifier: BSD 3-Clause License
4+
5+
/*
6+
* I2C-Generator: 0.3.0
7+
* Yaml Version: 2.1.3
8+
* Template Version: 0.7.0-112-g190ecaa
9+
*/
10+
/*
11+
* Copyright (c) 2021, Sensirion AG
12+
* All rights reserved.
13+
*
14+
* Redistribution and use in source and binary forms, with or without
15+
* modification, are permitted provided that the following conditions are met:
16+
*
17+
* * Redistributions of source code must retain the above copyright notice, this
18+
* list of conditions and the following disclaimer.
19+
*
20+
* * Redistributions in binary form must reproduce the above copyright notice,
21+
* this list of conditions and the following disclaimer in the documentation
22+
* and/or other materials provided with the distribution.
23+
*
24+
* * Neither the name of Sensirion AG nor the names of its
25+
* contributors may be used to endorse or promote products derived from
26+
* this software without specific prior written permission.
27+
*
28+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38+
* POSSIBILITY OF SUCH DAMAGE.
39+
*/
40+
41+
#include <Arduino.h>
42+
#include <SensirionI2CSen5x.h>
43+
#include <Wire.h>
44+
45+
// The used commands use up to 48 bytes. On some Arduino's the default buffer
46+
// space is not large enough
47+
#define MAXBUF_REQUIREMENT 48
48+
49+
#if (defined(I2C_BUFFER_LENGTH) && \
50+
(I2C_BUFFER_LENGTH >= MAXBUF_REQUIREMENT)) || \
51+
(defined(BUFFER_LENGTH) && BUFFER_LENGTH >= MAXBUF_REQUIREMENT)
52+
#define USE_PRODUCT_INFO
53+
#endif
54+
55+
SensirionI2CSen5x sen5x;
56+
57+
void printModuleVersions() {
58+
uint16_t error;
59+
char errorMessage[256];
60+
61+
unsigned char productName[32];
62+
uint8_t productNameSize = 32;
63+
64+
error = sen5x.getProductName(productName, productNameSize);
65+
66+
if (error) {
67+
Serial.print("Error trying to execute getProductName(): ");
68+
errorToString(error, errorMessage, 256);
69+
Serial.println(errorMessage);
70+
} else {
71+
Serial.print("ProductName:");
72+
Serial.println((char*)productName);
73+
}
74+
75+
uint8_t firmwareMajor;
76+
uint8_t firmwareMinor;
77+
bool firmwareDebug;
78+
uint8_t hardwareMajor;
79+
uint8_t hardwareMinor;
80+
uint8_t protocolMajor;
81+
uint8_t protocolMinor;
82+
83+
error = sen5x.getVersion(firmwareMajor, firmwareMinor, firmwareDebug,
84+
hardwareMajor, hardwareMinor, protocolMajor,
85+
protocolMinor);
86+
if (error) {
87+
Serial.print("Error trying to execute getVersion(): ");
88+
errorToString(error, errorMessage, 256);
89+
Serial.println(errorMessage);
90+
} else {
91+
Serial.print("Firmware: ");
92+
Serial.print(firmwareMajor);
93+
Serial.print(".");
94+
Serial.print(firmwareMinor);
95+
Serial.print(", ");
96+
97+
Serial.print("Hardware: ");
98+
Serial.print(hardwareMajor);
99+
Serial.print(".");
100+
Serial.println(hardwareMinor);
101+
}
102+
}
103+
104+
void printSerialNumber() {
105+
uint16_t error;
106+
char errorMessage[256];
107+
unsigned char serialNumber[32];
108+
uint8_t serialNumberSize = 32;
109+
110+
error = sen5x.getSerialNumber(serialNumber, serialNumberSize);
111+
if (error) {
112+
Serial.print("Error trying to execute getSerialNumber(): ");
113+
errorToString(error, errorMessage, 256);
114+
Serial.println(errorMessage);
115+
} else {
116+
Serial.print("SerialNumber:");
117+
Serial.println((char*)serialNumber);
118+
}
119+
}
120+
121+
void setup() {
122+
123+
Serial.begin(115200);
124+
while (!Serial) {
125+
delay(100);
126+
}
127+
128+
Wire.begin();
129+
130+
sen5x.begin(Wire);
131+
132+
uint16_t error;
133+
char errorMessage[256];
134+
error = sen5x.deviceReset();
135+
if (error) {
136+
Serial.print("Error trying to execute deviceReset(): ");
137+
errorToString(error, errorMessage, 256);
138+
Serial.println(errorMessage);
139+
}
140+
141+
// Print SEN55 module information if i2c buffers are large enough
142+
#ifdef USE_PRODUCT_INFO
143+
printSerialNumber();
144+
printModuleVersions();
145+
#endif
146+
147+
// set a temperature offset in degrees celsius
148+
// Note: supported by SEN54 and SEN55 sensors
149+
// By default, the temperature and humidity outputs from the sensor
150+
// are compensated for the modules self-heating. If the module is
151+
// designed into a device, the temperature compensation might need
152+
// to be adapted to incorporate the change in thermal coupling and
153+
// self-heating of other device components.
154+
//
155+
// A guide to achieve optimal performance, including references
156+
// to mechanical design-in examples can be found in the app note
157+
// “SEN5x – Temperature Compensation Instruction” at www.sensirion.com.
158+
// Please refer to those application notes for further information
159+
// on the advanced compensation settings used
160+
// in `setTemperatureOffsetParameters`, `setWarmStartParameter` and
161+
// `setRhtAccelerationMode`.
162+
//
163+
// Adjust tempOffset to account for additional temperature offsets
164+
// exceeding the SEN module's self heating.
165+
float tempOffset = 0.0;
166+
error = sen5x.setTemperatureOffsetSimple(tempOffset);
167+
if (error) {
168+
Serial.print("Error trying to execute setTemperatureOffsetSimple(): ");
169+
errorToString(error, errorMessage, 256);
170+
Serial.println(errorMessage);
171+
} else {
172+
Serial.print("Temperature Offset set to ");
173+
Serial.print(tempOffset);
174+
Serial.println(" deg. Celsius (SEN54/SEN55 only");
175+
}
176+
177+
// Start Measurement
178+
error = sen5x.startMeasurement();
179+
if (error) {
180+
Serial.print("Error trying to execute startMeasurement(): ");
181+
errorToString(error, errorMessage, 256);
182+
Serial.println(errorMessage);
183+
}
184+
185+
}
186+
187+
void loop() {
188+
uint16_t error;
189+
char errorMessage[256];
190+
191+
delay(1000);
192+
193+
// Read Measurement
194+
float massConcentrationPm1p0;
195+
float massConcentrationPm2p5;
196+
float massConcentrationPm4p0;
197+
float massConcentrationPm10p0;
198+
float ambientHumidity;
199+
float ambientTemperature;
200+
float vocIndex;
201+
float noxIndex;
202+
203+
error = sen5x.readMeasuredValues(
204+
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
205+
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
206+
noxIndex);
207+
208+
if (error) {
209+
Serial.print("Error trying to execute readMeasuredValues(): ");
210+
errorToString(error, errorMessage, 256);
211+
Serial.println(errorMessage);
212+
} else {
213+
Serial.print("MassConcentrationPm1p0:");
214+
Serial.print(massConcentrationPm1p0);
215+
Serial.println("\t");
216+
Serial.print("MassConcentrationPm2p5:");
217+
Serial.print(massConcentrationPm2p5);
218+
Serial.println("\t");
219+
Serial.print("MassConcentrationPm4p0:");
220+
Serial.print(massConcentrationPm4p0);
221+
Serial.println("\t");
222+
Serial.print("MassConcentrationPm10p0:");
223+
Serial.print(massConcentrationPm10p0);
224+
Serial.println("\t");
225+
Serial.print("AmbientHumidity:");
226+
if (isnan(ambientHumidity)) {
227+
Serial.print("n/a");
228+
} else {
229+
Serial.print(ambientHumidity);
230+
}
231+
Serial.println("\t");
232+
Serial.print("AmbientTemperature:");
233+
if (isnan(ambientTemperature)) {
234+
Serial.print("n/a");
235+
} else {
236+
Serial.print(ambientTemperature);
237+
}
238+
Serial.println("\t");
239+
Serial.print("VocIndex:");
240+
if (isnan(vocIndex)) {
241+
Serial.print("n/a");
242+
} else {
243+
Serial.print(vocIndex);
244+
}
245+
Serial.println("\t");
246+
Serial.print("NoxIndex:");
247+
if (isnan(noxIndex)) {
248+
Serial.println("n/a");
249+
} else {
250+
Serial.println(noxIndex);
251+
}
252+
Serial.println();
253+
}
254+
}

0 commit comments

Comments
 (0)