ESP32 DHT22 OLED is the classic first sensor project: the ESP32 reads temperature and humidity from a DHT22 and shows the values on a small OLED screen. In this post you build the whole project in Proteus and run your real sketch on the simulated ESP32 DevKitC, with no hardware on your desk.
In short: you use the ESP32 DevKitC model, a DHT22 sensor on IO4 and a 128×64 SSD1306 I2C OLED on IO21 (SDA) and IO22 (SCL). The Adafruit libraries read the sensor and draw the values on the screen, with a thermometer icon for the temperature and a water-drop icon for the humidity.
This is a new post in the ESP32 Proteus series. If you have not installed the model yet, start with the ESP32 Library for Proteus post. Here you learn how the DHT22 and the OLED talk to the ESP32, build the ESP32 DHT22 OLED circuit, and show the temperature and humidity on the screen with icons.
What Is the DHT22 Sensor?
The DHT22 (also sold as AM2302) is a digital sensor that measures temperature and relative humidity. It sends both values to the microcontroller over a single data wire. The DHT11 is the cheaper version of the same idea, but the DHT22 is more accurate and has a wider range, so it is the better choice for this project.
| DHT11 | DHT22 | |
|---|---|---|
| Temperature range | 0 to 50 °C | -40 to 80 °C |
| Temperature accuracy | ±2 °C | ±0.5 °C |
| Humidity range | 20 to 80 % | 0 to 100 % |
| Humidity accuracy | ±5 % | ±2 to 5 % |
| Sampling rate | 1 reading per second | 1 reading every 2 seconds |
| Supply voltage | 3.3 to 5.5 V | 3.3 to 6 V |
How the ESP32 DHT22 OLED Project Works
The project uses two different connections:
- DHT22: one data wire on IO4. The ESP32 sends a start signal, and the sensor answers with 40 bits that hold the temperature, the humidity and a checksum.
- OLED: an I2C connection with two wires. SDA is IO21 and SCL is IO22, which are the default I2C pins of the ESP32 DevKitC. The display address is usually
0x3C.
The sketch does the same steps again and again:
- Read the humidity and the temperature from the DHT22.
- Check the values. If a value is
NaN(not a number), the reading failed. - Draw the values on the OLED.
- Use the serial port only for the start-up message.
- Wait 2 seconds and repeat.
In Proteus, the ESP32 model runs your real firmware, and the DHT22 SENSOR and the OLED I2C (128X64) come from our free DHT22 and OLED libraries. You change the sensor values while the simulation runs and watch the screen follow.
What You Need
| Requirement | Notes |
|---|---|
| Windows 10 or 11, 64-bit | The engine is a 64-bit program. |
| Proteus 8 Professional | The ESP32 model is built for Proteus 8 only. |
| ESP32 DevKitC library and engine | Download them from the ESP32 Library for Proteus post. |
| OLED I2C (128X64) part | Free in the OLED Library for Proteus (SSD1306, 0.96 inch). |
| DHT22 SENSOR part | Free in the DHT11 Library for Proteus + DHT22. |
| Arduino IDE 2 | arduino.cc |
| esp32 by Espressif, version 3.x | Install it from the Boards Manager. The source is on GitHub. |
If you are new to adding parts, read how to install a library in Proteus 8 first.
Install the OLED and DHT22 Libraries in Proteus
This project uses two free Electronics Tree libraries. Download both from their pages and install them:
| Library | Files | Part in Pick Devices |
|---|---|---|
| OLED Library for Proteus | ETOLED.DLL and ElectronicsTree[OLED].LIB | OLED I2C (128X64) |
| DHT11 Library for Proteus + DHT22 | DHTSENSOR.DLL and ElectronicsTree[DHTSENSORS].LIB | DHT22 SENSOR |
- Close Proteus.
- Unzip each download. Both zip files use the password
electronicstree.com. - Copy each
.DLLfile intoDATA\MODELSand each.LIBfile intoDATA\LIBRARY. The Proteus data folder is normallyC:\Program Files (x86)\Labcenter Electronics\Proteus 8 Professional\DATA. Click Continue when Windows asks for administrator rights. - Start Proteus, press P to open Pick Devices, and search for
oled i2canddht. You find OLED I2C (128X64) and DHT22 SENSOR.
Install the Arduino Libraries
The sketch uses three libraries. Open Sketch → Include Library → Manage Libraries and install:
| Library | Author | Used for |
|---|---|---|
| DHT sensor library | Adafruit | Reading the DHT22 |
| Adafruit SSD1306 | Adafruit | Driving the OLED |
| Adafruit GFX Library | Adafruit | Drawing text and shapes |
When the Library Manager asks to install the missing dependencies (for example Adafruit Unified Sensor and Adafruit BusIO), click Install all. The source code of the two main libraries is on GitHub: DHT sensor library and Adafruit SSD1306.
Circuit for ESP32 DHT22 OLED
Start a new project (schematic only). Click Design → Configure Power Rails, select VCC/VDD and set the voltage to 3.3 V. Then place these parts:
| Part (Pick Devices name) | Connect |
|---|---|
| ESP32 DevKitC | The board |
| POWER terminal | To the 3V3 pin |
| GROUND terminal | To a GND pin |
| DHT22 SENSOR | VCC to 3.3 V, GND to ground, DATA to IO5 |
| OLED I2C (128X64) | VCC to 3.3 V, GND to ground, SDA to IO21, SCL to IO22 |
| VIRTUAL TERMINAL | Its RXD to TX0 |
Some things to know:
- No extra resistors. The OLED part has its own I2C pull-ups, and the DHT22 SENSOR has a built-in 10k pull-up (the Built-in 10k Pull-up property is on by default). If you switch that property off to simulate a bare sensor, add a 4.7k to 10k resistor from DATA to 3.3 V.
- Pin names. The OLED part has four pins: VCC, GND, SCL and SDA. SDA goes to IO21 and SCL goes to IO22.
- Virtual Terminal. Double-click it and set the baud rate to 115200, 8 data bits, no parity, 1 stop bit.
- Pins to avoid. SD0-SD3, CMD and CLK are the flash pins of the real board. Do not use them.
Sketch for ESP32 DHT22 OLED
Open the Arduino IDE, create a new sketch called ESP32_DHT22_OLED and paste this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
// ESP32_DHT22_OLED - ESP32 DHT22 OLED temperature and humidity monitor (www.electronicstree.com)
// DHT22 DATA = IO4, OLED SDA = IO21, OLED SCL = IO22
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHT_PIN 5
#define DHT_TYPE DHT22
#define SDA_PIN 21
#define SCL_PIN 22
#define SCREEN_W 128
#define SCREEN_H 64
#define OLED_ADDR 0x3C
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_SSD1306 display(SCREEN_W, SCREEN_H, &Wire, -1);
// 16 x 16 icons, 1 bit per pixel (1 = lit pixel)
const unsigned char ICON_TEMP[] PROGMEM = { // thermometer
0x01, 0x80, 0x02, 0x40, 0x02, 0x58, 0x02, 0x40,
0x02, 0x58, 0x02, 0x40, 0x03, 0xD8, 0x03, 0xC0,
0x03, 0xC0, 0x07, 0xE0, 0x0F, 0xF0, 0x0F, 0xF0,
0x0F, 0xF0, 0x0F, 0xF0, 0x07, 0xE0, 0x03, 0xC0
};
const unsigned char ICON_HUM[] PROGMEM = { // water drop
0x01, 0x80, 0x02, 0x40, 0x02, 0x40, 0x04, 0x20,
0x04, 0x20, 0x08, 0x10, 0x08, 0x10, 0x10, 0x08,
0x10, 0x08, 0x14, 0x08, 0x14, 0x08, 0x10, 0x08,
0x08, 0x10, 0x04, 0x20, 0x03, 0xC0, 0x00, 0x00
};
void showValues(float t, float h) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1); // same small text size everywhere
display.setCursor(0, 0);
display.print("ESP32 + DHT22");
display.drawLine(0, 11, 127, 11, SSD1306_WHITE);
// temperature: icon + label + value
display.drawBitmap(0, 18, ICON_TEMP, 16, 16, SSD1306_WHITE);
display.setCursor(24, 18);
display.print("Temperature");
display.setCursor(24, 27);
display.print(t, 1);
display.write(247); // degree symbol
display.print("C");
// humidity: icon + label + value
display.drawBitmap(0, 40, ICON_HUM, 16, 16, SSD1306_WHITE);
display.setCursor(24, 40);
display.print("Humidity");
display.setCursor(24, 49);
display.print(h, 1);
display.print(" %");
display.display();
}
void showError() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("ESP32 + DHT22");
display.drawLine(0, 11, 127, 11, SSD1306_WHITE);
display.setCursor(0, 28);
display.print("Sensor error");
display.display();
}
void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN);
dht.begin();
delay(2000); // the DHT22 needs about 1 s after power on
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println("SSD1306 not found. Check SDA, SCL and the address.");
while (true) delay(1000);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Starting...");
display.display();
Serial.println();
Serial.println("ESP32 DHT22 OLED started");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // degrees Celsius
if (isnan(h) || isnan(t)) {
Serial.println("DHT22 read failed");
showError();
} else {
showValues(t, h);
}
delay(2000); // the DHT22 gives a new value every 2 seconds
}How it works:
Wire.begin(SDA_PIN, SCL_PIN)starts the I2C bus on IO21 and IO22.display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)starts the OLED. If it fails, the sketch prints a message and stops, which tells you the wiring or the address is wrong.dht.readTemperature()returns degrees Celsius anddht.readHumidity()returns percent. Both returnNaNif the reading fails.showValues()clears the screen, writes a small title, and then draws an icon next to each value: a thermometer for the temperature and a droplet for the humidity. Nothing appears untildisplay.display()is called.display.write(247)draws the degree symbol. The two 16 x 16 icons come from the arrays ICON_TEMP and ICON_HUM at the top of the sketch. Each byte holds 8 pixels, and display.drawBitmap() draws them at the position you give. All text uses size 1, the same as the title, and display.drawLine() draws the thin line under the title.delay(2000)matches the sampling rate of the DHT22 and also lets the CPU rest, which keeps the simulation fast.
On a real ESP32 you change nothing. Wire the same parts, flash the same sketch, and the screen works the same way.
Export the ESP32 Firmware
The export steps are the same as in the previous posts:
- Tools → Board → esp32 → ESP32 Dev Module.
- Tools → Flash Mode → DIO. Other flash modes do not start in the simulation.
- Sketch → Export Compiled Binary (or Ctrl+Alt+S).
Double-click the ESP32 part the file ESP32_DHT22_OLED.ino.merged.bin from the build folder.
Run the ESP32 DHT22 OLED Simulation
Press Play. The first seconds are slow, because the engine starts and the ESP32 boots like a real chip. When the sketch runs, the Virtual Terminal shows:
ESP32 DHT22 OLED startedThe OLED shows the title ESP32 + DHT22, then a thermometer icon with the temperature and a droplet icon with the humidity. The values on the OLED update every 2 seconds.
What you should see:
- The Virtual Terminal shows only the start-up message. All readings appear on the OLED.
- A change on the DHT22 appears one reading later, like on the real sensor.
- If you disconnect the DATA wire, the screen shows
Sensor errorand the Virtual Terminal printsDHT22 read failed.
If the OLED stays black, read the troubleshooting table at the end of the post. The most common reason is a missing I2C pull-up resistor.
If the Proteus log says the DHT22 answers only 1 s after power on, the sketch read the sensor too early. Keep delay(2000) after dht.begin() in setup(), as in the code above. A real DHT22 also needs about one second after power on before its first reading.
Use the Same Sketch on a Real ESP32
The sketch in this post is not special for Proteus. To build the project on a real board:
- Use a DHT22 module or a bare DHT22 with a 10k pull-up resistor on the data pin.
- Connect a 0.96 inch SSD1306 I2C OLED to IO21 (SDA) and IO22 (SCL). Most OLED modules already have pull-up resistors.
- Power both parts from the 3V3 pin.
- Flash the same sketch with the ESP32 Dev Module board.
If the OLED stays black on a real board, run an I2C scanner sketch. Some modules use 0x3D instead of 0x3C.
Tips for a Fast ESP32 DHT22 OLED Simulation
- Keep the 2-second rule. Reading the DHT22 faster than every 2 seconds gives repeated values or errors.
- Keep delay() in your loops. A loop that never waits makes the emulator run every instruction, and the simulation becomes slow.
- Use the same pins as the post. IO21 and IO22 are digital pins in the model and simulate fastest.
- Slow start. The first seconds after Play are always slower. The speed is normal again when your sketch runs.
- Time step. If the simulation is still slow, set Time step (us) to 200 or 300. The OLED and the DHT22 do not need exact timing at that level.
- Redraw only when needed. Drawing the whole OLED screen takes time. Update it only when a value or the unit changes.
Frequently asked questions
Can Proteus simulate an ESP32 DHT22 OLED project?
Yes. With the Electronics Tree ESP32 model, your real sketch runs on a real ESP32 processor emulator. The DHT22 SENSOR and OLED I2C (128X64) parts come from our free Proteus libraries, so the screen shows the values you set on the sensor.
Which ESP32 pin should I use for the DHT22?
Any digital pin works. This post uses IO5. Avoid the flash pins SD0-SD3, CMD and CLK, and pins 34 to 39, which are input-only and have no internal pull-up.
Why does the DHT22 need 2 seconds between readings?
The sensor measures slowly. Its datasheet gives a sampling rate of 0.5 Hz, which is one reading every 2 seconds. A faster read returns the previous value or an error.
Can I use a DHT11 instead of the DHT22?
Yes. Change #define DHT_TYPE DHT22 to DHT11 and place the DHT11 SENSOR part, which is in the same library zip as the DHT22. The DHT11 is less accurate and measures only 0 to 50 °C.
Will the same sketch work on a real ESP32?
Yes. Connect the same parts, flash the sketch with the ESP32 Dev Module board, and the display works the same way.
Can I use the U8g2 library instead of Adafruit SSD1306?
Yes. The Electronics Tree OLED library for Proteus works with both Adafruit SSD1306 and U8g2, the same as on a real module.
Can I use my own icons on the OLED?
Yes. Draw a 16 x 16 black and white image, convert it to a byte array with a free tool such as image2cpp, and draw it with display.drawBitmap(). Keep icons small, because large bitmaps use more flash memory and take longer to draw.
Is the ESP32 library for Proteus free?
Yes. The model is a free download from the ESP32 Library for Proteus page.