Skip to content

Commit f2a48d6

Browse files
committed
Resolve issue #20 by moving delay after 2 second read check to make read return quickly.
1 parent 9611395 commit f2a48d6

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

DHT.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,8 @@ boolean DHT::read(void) {
9393
uint8_t j = 0, i;
9494
unsigned long currenttime;
9595

96-
// pull the pin high and wait 250 milliseconds
97-
digitalWrite(_pin, HIGH);
98-
delay(250);
99-
96+
// Check if sensor was read less than two seconds ago and return early
97+
// to use last reading.
10098
currenttime = millis();
10199
if (currenttime < _lastreadtime) {
102100
// ie there was a rollover
@@ -115,6 +113,10 @@ boolean DHT::read(void) {
115113

116114
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
117115

116+
// pull the pin high and wait 250 milliseconds
117+
digitalWrite(_pin, HIGH);
118+
delay(250);
119+
118120
// now pull it low for ~20 milliseconds
119121
pinMode(_pin, OUTPUT);
120122
digitalWrite(_pin, LOW);

examples/DHTtester/DHTtester.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ void setup() {
2525
}
2626

2727
void loop() {
28+
// Wait a few seconds between measurements.
29+
delay(2000);
30+
2831
// Reading temperature or humidity takes about 250 milliseconds!
2932
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
3033
float h = dht.readHumidity();

0 commit comments

Comments
 (0)