Skip to content

Commit 60ee6a1

Browse files
committed
Add SCD40 minimal example
1 parent a7b15ab commit 60ee6a1

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
// SCD40
35+
const int16_t SCD_ADDRESS = 0x62;
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+
// output format
45+
Serial.println("CO2(ppm)\tTemperature(degC)\tRelativeHumidity(percent)");
46+
47+
// init I2C
48+
Wire.begin();
49+
50+
// wait until sensors are ready, > 1000 ms according to datasheet
51+
delay(1000);
52+
53+
// start scd measurement in periodic mode, will update every 2 s
54+
Wire.beginTransmission(SCD_ADDRESS);
55+
Wire.write(0x21);
56+
Wire.write(0xb1);
57+
Wire.endTransmission();
58+
59+
// wait for first measurement to be finished
60+
delay(2000);
61+
}
62+
63+
void loop() {
64+
float co2, temperature, humidity;
65+
uint8_t data[12], counter;
66+
67+
// send read data command
68+
Wire.beginTransmission(SCD_ADDRESS);
69+
Wire.write(0xec);
70+
Wire.write(0x05);
71+
Wire.endTransmission();
72+
73+
// read measurement data: 2 bytes co2, 1 byte CRC,
74+
// 2 bytes T, 1 byte CRC, 2 bytes RH, 1 byte CRC,
75+
// 2 bytes sensor status, 1 byte CRC
76+
// stop reading after 12 bytes (not used)
77+
// other data like ASC not included
78+
Wire.requestFrom(SCD_ADDRESS, 12);
79+
counter = 0;
80+
while (Wire.available()) {
81+
data[counter++] = Wire.read();
82+
}
83+
84+
// floating point conversion according to datasheet
85+
co2 = (float)((uint16_t)data[0] << 8 | data[1]);
86+
// convert T in degC
87+
temperature = -45 + 175 * (float)((uint16_t)data[3] << 8 | data[4]) / 65536;
88+
// convert RH in %
89+
humidity = 100 * (float)((uint16_t)data[6] << 8 | data[7]) / 65536;
90+
91+
Serial.print(co2);
92+
Serial.print("\t");
93+
Serial.print(temperature);
94+
Serial.print("\t");
95+
Serial.print(humidity);
96+
Serial.println();
97+
98+
// wait 2 s for next measurement
99+
delay(2000);
100+
}

0 commit comments

Comments
 (0)