DHT22 Not measuring properly (Solved)

Greetings, I was working on a project with Arduino UNO and DHT22, reads temperature and humidity, makes some calculus and send the results to an 16x2 LCD screen
When I connected it to my desktop computer the LCD started working fine, but the measures are NAN for both values. But sometimes it sends some weird values like 12,7 Celsius and 3287,7% humidity (my temperature is around 25°) and then in the next measurement it goes back to NAN again, randomly

  • I have checked connections and everything seems to be in order

  • I have checked with another DHT22, same results

  • I tried this circuit before (for the first time) with a friend’s laptop as a power source and it worked perfectly

I have checked this with many sample codes and the conclusion is that the problem should be either the power source of my computer , or both of my DHT22 are failing
Or maybe I just messed up the libraries?

This is the code I’m now using as a test

#include <LiquidCrystal.h>
// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

#include <DHT.h>

#define DHTPIN 7 //Pin 7 of Arduino UNO
#define DHTTYPE DHT22 //Define sensor as DHT22


float t;
float h;

void setup()
lcd.begin(16, 2);

void loop()
delay(2500); //Tiempo de espera entre una lectura y otra. Para el DHT22 son 2 segundos

t = dht.readTemperature(); //Reads temperature
h = dht.readHumidity(); //Reads humidity

lcd.setCursor(0, 0); //Shows temperature
lcd.write(“Temp.: “);
lcd.write(” C”);

lcd.setCursor(0, 1); //Shows humidity
lcd.write(“Hume.: “);
lcd.write(” %”);


EDIT: problem solved, I added dht.begin(); in void_setup(). Thanks everyone for the help!

I was playing with a DHT11 last week. Mine is mounted on an I2C interface board which has 3 pins VCC, Data, and GND.

Here's an Amazon link to what mine looks like.
DHT11 Board on Amazon

Below is the code that I was successful in getting working. I had to include the Adafruit libraries to make it all compile.

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

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN 8     // Digital pin connected to the DHT sensor.  This works on the Mega
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

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

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 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

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.

void setup() {
  Serial.println(F("DHTxx test!"));


void loop() {
  // Wait a few seconds between measurements.

  // 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();
//  // Read temperature as Celsius (the default)
//  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
/*  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
  }   */
  if (isnan(h)) {
    Serial.println(F("Failed to read Humidity from DHT sensor!"));
/*  if (isnan(t)) {
    Serial.println(F("Failed to read Celcius from DHT sensor!"));
  if (isnan(f)) {
    Serial.println(F("Failed to read Fahrenheit from DHT sensor!"));

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
/*  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false); */

  Serial.print(F("Humidity: "));
  Serial.print(F("%  Temp: "));
//  Serial.print(t);
//  Serial.print(F("°C "));
  Serial.print(F("°F  Heat index: "));
//  Serial.print(hic);
//  Serial.print(F("°C "));

Hope this helps. No LCD stuff, but once you get it taking readings properly, it's easy to add in the LCD display.


does a program just working with the DHT22 work OK, e.g. Temperature Monitoring With DHT22

also use code tages for postig code, see forum guidelines

I would like to recommend you using the DHT library from Adafruit. See how to install it in Arduino DHT22 tutorial

For displaying the temperature and humidity on LCD, See Arduino display temperature and humidity on LCD tutorial

Hi edyorac,
Your test code is close to perfection, however there is an essential line missing that is necessary to initialize the sensor:

in void setup:

void setup()
  lcd.begin(16, 2);

As IoT_hobbyist comments one option is to try another <DHT.h> library. There are several libraries on the internet of which several are doubtful.

success, photoncatcher

I would recommend downloading a great DHT library by Adafruit in library manager:

Greeting to everyone, I have all the adafruit libraries, that was not the problem

I just found the error, here

In void_setup, I must add:


And that's it! Thanks everyone for your help!