Skip to content

Commit b438d2e

Browse files
committed
added SHT2x third party sensor library
1 parent 24b41b2 commit b438d2e

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an Arduino library for the SHT21 / SHT25 humidity and temperature sensors.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/****************************************************************
2+
* ReadSHT2x
3+
* An example sketch that reads the sensor and prints the
4+
* relative humidity to the PC's serial port
5+
*
6+
* Tested with:
7+
* - SHT21-Breakout Humidity sensor from Modern Device
8+
* - SHT2x-Breakout Humidity sensor from MisensO Electronics
9+
***************************************************************/
10+
11+
#include <Wire.h>
12+
#include <SHT2x.h>
13+
14+
15+
void setup()
16+
{
17+
pinMode(Vext, OUTPUT);
18+
digitalWrite(Vext, LOW);
19+
delay(500);
20+
21+
Wire.begin();
22+
Serial.begin(115200);
23+
}
24+
25+
void loop()
26+
{
27+
Serial.print("Humidity(%RH): ");
28+
Serial.print(SHT2x.GetHumidity());
29+
Serial.print(" Temperature(C): ");
30+
Serial.println(SHT2x.GetTemperature());
31+
Serial.print(" Dewpoint(C): ");
32+
Serial.println(SHT2x.GetDewPoint());
33+
34+
delay(1000);
35+
}
36+

