Wrong read from DHT22

Hi everyone,

I’m trying to read temperature and humidity from a DHT22 sensor on an Argon board.
I’ve tried two different libraries: Adafruit and PietteTech.
The former returns temperature=nan and humidity=0.0. The latter always returns -3.0.

How can I solve it? The code is very simple:

Adafruit version:

#include <Adafruit_DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
double temperature = 0;
double humidity = 0;
void setup() {
    Particle.variable("temperature", temperature);
    Particle.variable("humidity", humidity);
    dht.begin();
}
void loop() {
    temperature = dht.getTempCelcius();
    humidity = dht.getHumidity();
    Serial.printlnf("Temperature: %f", temperature);
    Serial.printlnf("Humidity   : %f", humidity);
    delay(3000);
}

Piette version:

#include <PietteTech_DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
PietteTech_DHT dht(DHTPIN, DHTTYPE);
double temperature = 0;
double humidity = 0;
void setup() {
    Particle.variable("temperature", temperature);
    Particle.variable("humidity", humidity);
    dht.begin();
}
void loop() {
    temperature = dht.readTemperature();
    humidity = dht.readHumidity();
    Serial.printlnf("Temperature: %f", temperature);
    Serial.printlnf("Humidity   : %f", humidity);
    delay(3000);
}

I’m currently testing the code under Libraries > PietteTech_DHT > DHT_simple.ino.
Results by PIN:
D2 => -3
D6 => -5
D7 => -5

I cannot talk for the Adafruit library but for the PietteTech library you need to acquire the data before accessing the values.

Have you tried the examples that come with the libraries?

BTW, are you sure the PietteTech library returns 7 and not -7?

// error codes                              
const int  DHTLIB_ERROR_CHECKSUM         = -1;
const int  DHTLIB_ERROR_ISR_TIMEOUT      = -2;
const int  DHTLIB_ERROR_RESPONSE_TIMEOUT = -3;
const int  DHTLIB_ERROR_DATA_TIMEOUT     = -4;
const int  DHTLIB_ERROR_ACQUIRING        = -5;
const int  DHTLIB_ERROR_DELTA            = -6;
const int  DHTLIB_ERROR_NOTSTARTED       = -7;

I’ve fixed the original post, with Piette it’s -3.0.

I think I’m acquiring fine, I’m using float PietteTech_DHT::readTemperature() and float PietteTech_DHT::readHumidity()

To @ScruffR 's point, you need to do an acquireAndWait() prior to trying to access the temperature and humidity readings.

You might want to post a snippet of your code that’s reading the values.

FWIW, here are some snippets of code I use in one of my several programs used for DHT22’s:

#define DHTTYPE  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D1           // Digital pin for communications

//declarations
void dht_wrapper(); // must be declared before the lib is initialized

//Lib instantiate
PietteTech_DHT DHT(DHTPIN,DHTTYPE, dht_wrapper);

void readSensor() {
  //read temperature and humidity from DHT22 sensor
  gLastRead = gNow;
  Log.info("readSensor entered");
  int result = DHT.acquireAndWait();
  delay(1000);
  Log.info("after acquireAndWait");
  gResult = result;
  switch (result) {
    case DHTLIB_OK:
      Log.info("DHT OK");
      gHumidity = DHT.getHumidity();
      gTemperature = DHT.getFahrenheit();
      Log.info("Temp: %.1f, Humidity: %.1f",gTemperature,gHumidity);
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Log.info("Error--Checksum error");
      break;
    case DHTLIB_ERROR_ISR_TIMEOUT:
      Log.info("Error--ISR time out error");
      break;
    case DHTLIB_ERROR_RESPONSE_TIMEOUT:
      Log.info("Error--Response time out error");
      break;
    case DHTLIB_ERROR_DATA_TIMEOUT:
      Log.info("Error--Data time out error");
      break;
    case DHTLIB_ERROR_ACQUIRING:
      Log.info("Error--Acquiring");
      break;
    case DHTLIB_ERROR_DELTA:
      Log.info("Error--Delta time to small");
      break;
    case DHTLIB_ERROR_NOTSTARTED:
      Log.info("Error--Not started");
      break;
    default:
      Log.info("Unknown error");
      break;
  }
  if(gResult!=DHTLIB_OK) {
    gErrorCode = result;
    Log.info("Failed to read from DHT sensor!");
    delay(5000);
  }

}

@ctmorrison Thanks for helping. I’ve added more information in the initial post.

I forgot to include the DHT.begin() that I have in setup().

It does appear, as @ScruffR suggested, you’re not doing a dht.acquireAndWait() prior to trying to read the temp and humidity.

@ctmorrison I’m using PietteTech_DHT.h. If you check the source code of the function that I mentioned:

float PietteTech_DHT::readTemperature() {
  acquireAndWait();
  return getCelsius();
}

Ahhh…I see that now. Do you have a pull-up resistor connected between the data pin and VCC?

@ctmorrison I’m using this sensor that has an embedded resistor:

@vault , what’s the part number of that sensor? I’ve used the DHT22 (from Adafruit and others) without issue with the code I have above. The only time I’ve had issues is if the sensor is bad (very rare).

There’s nothing printed on the sensor…

where did you get it?

here

How does your DHT22 look internally? Mine is empty… there are 4 metallic dots but no yellow circle like this one:

Hmmm…the ones I’ve used do not have the small PCB protruding from the bottom–just 4 leads (VCC, Gnd, Data, and NC). Having looked a bit at Amazon, it appears some folks had trouble when powering these with 3.3 Vdc and had better success at 5Vdc. Might be work trying. If you’ve powered it in reverse by accident, it’s likely smoked.

There are several offerings on Amazon, so I’m not sure I’m looking at the exact same one that you’re using.

Actually I’ve had problems with VUSB and reverted to 3.3V: the board was freezing after some minutes (the loop stopped and it was no longer doing its job), and I read that the cause was using 5V instead of 3.3V… Never happened to you?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.