Build a Smart Temperature & Humidity Monitor using ESP32 + DHT11 + OLED Display


An Introductory Look at IoT and Embedded Systems

Introduction:

Have you ever wondered how your smart air conditioner or weather station determines the ambient temperature and humidity?
In this do-it-yourself project, I used an OLED display, a DHT11 sensor, and an ESP32 microcontroller to create a real-time temperature and humidity monitor. After gathering information from the DHT11 sensor, the ESP32 shows it on the OLED panel.
The outcome? A handheld digital weather station that is small and portable!

The project's goal is to use the ESP32 to interface with the DHT11 sensor and read humidity and temperature.
Use I2C connection to show real-time readings on an OLED display.
Recognize the interplay between sensors, displays, and microcontrollers in an Internet of Things system.

What is the Sensor DHT11?

A cheap digital sensor called the DHT11 is capable of measuring:
Temperature range: 0 to 50°C (accuracy of ±2°C)
Humidity: 20% to 90% relative humidity (accuracy ±5%)

Its single-wire digital interface facilitates microcontroller connectivity.
The DHT11's internal temperature and humidity sensors are a thermistor and a capacitive humidity sensor, respectively. The internal chip creates a digital signal from the analog values.

Why Opt for the ESP32 Over the Arduino Uno?

A potent microcontroller, the ESP32 has:
1) Bluetooth and Wi-Fi built in
2) Faster dual-core processor
3) Additional GPIO pins
4) Reduced power use

This makes it perfect for Internet of Things projects like remote monitoring systems, home automation, and smart weather stations.

Comprehending OLED Screen (SSD1306)

Organic Light Emitting Diode is referred to as OLED. The screen that we utilized is:

1) 128 × 64 pixels
2) The SSD1306 driver is used.
3) uses the I2C protocol to communicate (two wires: SDA, SCL)

Advantages of OLED:

1) Minimal power consumption
2) High contrast
3) No need for a backlight
4) Small dimensions (ideal for embedded projects)

Hardware Required:

Component Quantity Description
ESP32 Dev Board 1 Main microcontroller
DHT11 Sensor 1 Temp & humidity sensor
OLED Display (128x64) 1 For visual output
Breadboard 1 For prototyping
Jumper Wires ~6 To connect components

 Circuit Diagram:


Component Pin Connects to ESP32 Pin
DHT11 VCC 3.3V
DHT11 GND GND
DHT11 Data GPIO 15
OLED VCC 3.3V
OLED GND GND
OLED SDA GPIO 21
OLED SCL GPIO 22

Arduino Code:

#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define DHTPIN 15       
#define DHTTYPE DHT11   

DHT dht(DHTPIN, DHTTYPE);

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(115200);
  dht.begin();

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED not found");
    while (true);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

  if (isnan(temp) || isnan(hum)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("DHT11 Sensor Readings");
  display.print("Temp: ");
  display.print(temp);
  display.println(" C");
  display.print("Humidity: ");
  display.print(hum);
  display.println(" %");
  display.display();

  delay(2000);
}

Libraries Required for the above code:

1) DHT Sensor library 
2) Adafruit Unified Sensor library
3) Adafruit GFX and Adafruit SSD1306

Typical Mistakes and Their Solutions:

Here are some mistakes which I made during the performance of the project.
 1) "Failed to connect to ESP32: Wrong boot mode!"
Solution: Hold down the BOOT button while the code is being uploaded.

2) "OLED not found" 
Solution: Verify the I2C address and wiring (by default, it is 0x3C).

3) "DHT.h not found" 
Solution: Using Library Manager, install the appropriate DHT sensor library.

Expected Output:

The following is the expected output one should get ;
DHT11 Sensor Readings
Temp: 30.00 °C
Humidity: 79.40 %


According to the above code used live data updates will be shown in every 2 seconds.

Use cases and applications:

1) Weather monitoring at home
2) Climate control and greenhouse
3) Monitoring of the environment with IoT
4) Automation of smart fans or air conditioners
5) Learning assignment for students pursuing a B.E. or B.Tech.

What you get to learn:

1) The operation of sensors and the handling of analog data
2) I2C communication between an OLED and an ESP32
3) The fundamentals of serial debugging and embedded programming
4) How to combine several modules into a single project

Additional Enhancements:

1) This project can be improved by:
2) Data transmission to Google Sheets or Blynk
3) To log data with timestamps, add an RTC.
4) Using a Li-ion battery to make it portable
5) Including Wi-Fi to enable online publishing 

In conclusion:

This project serves as an excellent introduction to embedded electronics and the Internet of Things. We've developed a straightforward setup that uses the ESP32, a DHT11 sensor, and an OLED display to read and show the temperature and humidity in the real environment. It is an excellent place for beginners to start because it covers fundamental skills like sensor interfacing and data visualization. Anyone can achieve this if I can with my meager experience; all you need is curiosity and the will to keep learning and developing.

Comments