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 Arduino Mega 2560 — temperature, humidity and daily min/max

Wire a DHT22 to an Arduino Mega 2560 on pin 22 at 5 V and print temperature and humidity with running minimum and maximum values you can reset.

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

Community draft — report a mistake

The DHT22 is a common first sensor in Mega-based weather stations and greenhouse controllers. It needs one digital pin — here pin 22 on the double-row header, so the busier pins stay free for displays and modules.

Besides the live reading, the sketch keeps the minimum and maximum since start-up — useful for checking how hot a terrace room or a cold store gets overnight. Send r to reset them.

Wiring

Arduino Mega 2560Only the pins used are shown · 5 V logicDHT22 (AM2302)10k → 5V5VGNDD221 VCC2 DATA3 NC (not connected)4 GND
Also needed: 10 kΩ resistor (bare sensor only).
Every connection
Arduino Mega 2560DHT22 (AM2302)WireNotes
5V1 VCCred
D222 DATAyellow
5V2 DATAredVia 10 kΩ — Pull-up for the bare 4-pin sensor; 3-pin modules already have it.
GND4 GNDblack

3.3–6 V part, 5 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

  • Pin 22 is on the double-row header. Using it leaves PWM pins 2–13, the serial ports and I2C (20/21) free for the rest of a bigger Mega project.
  • The Mega is 5 V like the sensor’s data line, so no level shifting.
  • Several DHT22s can each have their own pin: DHT dht2(23, DHT22); — the Mega has plenty.
  • The DHT library briefly disables interrupts while reading. With a long WS2812 strip or fast serial traffic, read the DHT22 every few seconds only.

Code

dht22-mega.inoArduino C++Open in PlaygroundOpen in Playground
// DHT22 on an Arduino Mega 2560, keeping min/max for the day in RAM.
// 1 VCC → 5V, 2 DATA → D22 (10 kΩ pull-up to 5V on the bare sensor), 4 GND → GND.
#include <DHT.h>

const uint8_t DHT_PIN = 22;  // double-row header: keeps PWM, serial and I2C pins free
DHT dht(DHT_PIN, DHT22);
float tMin = 1000, tMax = -1000, hMin = 1000, hMax = -1000;

void setup() {
  Serial.begin(115200);
  dht.begin();
  Serial.println(F("DHT22 on D22 - send r to reset min/max"));
}

void loop() {
  delay(2000);
  if (Serial.available() && Serial.read() == 'r') {
    tMin = hMin = 1000;
    tMax = hMax = -1000;
    Serial.println(F("Min/max reset"));
  }
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT22"));
    return;
  }
  tMin = min(tMin, t);
  tMax = max(tMax, t);
  hMin = min(hMin, h);
  hMax = max(hMax, h);
  Serial.print(F("Temp: "));
  Serial.print(t, 1);
  Serial.print(F(" C (min "));
  Serial.print(tMin, 1);
  Serial.print(F(" / max "));
  Serial.print(tMax, 1);
  Serial.print(F(")  Humidity: "));
  Serial.print(h, 1);
  Serial.print(F(" % (min "));
  Serial.print(hMin, 1);
  Serial.print(F(" / max "));
  Serial.print(hMax, 1);
  Serial.println(F(")"));
}

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 D22 - send r to reset min/max
Temp: 31.2 C (min 31.2 / max 31.2)  Humidity: 58.4 % (min 58.4 / max 58.4)
Temp: 31.0 C (min 31.0 / max 31.2)  Humidity: 59.1 % (min 58.4 / max 59.1)

Common errors

Failed to read from DHT22

Missing pull-up on a bare sensor, or reading faster than every 2 s. Full fix.

avrdude: stk500v2_ReceiveMessage(): timeout

Wrong board or port, or the CH340 driver missing on a clone Mega. Full fix.

↑↓ to move · Enter to open