Skip to content

Commit 61e38bc

Browse files
committed
Add SHT3x minimal example
1 parent 60ee6a1 commit 61e38bc

File tree

1 file changed

+89
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)