I'm making a weather station using DHT22 with Adafruit's 1.1.0 library. I use millis to pull sensor data every 10 seconds. And the result is reflected on the 5110 LCD screen.
But very often I get 0 values from sensor. Is there anything I can do to avoid this? Like trying several times until sensor gives me valid data?
It's fine if the data on screen is not updated at that moment, I just don't want zeroes on the screen.
EDIT:
I tried to fix it using do while loop
do
read sensor code
while (humidity ==0);
But now it hangs like 2-3 secs, making Arduino unresponsive. I have a menu system and user can navigate in the menu using a joystick so such a long hang is so bad.
google dht22 unstable reading. The sensor is voltage sensitive so usb power is not going to give a good result. Also the ground from the sensor needs to really be landed at the arduino not on a bread board with other items. A capacitor around 100uf between the positive and negative lead also help. (you should already have a 4.7kohm to 10k resistor as a pull up between the signal cable and the positive)
you example may have contained code to test the sensor.
if (temp_timer >=1){
h = dht.readHumidity();
// Read temperature as Celsius
t = dht.readTemperature();
// Read temperature as Fahrenheit
f = dht.readTemperature(true);
humidity = h;
temp =f;
temp_timer =0;}
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
}
use the isnan argument to stop the temp being changed if the reading is bad.
My dht22 is stable to less than 0.2f (in the beginning it use to fluctuate anything up to 20f then I made the changes to my set up as suggested and I haven't seen a fail to read over the last 30 days)