ESP32 C3 Super Mini - DHT22 - OLED

This tutorial shows you how to use the ESP32 C3 Super Mini to read the temperature and humidity from a DHT22 module and display them on an OLED. You will build a small climate monitor that works without a computer once the code is uploaded.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - DHT22 - OLED

Hardware Preparation

1×ESP32 C3 Super Mini
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×DHT22 Temperature Humidity Sensor Module
1×Alternatively, DHT22 Sensor
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of OLED Display and DHT22 Temperature Humidity Sensor

A DHT22 is a digital sensor that measures temperature and humidity, and an OLED is a small screen that shows the result.

Key Features:

  • One data wire: The DHT22 module needs only one GPIO pin
  • Two readings: You get temperature and humidity from one sensor
  • Easy to read: The OLED shows clear, high-contrast text
  • Simple wiring: The OLED uses only two I2C signal wires

Technical Specifications:

  • DHT22 voltage: 3V to 5V
  • Humidity range: 0% to 100%, accuracy ±2% to 5%
  • Temperature range: -40°C to 80°C, accuracy ±0.5°C
  • Reading rate: 0.5Hz (one new reading every 2 seconds)
  • OLED driver: SSD1306
  • OLED size: 128x64 or 128x32 pixels
  • OLED I2C address: 0x3C (used in the code below)

We have specific tutorials about each part. They cover the hardware pinout, how it works and how to program it:

DHT22 Module Pinout

The DHT22 module has three pins:

  • VCC (or +): Connect to 3.3V
  • GND (or -): Connect to ground (0V)
  • DATA (or OUT): Sends the readings to the board
DHT22 sensor module Pinout

OLED Pinout

The I2C OLED module has four pins:

  • VCC: Power for the display
  • GND: Connect to ground (0V)
  • SCL: I2C clock line
  • SDA: I2C data line

Wiring Diagram

Connect the DHT22 module and the OLED to the ESP32 C3 Super Mini as shown below.

  • Note: The DHT22 module has a built-in pull-up resistor, so you do not need a separate one.
  • Note: The built-in LED also uses GPIO8, so it may flicker while the OLED updates. This is normal.
  • Note: Check every wire before you power the board.
The wiring diagram between ESP32 C3 Super Mini DHT22 module OLED

This image is created using Fritzing. Click to enlarge image

DHT22 module connections:

DHT22 Module Pin ESP32 C3 Super Mini Pin
VCC 3.3V
GND GND
DATA GPIO4

OLED connections:

OLED Pin ESP32 C3 Super Mini Pin
VCC 3.3V
GND GND
SDA GPIO8
SCL GPIO9

ESP32 C3 Super Mini Code - DHT22 Sensor - OLED

The code below reads the DHT22 sensor again and again and shows the temperature and humidity on the OLED.

What This Code Does:

  • Starts the OLED: Uses the default I2C pins of the board: GPIO8 (SDA) and GPIO9 (SCL)
  • Starts the sensor: Uses the DHT22 data line on GPIO4
  • Reads the values: Gets the humidity and the temperature in Celsius
  • Builds the text: Keeps one decimal place and adds the degree symbol and "C", or the "%" sign
  • Shows the result: Prints both values on the OLED and the Serial Monitor, or "Failed" on the OLED if the reading fails
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-dht22-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define DHT22_PIN 4 // The ESP32 C3 SuperMini pin connected to DHT22 sensor Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht22(DHT22_PIN, DHT22); String temperature; String humidity; void setup() { Serial.begin(115200); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(3); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht22.begin(); // initialize DHT22 the temperature and humidity sensor temperature.reserve(10); // to avoid fragmenting memory when using String humidity.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht22.readHumidity(); // read humidity float tempC = dht22.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(tempC)) { temperature = "Failed"; humidity = "Failed"; } else { temperature = String(tempC, 1); // one decimal places temperature += char (247); // degree character temperature += "C"; humidity = String(humi, 1); // one decimal places humidity += "%"; Serial.print(tempC); // print to Serial Monitor Serial.print("°C | " ); // print to Serial Monitor Serial.print(humi); // print to Serial Monitor Serial.println("%"); // print to Serial Monitor } oledDisplayCenter(temperature, humidity); // display temperature and humidity on OLED } void oledDisplayCenter(String temperature, String humidity) { int16_t x1; int16_t y1; uint16_t width_T; uint16_t height_T; uint16_t width_H; uint16_t height_H; oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T); oled.getTextBounds(humidity, 0, 0, &x1, &y1, &width_H, &height_H); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT/2 - height_T - 5); oled.println(temperature); // text to display oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT/2 + 5); oled.println(humidity); // text to display oled.display(); }

