-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHT2x.cpp
More file actions
89 lines (70 loc) · 2.77 KB
/
SHT2x.cpp
File metadata and controls
89 lines (70 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
SHT2x - A Humidity Library for Arduino.
Supported Sensor modules:
SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
SHT2x-Breakout Module - http://www.misenso.com/products/001
Created by Christopher Ladden at Modern Device on December 2009.
Modified by Paul Badger March 2010
Modified by www.misenso.com on October 2011:
- code optimisation
- compatibility with Arduino 1.0
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <inttypes.h>
#include <Wire.h>
#include "Arduino.h"
#include "SHT2x.h"
/******************************************************************************
* Global Functions
******************************************************************************/
/**********************************************************
* GetHumidity
* Gets the current humidity from the sensor.
*
* @return float - The relative humidity in %RH
**********************************************************/
float SHT2xClass::GetHumidity(void)
{
return (-6.0 + 125.0 / 65536.0 * (float)(readSensor(eRHumidityHoldCmd)));
}
/**********************************************************
* GetTemperature
* Gets the current temperature from the sensor.
*
* @return float - The temperature in Deg C
**********************************************************/
float SHT2xClass::GetTemperature(void)
{
return (-46.85 + 175.72 / 65536.0 * (float)(readSensor(eTempHoldCmd)));
}
/******************************************************************************
* Private Functions
******************************************************************************/
uint16_t SHT2xClass::readSensor(uint8_t command)
{
uint16_t result;
Wire.beginTransmission(eSHT2xAddress); //begin
Wire.write(command); //send the pointer location
delay(100);
Wire.endTransmission(); //end
Wire.requestFrom(eSHT2xAddress, 3);
while(Wire.available() < 3) {
; //wait
}
//Store the result
result = ((Wire.read()) << 8);
result += Wire.read();
result &= ~0x0003; // clear two low bits (status bits)
return result;
}
SHT2xClass SHT2x;