DHT22 is just randomly giving a result, else NAN

Hello together.

I have a strange problem at the moment and hope you can help me.
I just build a little weather station (actually just a project to play around with all the stuff). Therefore I solder a breadboard arduino to a board.

Here is what I have:

The NRF24L01 is sending the sensor data to an RPi. This part works very well now. But the DHT22 is giving most of the time a NAN. I thought already that I damaged the sensor while soldering. But then values came... and after NAN again. So it came out that sometimes the sensor is giving values and sometimes not (always either no value for temp and humidity or it gives both values).

And there is another strange thing. I also want to add a 'flame sensor' (this dark LED). It's not soldered yet in the circuit, but already in the code. And this A1 Pin gives me a value. And not just a constant value... its changing. (The photocell is on A0) How can this be? Where is this value coming from?

My current code:

#include <JeeLib.h>

#include <DHT.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

#define DHTPIN 7     
#define DHTTYPE DHT22 //DHT11, DHT21, DHT22

String writestring;
int led = 8;
int flameled = A1;
int lightsensor = A0;
//for nrf24 debug
int serial_putc( char c, FILE * )
  Serial.write( c );
  return c;

//for nrf24 debug
void printf_begin(void)
  fdevopen( &serial_putc, 0 );

//nRF24 set the pin 9 to CE and 10 to CSN/SS
// Cables are:
//     SS       -> 10
//     MOSI     -> 11
//     MISO     -> 12
//     SCK      -> 13

RF24 radio(9, 10);
//we only need a write pipe, but am planning to use it later
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

// here we can send up to 30 chars
char SendPayload[31] = "";

ISR(WDT_vect) { Sleepy::watchdogEvent(); } // Setup the watchdog

void setup(void) {
  Serial.begin(9600); //Debug
  //nRF24 configurations
  radio.setRetries(15, 15);
  radio.openReadingPipe(1, pipes[0]);
  radio.printDetails(); //for Debugging
  //pinMode(led, OUTPUT);
  dht.begin(); //DHT22 start

void loop() {
  writestring = "1;"; //Kennung des Arduino
  //digitalWrite(led, HIGH); // LED an
  float h = dht.readHumidity();     //Luftfeuchte auslesen
  float t = dht.readTemperature();  //Temperatur auslesen
  writestring += t;
  writestring += ";";
  writestring += h;
  writestring += ";";
  //Fame sensor
  int IRvalue = analogRead(flameled);
  writestring += IRvalue;
  writestring += ";";
  //Light sensor
  unsigned int Lightvalue = analogRead(lightsensor);
  writestring += Lightvalue;
  writestring += ";";
  // Assign distance to payload, here am sending it as string
  //dtostrf(distance, 2, 2, SendPayload);
  //add a tag
  //strcat(SendPayload, "X");   // add first string
  //send a heartbeat
  bool ok = radio.write(&SendPayload, strlen(SendPayload));
  if (ok)
  //digitalWrite(led, LOW); // LED aus

Hope you can help me. Thanks.


. And this A1 Pin gives me a value. And not just a constant value

The pin is "floating" so the output is undefined. Tie the input to ground or V+ and see if you read 0 or 1023.

How often are you reading the DHT? adafruit dht22 says the sensor should not be sampled at less than 2 seconds per sample. Blink without delay will show how to let the rest of your code run at full speed (no delay()s) and sample the DHT every 2 seconds.

Ah, ok. I will try it later.

I read the DHT22 every 30 seconds. It's not the min rate. I was checking it today an hour maybe. Absolute random. 5 times values, 2 times NaN, 3 times values, 1 time NaN, 7 times values... and so on.

I figured out that its not only with this one. I have another Atmega328 still on a breadboard (to test the circuits before I solder them) and there is the same problem.

Have you tried a pull up resistor on the data line? 10K or so. I have not needed it on my DHTs, but it seems to have helped others.

Hi there,

I've had a very similar issue! I thought my sensor was substandard and after a few days of trying, I found it was due to the Adafruit DHT22 library that I was using. I started using this lib and everything works perfectly!


Hope this helps.