Skip to content

Commit 8f05bc3

Browse files
committed
Add SHT4x example
1 parent 3085765 commit 8f05bc3

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

SDP8xx_I2C_minimal_example/SDP8xx_I2C_minimal_example.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131

3232
#include <Wire.h>
3333

34-
// SDP810 500 Pa
35-
const int16_t SDP810_ADDRESS = 0x25;
34+
// SDP8xx with I2C address = 0x25
35+
// depending on the sensor type this could be alternatively 0x26
36+
const int16_t SDP8xx_ADDRESS = 0x25;
3637

3738
void setup() {
3839
Serial.begin(115200);
@@ -52,7 +53,7 @@ void setup() {
5253

5354
// start up sensor, sensor will go to continuous measurement mode
5455
// differential pressure mode with averaging till read
55-
Wire.beginTransmission(SDP810_ADDRESS);
56+
Wire.beginTransmission(SDP8xx_ADDRESS);
5657
Wire.write(0x36);
5758
Wire.write(0x15);
5859
Wire.endTransmission();
@@ -68,7 +69,7 @@ void loop() {
6869
uint8_t data[9], counter;
6970

7071
// read measurement data, after two bytes a CRC follows
71-
Wire.requestFrom(SDP810_ADDRESS, 9);
72+
Wire.requestFrom(SDP8xx_ADDRESS, 9);
7273
counter = 0;
7374
while (Wire.available()) {
7475
data[counter++] = Wire.read();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// SHT4x with I2C address A (SHT4x-AD1B) = 0x44
35+
// SHT40 with I2C address A (SHT40-BD1B) = 0x45
36+
const int16_t SHT_ADDRESS = 0x44;
37+
38+
void setup() {
39+
Serial.begin(115200);
40+
// wait for serial connection from PC
41+
// comment the following line if you'd like the output
42+
// without waiting for the interface being ready
43+
while(!Serial);
44+
45+
// output format
46+
Serial.println("RelativeHumidity(percent)\tTemperature(degC)");
47+
48+
// init I2C
49+
Wire.begin();
50+
51+
// wait until sensors are ready, < 1 ms according to datasheet
52+
delay(1);
53+
}
54+
55+
void loop() {
56+
float temperature, humidity;
57+
uint8_t data[6], counter;
58+
59+
// start sht measurement in high prescision
60+
Wire.beginTransmission(SHT_ADDRESS);
61+
Wire.write(0xFD);
62+
Wire.endTransmission();
63+
64+
// wait for measurement has finished according to datasheet > 8.2 ms
65+
delay(9);
66+
67+
// read measurement data sht: 2 bytes T, 1 byte CRC, 2 bytes RH, 1 byte CRC
68+
Wire.requestFrom(SHT_ADDRESS, 6);
69+
counter = 0;
70+
while (Wire.available()) {
71+
data[counter++] = Wire.read();
72+
}
73+
74+
// floating point conversion according to datasheet
75+
// convert T in degC
76+
temperature = -45 + 175 * (float)((uint16_t)data[0] << 8 | data[1]) / 65535;
77+
// convert RH in %
78+
humidity = -6 + 125 * (float)((uint16_t)data[3] << 8 | data[4]) / 65535;
79+
80+
Serial.print(humidity);
81+
Serial.print("\t");
82+
Serial.print(temperature);
83+
Serial.println();
84+
85+
// wait 1 s for next measurement
86+
delay(1000);
87+
}

0 commit comments

Comments
 (0)