forked from adafruit/DHT-sensor-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDHT.cpp
More file actions
66 lines (55 loc) · 1.57 KB
/
DHT.cpp
File metadata and controls
66 lines (55 loc) · 1.57 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
/* DHT library
MIT license
In part (c) 2012 Chris Andreae
Includes portions written by Adafruit Industries
*/
#include "DHT.h"
DHT::DHT(uint8_t pin) {
_pin = pin;
}
static uint8_t waitFor(uint8_t val, uint8_t pin, uint8_t timeout_us){
uint32_t start = micros();
uint32_t timeout_t = start + timeout_us;
while(digitalRead(pin) != val){
if(micros() > timeout_t) return 0xff;
}
return micros() - start;
}
#define WAIT_FOR(val, timeout, error) ({ \
uint8_t __r = waitFor(val, _pin, timeout); \
if(__r == 0xff) return error; \
__r; \
})
/** May read only once every 2 seconds or so. This is not enforced. */
uint8_t DHT::read(void) {
uint8_t data[5] = {0}; // read buffer
uint8_t data_i = 0, data_b = 0;
digitalWrite(_pin, LOW); // externally pulled up: don't use internal pullup
// now pull it low for ~20 milliseconds
pinMode(_pin, OUTPUT);
delay(20);
pinMode(_pin, INPUT);
delayMicroseconds(40);
// sensor will respond by pulling low for 80us and high for 80us
WAIT_FOR(HIGH, 90, 1);
WAIT_FOR(LOW, 90, 2);
for(int i = 0; i < 40; ++i){
uint8_t* byte = &data[i/8];
*byte <<= 1;
// then each bit is 50us of low, and then either 25 or 70 us of high (0/1)
WAIT_FOR(HIGH, 60, 3);
uint8_t elapsed = WAIT_FOR(LOW, 90, 4);
if(elapsed > 40){
*byte |= 0x1;
}
}
// check we read 40 bits and that the checksum matches
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
humidity = (data[0] << 8) | data[1];
temperature = (data[2] << 8) | data[3];
return 0;
}
else{
return 5;
}
}