home / tutorials / DHT22 on OLED: Live Temperature & Humidity
Sensors · ESP32-C3
DHT22 on OLED: Live Temperature & Humidity
Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds
Overview & Wiring
Read a DHT22 (AM2302) and show live temperature and humidity on the OLED - no PC needed.
Wiring:
OLED -> VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5
DHT22 -> VCC=3V3, DATA=GPIO10, GND=GND
If your DHT22 has no built-in resistor, add a 10k pull-up between DATA and 3V3.
Libraries (Library Manager): "Adafruit SSD1306", "Adafruit GFX Library", "DHT sensor library" by Adafruit (also installs "Adafruit Unified Sensor").
Circuit Diagram
Parts You Need
Source Code (Arduino / ESP32)
Copy Download .ino
#include <Wire.h> // I2C for the OLED
#include <Adafruit_GFX.h> // graphics functions
#include <Adafruit_SSD1306.h> // OLED driver
#include <DHT.h> // Adafruit DHT sensor library
Adafruit_SSD1306 display(128, 64, &Wire, -1); // the OLED (I2C)
#define DHTPIN 10 // DATA wire is on GPIO10
#define DHTTYPE DHT22 // it's a DHT22 (not DHT11)
DHT dht(DHTPIN, DHTTYPE); // create the sensor object
void setup() {
Serial.begin(115200); // Serial for debugging
Wire.begin(4, 5); // OLED I2C on GPIO4/GPIO5
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // start OLED at 0x3C
dht.begin(); // start the DHT22
}
void loop() {
float t = dht.readTemperature(); // temperature in Celsius
float h = dht.readHumidity(); // humidity in %
display.clearDisplay(); // erase previous frame
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1); // small label font
display.setCursor(0, 0);
display.println("DHT22 Sensor");
if (isnan(t) || isnan(h)) { // NaN = read failed
display.setCursor(0, 26);
display.println("Read failed...");
Serial.println("Sensor read failed");
} else {
display.setTextSize(2); // big value font
display.setCursor(0, 18); display.printf("%.1f C", t); // temp, 1 decimal
display.setCursor(0, 42); display.printf("%.0f %%", h); // humidity, no decimals
Serial.printf("Temp: %.1f C Humidity: %.1f %%\n", t, h);
}
display.display(); // push to screen
delay(2000); // DHT22 needs ~2s between reads
}