Skip to content

Commit 01490aa

Browse files
Vary delay by sensor type a la PR #70
1 parent 261e997 commit 01490aa

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

DHT.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,35 @@ bool DHT::read(bool force) {
148148

149149
// Go into high impedence state to let pull-up raise data line level and
150150
// start the reading process.
151-
digitalWrite(_pin, HIGH);
151+
pinMode(_pin, INPUT_PULLUP);
152152
delay(1);
153153

154-
// First set data line low for 1 millisecond.
154+
// First set data line low for a period according to sensor type
155155
pinMode(_pin, OUTPUT);
156156
digitalWrite(_pin, LOW);
157-
delay(1);
157+
switch(_type) {
158+
case DHT22:
159+
case DHT21:
160+
delayMicroseconds(1100); // data sheet says "at least 1ms"
161+
break;
162+
case DHT11:
163+
default:
164+
delay(20); //data sheet says at least 18ms, 20ms just to be safe
165+
break;
166+
}
158167

159168
uint32_t cycles[80];
160169
{
161-
// Turn off interrupts temporarily because the next sections are timing critical
162-
// and we don't want any interruptions.
163-
InterruptLock lock;
164-
165170
// End the start signal by setting data line high for 40 microseconds.
166-
digitalWrite(_pin, HIGH);
167171
pinMode(_pin, INPUT_PULLUP);
168172

169173
// Now start reading the data line to get the value from the DHT sensor.
170174
delayMicroseconds(60); // Delay a bit to let sensor pull data line low.
171175

176+
// Turn off interrupts temporarily because the next sections
177+
// are timing critical and we don't want any interruptions.
178+
InterruptLock lock;
179+
172180
// First expect a low signal for ~80 microseconds followed by a high signal
173181
// for ~80 microseconds again.
174182
if (expectPulse(LOW) == TIMEOUT) {

0 commit comments

Comments
 (0)