Skip to content

Commit 9d4cbff

Browse files
committed
Add example for SEN44 to read raw signals VOC and RH, T
1 parent e1ebc9e commit 9d4cbff

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
void setup() {
38+
Serial.begin(115200); // start serial for output
39+
// wait for serial connection from PC
40+
// comment the following line if you'd like the output
41+
// without waiting for the interface being ready
42+
while(!Serial);
43+
44+
// output format
45+
Serial.println("PM1.0\tPM2.5\tPM4.0\tPM10.0\tVOC_Index\tRH\tT\tVOC_Raw\tRH_Uncomp\tT_Uncomp");
46+
47+
// init I2C
48+
Wire.begin();
49+
50+
// wait until sensors startup, > 1 ms according to datasheet
51+
delay(1);
52+
53+
// start up sensor, sensor will go to continous measurement mode
54+
// each second there will be new measurement values
55+
Wire.beginTransmission(SEN44_ADDRESS);
56+
Wire.write(0x00);
57+
Wire.write(0x21);
58+
Wire.endTransmission();
59+
60+
// wait until sensor is ready, fan is initialized
61+
delay(1000);
62+
}
63+
64+
void loop() {
65+
uint16_t pm1p0, pm2p5, pm4p0, pm10p0, voc_raw;
66+
int16_t voc, humidity, temperature;
67+
int16_t humidity_uncomp, temperature_uncomp;
68+
uint8_t data[30], counter;
69+
70+
// read measurement data
71+
Wire.beginTransmission(SEN44_ADDRESS);
72+
Wire.write(0x03);
73+
Wire.write(0x81);
74+
Wire.endTransmission();
75+
76+
// wait 5 ms to allow the sensor to fill the internal buffer
77+
delay(10);
78+
79+
// read measurement data sen4x, after two bytes a CRC follows
80+
Wire.requestFrom(SEN44_ADDRESS, 30);
81+
counter = 0;
82+
while (Wire.available()) {
83+
data[counter++] = Wire.read();
84+
}
85+
86+
// PM1.0 to PM10 are unscaled unsigned integer values in ug / um3
87+
// VOC level is a signed int and scaled by a factor of 10 and needs to be divided by 10
88+
// VOC raw value is an uint16_t and has no scaling
89+
// humidity is a signed int and scaled by 100 and need to be divided by 100
90+
// temperature is a signed int and scaled by 200 and need to be divided by 200
91+
pm1p0 = (uint16_t)data[0] << 8 | data[1];
92+
pm2p5 = (uint16_t)data[3] << 8 | data[4];
93+
pm4p0 = (uint16_t)data[6] << 8 | data[7];
94+
pm10p0 = (uint16_t)data[9] << 8 | data[10];
95+
voc = (uint16_t)data[12] << 8 | data[13];
96+
humidity = (uint16_t)data[15] << 8 | data[16];
97+
temperature = (uint16_t)data[18] << 8 | data[19];
98+
voc_raw = (uint16_t)data[21] << 8 | data[22];
99+
humidity_uncomp = (uint16_t)data[24] << 8 | data[25];
100+
temperature_uncomp = (uint16_t)data[27] << 8 | data[28];
101+
102+
Serial.print(pm1p0);
103+
Serial.print("\t");
104+
Serial.print(pm2p5);
105+
Serial.print("\t");
106+
Serial.print(pm4p0);
107+
Serial.print("\t");
108+
Serial.print(pm10p0);
109+
Serial.print("\t");
110+
Serial.print(String(float(voc) / 10));
111+
Serial.print("\t");
112+
Serial.print(String(float(humidity) / 100));
113+
Serial.print("\t");
114+
Serial.print(String(float(temperature) / 200));
115+
Serial.print("\t");
116+
Serial.print(voc_raw);
117+
Serial.print("\t");
118+
Serial.print(String(float(humidity_uncomp) / 100));
119+
Serial.print("\t");
120+
Serial.print(String(float(temperature_uncomp) / 200));
121+
Serial.println();
122+
123+
// wait 1 s for next measurement
124+
delay(1000);
125+
}

0 commit comments

Comments
 (0)