Ir para conteúdo
View in the app

A better way to browse. Learn more.

EletrônicaBR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Navegação Limpa e Sem Anúncios! (Cadastre-se)

O melhor fórum técnico do mundo está de cara nova e de portas abertas! Estamos de cara nova e com ferramentas ainda mais poderosas para a sua bancada. Faça o seu cadastro de forma rápida e simples para acessar o maior e mais atualizado acervo de Esquemas Elétricos, BIOS e Firmwares da internet.

Aqui, o conhecimento vale muito: através do nosso sistema de créditos, membros participativos ganham acesso totalmente gratuito aos downloads. Venha trocar experiências com os melhores especialistas do mercado!

👉 Técnico sem o EletrônicaBR não é um técnico completo!

 

ESP32 - DHT22 sensor - reads NaN on serial monitor

Featured Replies

Postado

Board/platform: ESP32 DevKit V1 (38-pin, CH340 USB driver)

Module/component: DHT22 temperature and humidity sensor

Power supply:

  • ESP32 powered via USB from PC (5V, ~500mA available)

  • DHT22 powered from ESP32's 3.3V pin

  • Common ground: yes, DHT22 GND connected to ESP32 GND

Wiring:

  • DHT22 VCC → ESP32 3.3V

  • DHT22 GND → ESP32 GND

  • DHT22 DATA → ESP32 GPIO4

  • ESP32 - DHT22 | ESP32 Tutorial


  • Code snippet:

    cpp

    #include <DHT.h>
    
    #define DHTPIN 4
    #define DHTTYPE DHT22
    
    DHT dht(DHTPIN, DHTTYPE);
    
    void setup() {
      Serial.begin(115200);
      dht.begin();
    }
    
    void loop() {
      float temp = dht.readTemperature();
      float hum = dht.readHumidity();
      Serial.print("Temp: ");
      Serial.print(temp);
      Serial.print(" Hum: ");
      Serial.println(hum);
      delay(2000);
    }

    Libraries/IDE: Adafruit DHT sensor library v1.4.4, Adafruit Unified Sensor v1.1.9, Arduino IDE 2.3.2, ESP32 board package v3.0.0

    Serial monitor output:

    Temp: nan Hum: nan
    Temp: nan Hum: nan

    Expected vs actual: Expected valid temperature/humidity readings (e.g., 24.5°C, 45%). Instead getting nan on every read.

    Tests already performed:

    • Verified 3.3V present at DHT22 VCC pin with multimeter

    • Swapped DHT22 module — same result

    • Tried GPIO16 and GPIO17 instead of GPIO4 — same result

    • Increased delay to 2000ms per datasheet minimum sampling rate
      ESP32 MicroPython DHT22 Temperature Humidity Sensor | ESP32 MicroPython ...

Postado

Hello @ryuiijb.

You brought a really well-structured post, sharing your exact board model, libraries, code snippet, and the specific troubleshooting steps you already performed. That level of detail helps a lot on the workbench.

Getting NaN readings from a DHT22 on an ESP32 is a classic challenge, and since you already verified the 3.3V supply, tried another sensor, and tested different GPIOs, let's look at a few additional factors that frequently trigger this behavior on the ESP32 platform:

Next steps and suggestions to check

  • Pull-up resistor: The DHT22 data line requires a pull-up resistor (typically 4.7kΩ to 10kΩ) connected between the DATA pin and 3.3V. If you are using a bare sensor instead of a mounted module that already includes the onboard resistor, the ESP32 GPIO might fail to read the timing pulses correctly.
  • ESP32 core version compatibility: You mentioned using ESP32 board package v3.0.0. Major core updates (like v3.x) sometimes introduce timing or interrupt handling changes that affect bit-bang protocols like the one used by the DHT series. If possible, testing with board package v2.x or ensuring your Adafruit DHT library is updated to the absolute latest version can rule out package timing discrepancies.
  • Power source stability: Even though USB power is generally sufficient, try powering the DHT22 from an external 3.3V source or adding a 100nF decoupling capacitor close to the sensor VCC and GND pins to filter out any micro-transients on the rail.

Feel free to share if your DHT22 is a bare 4-pin component or a 3-pin module with an integrated resistor. Let us know how it goes with these checks!

Prefixo ajustado: atualizei o prefixo deste tópico para em análise. Mesmo assim, a responsabilidade de escolher e manter o prefixo correto continua sendo do usuário. Veja o guia: Prefixos - Saiba aqui como utilizar.

Participe agora da conversa!

Você pode postar agora e se cadastrar mais tarde. Se você tiver uma conta, faça login para postar com sua conta.

Visitante
Responder

Account

Navigation

Pesquisar

Pesquisar

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.