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 2560 | DHT22 (AM2302) | Wire | Notes |
|---|---|---|---|
| 5V | 1 VCC | red | |
| D22 | 2 DATA | yellow | |
| 5V | 2 DATA | red | Via 10 kΩ — Pull-up for the bare 4-pin sensor; 3-pin modules already have it. |
| GND | 4 GND | black |
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 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(")"));
}// 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
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)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)