File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Original file line number Diff line number Diff 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) {
You can’t perform that action at this time.
0 commit comments