Go Down

Topic: AM2302 DHT22 Sensor sometimes don't get a value (Read 94 times) previous topic - next topic

vinc14

Hello

my Code
Code: [Select]

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

const int sensorPin = A0; // Normal Temp MP36 / 36gz Chip
#define DHTPIN 7 // what pin we're connected to

#include "DHT.h"

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
    Serial.begin(9600);
    Serial.println("DHTxx test!");
    dht.begin();
}

void loop()
{

int sensorVal = analogRead(sensorPin);
  Serial.print("TMP36 / 36gz Chip: --> " );
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);

float voltage = (sensorVal/1024.0) * 5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  Serial.print(", degrees C: ");

float temperature = (voltage - .5) * 100;
  Serial.println(temperature);
// Serial.print("hallo askim, off es ist " );
// Serial.print(temperature);
// Serial.println(" C, warm!!!");


    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    delay(60);

    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h))
    {
        Serial.println("Failed to read from DHT");
    }
    else
    {   Serial.print("AM2302 DHT22 Chip: --> " );
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" *C");
        Serial.print("Temperatur Differenz: ");
        Serial.print(t - temperature);
        Serial.println(" Grad");
        Serial.print("Durchscnittliche Temperatur: ");
        Serial.print( (t + temperature)/2);
        Serial.println(" Grad Celsius");
        Serial.println("================================");
 delay(24000);
    }
}


so i got a output like

17:34:01.710 -> Failed to read from DHT
17:34:01.743 -> TMP36 / 36gz Chip: --> Sensor Value: 160, Volts: 0.78, degrees C: 28.12
17:34:01.843 -> Failed to read from DHT
17:34:01.843 -> TMP36 / 36gz Chip: --> Sensor Value: 159, Volts: 0.78, degrees C: 27.64

and then it reads the Sensordata and all works fine.

so when reading the dht data it is not possible to wait until they are there.
around line 52 i added a delay(60) but did not seems to work

have e nice day
vinc

pylon

Code: [Select]
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    delay(60);


Quote
around line 52 i added a delay(60) but did not seems to work
It doesn't make sense to wait after you already read the values.

It seems that your sensor just needs a bit more startup time so insert a delay() into the setup routine (1 second should be enough).


Go Up