Index

Introduction
In this project, we will interface an OLED display with an ESP32 and use it to show real-time data from a DHT11 temperature and humidity sensor. This project is useful for IoT applications like weather monitoring.
Required Components
- ESP32 Board
- OLED Display (SSD1306, 128×64)
- DHT11 Temperature & Humidity Sensor
- Breadboard
- Jumper Wires
Pinout
- DHT11 Sensor
- GND: Ground pin
- VCC: Power supply pin (3.3V to 5V)
- SIGNAL: Digital data output pin

- OLED Display
- GND: Ground pin
- VCC: Power supply pin (3.3V to 5V)
- SCL: Serial Clock pin for I2C communication
- SDA: Serial Data pin for I2C communication

Circuit Diagram
- OLED Display (SSD1306, I2C)
- VCC → ESP32 3.3V
- GND → ESP32 GND
- SDA → ESP32 GPIO 21
- SCL → ESP32 GPIO 22
- DHT11 Sensor
- VCC → ESP32 3.3V
- GND → ESP32 GND
- Data Pin → ESP32 GPIO 4

Arduino Code
Required Libraries:
Your code uses the following libraries:
- Adafruit SSD1306
- Adafruit GFX Library
- Adafruit Unified Sensor
- DHT sensor library
Installation Steps in Arduino IDE:
Open Arduino IDE on your computer.
- Go to the “Libraries” tab on the left side (or click
Tools > Manage Libraries
). - Click the “Library Manager” button (book icon).
- In the Library Manager window, type the name of the library in the search bar.
- Find the correct library (
Adafruit SSD1306
,Adafruit GFX Library
,Adafruit Unified Sensor, DHT sensor library
.). - Click on the “Install” button next to it.
- Wait for the installation to complete — and you’re ready to use the library in your code!
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define DHTPIN 4 // Digital pin connected to the DHT sensor
// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
delay(5000);
//read temperature and humidity
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
// clear display
display.clearDisplay();
// display temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(t);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(h);
display.print(" %");
display.display();
}
Explanation
- Reads temperature and humidity from a DHT11 sensor connected to pin 4.
- Displays the values on a 128×64 I2C OLED screen using Adafruit SSD1306 library.
- Prints error to Serial Monitor if sensor fails to respond, and updates display every 5 seconds.
Troubleshooting
- Check wiring of DHT11 and OLED display (VCC, GND, Data/SCL/SDA).
- Ensure all required libraries are installed: Adafruit SSD1306, GFX, DHT, Unified Sensor.
- Use correct I2C address (usually
0x3C
) and DHT data pin in code.