Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

DHT22 with NodeMCU ESP8266 — wiring and code

Wire a DHT22 to a NodeMCU ESP8266 on D2 (GPIO4) at 3.3 V with its pull-up and read temperature and humidity in Arduino C++.

Arduino C++Verified — core esp8266 3.1.2, DHT sensor library 1.4.7, 3 Nov 2026Compile-only

Community draft — report a mistake

The NodeMCU is still the cheapest Wi-Fi board in many Indian shops, and a DHT22 on it makes a classic room monitor.

The trap is the pin naming: D2 on the board is GPIO4. Use D1/D2 for sensors and keep D3, D4 and D8 free, because they decide how the ESP8266 boots.

Wiring

NodeMCU ESP8266Only the pins used are shown · 3.3 V logicDHT22 (AM2302)10k → 3V3GND3V3D21 VCC2 DATA3 NC (not connected)4 GND
Every connection
NodeMCU ESP8266DHT22 (AM2302)WireNotes
3V31 VCCred
D22 DATAyellow
3V32 DATAredVia 10 kΩ — Pull-up for the bare 4-pin sensor. 3-pin breakouts already have it — skip this wire.
GND4 GNDblack

3.3–6 V part, 3.3 V board. Works at 3.3 V and 5 V. Power it from the same voltage as your board (3.3 V on ESP32 and Pico, 5 V on Uno), so the data line never exceeds the GPIO limit.

Board quirks

  • NodeMCU labels (D0–D8) are not GPIO numbers: D2 is GPIO4. The core defines the D-names, so the sketch uses D2 directly.
  • D3, D4 and D8 are boot pins on the ESP8266 — a sensor that pulls them the wrong way at reset stops the board booting. D1 and D2 are the safe ones.
  • The ESP8266 is 3.3 V-only, so the DHT22 runs from 3V3.

Code

dht22-nodemcu.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// DHT22 / AM2302 on a NodeMCU (ESP8266).
// VCC → 3V3, DATA → D2 (GPIO4, 10 kΩ pull-up to 3V3 for the bare sensor), GND → GND.
#include <DHT.h>

const uint8_t DHT_PIN = D2;  // GPIO4 — free at boot, unlike D3/D4/D8
DHT dht(DHT_PIN, DHT22);

void setup() {
  Serial.begin(115200);
  dht.begin();
  Serial.println();
  Serial.println("DHT22 on D2 (GPIO4) - first reading in 2 s");
}

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor - check wiring and pull-up");
    return;
  }
  Serial.printf("Temp %.1f C  Humidity %.1f %%\n", t, h);
}

Library: DHT sensor library (Adafruit (also install Adafruit Unified Sensor)) — compiled with DHT sensor library 1.4.7.

Expected serial output

Serial Monitor · 115200Output
DHT22 on D2 (GPIO4) - first reading in 2 s
Temp 28.4 C  Humidity 60.7 %
Temp 28.4 C  Humidity 60.6 %

Common errors

Garbage characters in the Serial Monitor at start-up

That is the ESP8266 boot log at 74880 baud. It is harmless; the sketch output at 115200 follows.

“Failed to read from DHT sensor”

Check the pull-up and that DATA is on D2. Full fix.

↑↓ to move · Enter to open