※ NOTE THAT:

The above code automatically horizontal and vertical center aligns the text on OLED display.

※ NOTE THAT:

The code is set for the 128x64 OLED. If you use the 128x32 OLED, change SCREEN_HEIGHT to 32 and change oled.setTextSize(3) to oled.setTextSize(1), so that both lines fit on the screen.

Detailed Instructions

Follow these bullets to run the project on your ESP32 C3 Super Mini.

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire the parts: Connect the DHT22 module and the OLED as shown in the wiring diagram above.
  • Connect the board: Connect the ESP32 C3 Super Mini to your computer with a USB Type-C cable.
  • Open Arduino IDE: Start Arduino IDE on your computer.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the port: Choose the COM port of your board.
  • Open Libraries: Click the Libraries icon on the left bar of the Arduino IDE.
  • Find the OLED library: Search “SSD1306”, then find the SSD1306 library by Adafruit.
  • Install it: Click the Install button to install the library.
ESP32 C3 Super Mini OLED library
  • Add the extras: You will be asked to install some other library dependencies. Click the Install All button to install them.
ESP32 C3 Super Mini Adafruit GFX sensor library
  • Find the sensor library: Search “DHT”, then find the DHT sensor library by Adafruit.
  • Install it: Click the Install button to install the library.
ESP32 C3 Super Mini DHT sensor library
  • Add the extras: You will be asked to install some other library dependencies. Click the Install All button to install them.
ESP32 C3 Super Mini Adafruit Unified sensor library
  • Copy the code: Copy the above code and open it with Arduino IDE.
  • Upload the code: Click the Upload button to upload the code to your ESP32 C3 Super Mini.
How to upload ESP32 C3 Super Mini code on Arduino IDE
  • Test the sensor: Hold the sensor in your hand or breathe gently on it. Keep it dry, because the DHT22 is not waterproof.
  • Read the OLED: See the result on the OLED.
  • Open Serial Monitor: Click the Serial Monitor icon and set the baud rate to 115200 to see the same result.
How to open serial monitor on Arduino IDE
  • Pro Tip: If the OLED shows "Failed", the board cannot read the sensor. Check the DATA wire on GPIO4 and the power wires.

Serial Monitor Output

Each reading prints one line with the temperature and the humidity. The same line can repeat many times, because the sensor gives a new reading only every 2 seconds and the code has no delay.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
25.40°C | 57.30% 25.50°C | 57.80% 26.20°C | 62.10% 27.10°C | 68.40%
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Application and Project Ideas

This ESP32 C3 Super Mini DHT22 and OLED project is a good base for many small builds.

  • Room climate monitor: Show the temperature and humidity on your desk.
  • Greenhouse display: Check the air near your plants at a glance.
  • Plant care helper: See when the air is too dry for your houseplants.
  • Storage checker: Watch the humidity in a cupboard or a tool box.
  • Server cabinet monitor: Keep an eye on the air inside a small cabinet.

Challenge Yourself

Ready for more? Try these ESP32 C3 Super Mini challenges.

  • Easy: Show the temperature in Fahrenheit instead of Celsius.
  • Easy: Add delay(2000) to the loop so the Serial Monitor prints one line every 2 seconds.
  • Medium: Keep the lowest and highest temperature and show them on the OLED.
  • Medium: Add a buzzer that beeps when the humidity gets too high.
  • Advanced: Draw a small graph of the last humidity readings on the OLED.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!