libraries/Sensor_ThirdParty/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ Currently with following sensors:
1515
| BME280 | Seeed Studio | [Seeed Studio](https://github.com/Seeed-Studio/Grove_BME280) | Work well |
1616
| BME680 | SV-Zanshin | [SV-Zanshin](https://github.com/SV-Zanshin/BME680) | Work well |
1717
| ADS1x15| Adafruit | [Adafruit](https://github.com/adafruit/Adafruit_ADS1X15) | not tested yet |
18+
| SHT2x | SodaqMoja | [SodaqMoja](https://github.com/SodaqMoja/Sodaq_SHT2x) | not tested yet |
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
SHT2x - A Humidity Library for Arduino.
3+
4+
Supported Sensor modules:
5+
SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
6+
SHT2x-Breakout Module - http://www.misenso.com/products/001
7+
8+
Created by Christopher Ladden at Modern Device on December 2009.
9+
Modified by Paul Badger March 2010
10+
11+
Modified by www.misenso.com on October 2011:
12+
- code optimisation
13+
- compatibility with Arduino 1.0
14+
15+
* This file is part of Sodaq_SHT2x.
16+
*
17+
* Sodaq_SHT2x is free software: you can redistribute it and/or
18+
* modify it under the terms of the GNU Lesser General Public License
19+
* as published by the Free Software Foundation, either version 3 of
20+
* the License, or(at your option) any later version.
21+
*
22+
* Sodaq_SHT2x is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU Lesser General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU Lesser General Public
28+
* License along with Sodaq_SHT2x. If not, see
29+
* <http://www.gnu.org/licenses/>.
30+
*/
31+
32+
#include <stdint.h>
33+
#include <math.h>
34+
#include <Arduino.h>
35+
#include <Wire.h>
36+
#include "SHT2x.h"
37+
38+
// Specify the constants for water vapor and barometric pressure.
39+
#define WATER_VAPOR 17.62f
40+
#define BAROMETRIC_PRESSURE 243.5f
41+
42+
typedef enum {
43+
eSHT2xAddress = 0x40,
44+
} HUM_SENSOR_T;
45+
46+
typedef enum {
47+
eTempHoldCmd = 0xE3,
48+
eRHumidityHoldCmd = 0xE5,
49+
eTempNoHoldCmd = 0xF3,
50+
eRHumidityNoHoldCmd = 0xF5,
51+
} HUM_MEASUREMENT_CMD_T;
52+
53+
54+
/******************************************************************************
55+
* Global Functions
56+
******************************************************************************/
57+
58+
/**********************************************************
59+
* GetHumidity
60+
* Gets the current humidity from the sensor.
61+
*
62+
* @return float - The relative humidity in %RH
63+
**********************************************************/
64+
float SHT2xClass::GetHumidity(void)
65+
{
66+
float value = readSensor(eRHumidityHoldCmd);
67+
if (value == 0) {
68+
return 0; // Some unrealistic value
69+
}
70+
return -6.0 + 125.0 / 65536.0 * value;
71+
}
72+
73+
/**********************************************************
74+
* GetTemperature
75+
* Gets the current temperature from the sensor.
76+
*
77+
* @return float - The temperature in Deg C
78+
**********************************************************/
79+
float SHT2xClass::GetTemperature(void)
80+
{
81+
float value = readSensor(eTempHoldCmd);
82+
if (value == 0) {
83+
return -273; // Roughly Zero Kelvin indicates an error
84+
}
85+
return -46.85 + 175.72 / 65536.0 * value;
86+
}
87+
88+
/**********************************************************
89+
* GetDewPoint
90+
* Gets the current dew point based on the current humidity and temperature
91+
*
92+
* @return float - The dew point in Deg C
93+
**********************************************************/
94+
float SHT2xClass::GetDewPoint(void)
95+
{
96+
float humidity = GetHumidity();
97+
float temperature = GetTemperature();
98+
99+
// Calculate the intermediate value 'gamma'
100+
float gamma = log(humidity / 100) + WATER_VAPOR * temperature / (BAROMETRIC_PRESSURE + temperature);
101+
// Calculate dew point in Celsius
102+
float dewPoint = BAROMETRIC_PRESSURE * gamma / (WATER_VAPOR - gamma);
103+
104+
return dewPoint;
105+
}
106+
107+
/******************************************************************************
108+
* Private Functions
109+
******************************************************************************/
110+
111+
uint16_t SHT2xClass::readSensor(uint8_t command)
112+
{
113+
uint16_t result;
114+
115+
Wire.beginTransmission(eSHT2xAddress);
116+
Wire.write(command);
117+
delay(100);
118+
Wire.endTransmission();
119+
120+
Wire.requestFrom(eSHT2xAddress, 3);
121+
uint32_t timeout = millis() + 300; // Don't hang here for more than 300ms
122+
while (Wire.available() < 3) {
123+
if ((millis() - timeout) > 0) {
124+
return 0;
125+
}
126+
}
127+
128+
//Store the result
129+
result = Wire.read() << 8;
130+
result += Wire.read();
131+
result &= ~0x0003; // clear two low bits (status bits)
132+
133+
//Clear the final byte from the buffer
134+
Wire.read();
135+
136+
return result;
137+
}
138+
139+
SHT2xClass SHT2x;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
SHT2x - A Humidity Library for Arduino.
3+
4+
Supported Sensor modules:
5+
SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
6+
SHT2x-Breakout Module - http://www.misenso.com/products/001
7+
8+
Created by Christopher Ladden at Modern Device on December 2009.
9+
10+
Modified by www.misenso.com on October 2011:
11+
- code optimisation
12+
- compatibility with Arduino 1.0
13+
14+
* This file is part of Sodaq_SHT2x.
15+
*
16+
* Sodaq_SHT2x is free software: you can redistribute it and/or
17+
* modify it under the terms of the GNU Lesser General Public License
18+
* as published by the Free Software Foundation, either version 3 of
19+
* the License, or(at your option) any later version.
20+
*
21+
* Sodaq_SHT2x is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Lesser General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Lesser General Public
27+
* License along with Sodaq_SHT2x. If not, see
28+
* <http://www.gnu.org/licenses/>.
29+
*/
30+
31+
32+
#ifndef SHT2X_H
33+
#define SHT2X_H
34+
35+
#include <stdint.h>
36+
37+
class SHT2xClass
38+
{
39+
private:
40+
uint16_t readSensor(uint8_t command);
41+
42+
public:
43+
float GetHumidity(void);
44+
float GetTemperature(void);
45+
float GetDewPoint(void);
46+
};
47+
48+
extern SHT2xClass SHT2x;
49+
50+
#endif

0 commit comments

Comments
 (0)