Skip to content

Commit 9513983

Browse files
committed
Add SFA30 example
1 parent 8657c34 commit 9513983

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
// SFA30
35+
const int16_t SFA_ADDRESS = 0x5D;
36+
37+
void setup() {
38+
Serial.begin(115200);
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+
// init I2C
45+
Wire.begin();
46+
47+
// wait until sensor is ready
48+
delay(10);
49+
50+
// start SFA measurement in periodic mode, will update every 0.5 s
51+
Wire.beginTransmission(SFA_ADDRESS);
52+
Wire.write(0x00);
53+
Wire.write(0x06);
54+
Wire.endTransmission();
55+
56+
// module is not outputing HCHO for the first 10 s after powering up
57+
delay(10000);
58+
59+
}
60+
61+
void loop() {
62+
float hcho, temperature, humidity;
63+
uint8_t data[9], counter;
64+
65+
// send read data command
66+
Wire.beginTransmission(SFA_ADDRESS);
67+
Wire.write(0x03);
68+
Wire.write(0x27);
69+
Wire.endTransmission();
70+
71+
//wait time before reading for the values should be more than 2ms
72+
delay(10);
73+
74+
// read measurement data:
75+
// 2 bytes formaldehyde, 1 byte CRC, scale factor 5
76+
// 2 bytes RH, 1 byte CRC, scale factor 100
77+
// 2 bytes T, 1 byte CRC, scale factor 200
78+
// stop reading after 9 bytes (not used)
79+
Wire.requestFrom(SFA_ADDRESS, 9);
80+
counter = 0;
81+
while (Wire.available()) {
82+
data[counter++] = Wire.read();
83+
}
84+
85+
// floating point conversion according to datasheet
86+
hcho = (float)((int16_t)data[0] << 8 | data[1])/5;
87+
// convert RH in %
88+
humidity = (float)((int16_t)data[3] << 8 | data[4])/100;
89+
// convert T in degC
90+
temperature = (float)((int16_t)data[6] << 8 | data[7])/200;
91+
92+
Serial.print(hcho);
93+
Serial.print("\t");
94+
Serial.print(temperature);
95+
Serial.print("\t");
96+
Serial.print(humidity);
97+
Serial.println();
98+
99+
// wait 1 s for next measurement read out, sensor is averaging in between
100+
delay(1000);
101+
102+
103+
}

0 commit comments

Comments
 (0)