Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Project 3: Digital Thermometer

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)
    • VCCESP32 3.3V
    • GNDESP32 GND
    • SDAESP32 GPIO 21
    • SCLESP32 GPIO 22
  • DHT11 Sensor
    • VCCESP32 3.3V
    • GNDESP32 GND
    • Data PinESP32 GPIO 4

Arduino Code

Required Libraries:

Your code uses the following libraries:

  1. Adafruit SSD1306
  2. Adafruit GFX Library
  3. Adafruit Unified Sensor
  4. 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.

    Leave a Reply

    Your email address will not be published.

    Need Help?