DHT11 -- a strange little sensor

Ok, so puzzle me this.

I have a rather intensive program running on a Nano, with timer2 operating as a 2 KHz. timer, and also an interrupt on Pin 3 which may interrupt from 0 to 20 times/second, but sporadically (motion detector).

I hooked up a DHT11 temp/humidity sensor on Pin2, and tried to read it every 60 seconds in the main loop. It only returned zeros. (And it does have a pullup resistor on the data pin.) After a lot of experimentation, I discovered that if I read it once during setup, it would return good values down in the main loop, but only if I read it in setup() before I initialized timer2. If I initialized the timer, but then read the DHT11 below that point, it's back to all zeros output from the DHT11.

I'm not going to post the code, because it's over 500 lines, but what I have reported should be an adequate description of this oddity.

Anybody want to explain this to me?

Thanks in advance for any insights.

John Doner

Which DHT11 library are you using ?
The DHT11 uses specific timing, so an interrupt distorts the timing for the DHT11.
Some DHT11 libraries disable the interrupt temporarely to be sure it is okay, but that stops all interrupts.
I made a sketch that tries 10 times to get a value from the sensor (without disabling interrupts), but even that was not enough. Now I keep requestion data from the DTH11 in the background, and update the values if finally the data was okay.

I'm not going to post the code, because it's over 500 lines, but what I have reported should be an adequate description of this oddity.

You could strip it down to a minimal version that shows the problem.

having written 2 DHT libs I agree with Caltoa, interrupts, especially large ones, are disrupting the DHT protocol.

I suggest that you write a separate sketch that just reads the DHT11. Experiment with that until it works. The review your large program with that experience.

As a matter of fact, I have written a separate DHT11 only program which only implements the 2 KHz. timer running a simple ISR, and then reading the DHT11 every 3 seconds in the mai9n loop. That one works.

As it turns out, if I shut timer2 off just for the duration of reading the DHT11, I get good readings. I know that the DHT library uses some of the Arduino delay functions, which I believe are all based on timer0, but apparently, the extra interrupt activity from timer2 is enough to mess up the DHT comm. protocol. Fortunately, my timer2 activity does not require extremely tight timing.

That's the same as I had.
Since I have a I2C interrupt, it need the interrupt to be handled fast.
In the end, it was kind of useless, since I already had the temperature from a BMP085 sensor, and the humidity of my DHT11 is 50% too low.