Skip to content

Commit e1ebc9e

Browse files
committed
Add example for readout pm values in floating point representation
1 parent ce2e7bb commit e1ebc9e

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) 2020, Sensirion AG
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* * Neither the name of Sensirion AG nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#include <Wire.h>
33+
34+
// SEN44
35+
const int16_t SEN44_ADDRESS = 0x69;
36+
37+
// union construct to help to convert byte array to float
38+
union u_tag {
39+
byte b[4];
40+
float fval;
41+
} u;
42+
43+
void setup() {
44+
Serial.begin(115200);
45+
// wait for serial connection from PC
46+
// comment the following line if you'd like the output
47+
// without waiting for the interface being ready
48+
while(!Serial);
49+
50+
// output format
51+
Serial.println("PM1.0\tPM2.5\tPM4.0\tPM10.0\tVOC_Index\tRH\tT");
52+
53+
// init I2C
54+
Wire.begin();
55+
56+
// wait until sensors startup, > 1 ms according to datasheet
57+
delay(1);
58+
59+
// start up sensor, sensor will go to continuous measurement mode
60+
// each second there will be new measurement values
61+
Wire.beginTransmission(SEN44_ADDRESS);
62+
Wire.write(0x00);
63+
Wire.write(0x21);
64+
Wire.endTransmission();
65+
66+
// wait until sensor is ready, fan is initialized
67+
delay(1000);
68+
}
69+
70+
void loop() {
71+
72+
float pm1p0_f, pm2p5_f, pm4p0_f, pm10p0_f;
73+
int16_t voc, humidity, temperature;
74+
uint8_t data[40], counter;
75+
76+
// send read measurement data command to get pm values in floating point
77+
Wire.beginTransmission(SEN44_ADDRESS);
78+
Wire.write(0x03);
79+
Wire.write(0x31);
80+
Wire.endTransmission();
81+
82+
// wait 10 ms to allow the sensor to fill the internal buffer
83+
delay(10);
84+
85+
// read measurement data SEN44, after two bytes a CRC follows
86+
Wire.requestFrom(SEN44_ADDRESS, 40);
87+
counter = 0;
88+
while (Wire.available()) {
89+
data[counter++] = Wire.read();
90+
}
91+
92+
// only the first four float values are used
93+
// rest will be ignored
94+
// convert byte arrays to float
95+
u.b[0] = data[4];
96+
u.b[1] = data[3];
97+
u.b[2] = data[1];
98+
u.b[3] = data[0];
99+
pm1p0_f = u.fval;
100+
101+
u.b[0] = data[10];
102+
u.b[1] = data[9];
103+
u.b[2] = data[7];
104+
u.b[3] = data[6];
105+
pm2p5_f = u.fval;
106+
107+
u.b[0] = data[16];
108+
u.b[1] = data[15];
109+
u.b[2] = data[13];
110+
u.b[3] = data[12];
111+
pm4p0_f = u.fval;
112+
113+
u.b[0] = data[22];
114+
u.b[1] = data[21];
115+
u.b[2] = data[19];
116+
u.b[3] = data[18];
117+
pm10p0_f = u.fval;
118+
119+
Serial.print(pm1p0_f);
120+
Serial.print("\t");
121+
Serial.print(pm2p5_f);
122+
Serial.print("\t");
123+
Serial.print(pm4p0_f);
124+
Serial.print("\t");
125+
Serial.print(pm10p0_f);
126+
Serial.print("\t");
127+
128+
// send read measurement data command to get VOC, RH, T
129+
Wire.beginTransmission(SEN44_ADDRESS);
130+
Wire.write(0x03);
131+
Wire.write(0xA6);
132+
Wire.endTransmission();
133+
134+
// wait 10 ms to allow the sensor to fill the internal buffer
135+
delay(10);
136+
137+
// read measurement data SEN44, after two bytes a CRC follows
138+
Wire.requestFrom(SEN44_ADDRESS, 9);
139+
counter = 0;
140+
while (Wire.available()) {
141+
data[counter++] = Wire.read();
142+
}
143+
144+
// VOC level is a signed int and scaled by a factor of 10 and needs to be divided by 10
145+
// humidity is a signed int and scaled by 100 and need to be divided by 100
146+
// temperature is a signed int and scaled by 200 and need to be divided by 200
147+
voc = (uint16_t)data[0] << 8 | data[1];
148+
humidity = (uint16_t)data[3] << 8 | data[4];
149+
temperature = (uint16_t)data[6] << 8 | data[7];
150+
151+
Serial.print(String(float(voc) / 10));
152+
Serial.print("\t");
153+
Serial.print(String(float(humidity) / 100));
154+
Serial.print("\t");
155+
Serial.print(String(float(temperature) / 200));
156+
Serial.println();
157+
158+
delay(10);
159+
160+
// wait 1 s for next measurement
161+
delay(1000);
162+
}

0 commit comments

Comments
 (0)