Esp-01 with Oled and DHT11

In today’s world of IoT (Internet of Things), creating personalized weather monitoring systems has become increasingly popular. These systems provide real-time data on temperature and humidity, offering valuable insights for various applications, from agriculture to home automation. In this tutorial, we’ll explore how to build a simple weather monitoring system using an ESP8266 microcontroller (specifically, the ESP-01 model) and an OLED display. We’ll utilize the DHT11 sensor for temperature and humidity readings, and display the data on a small OLED screen.

Hardware Components

Logic Converter

A logic level converter is used with an ESP01 or other microcontrollers for voltage compatibility when interfacing with devices that use different voltage levels. It ensures proper communication, prevents damage to the microcontroller, and allows for reliable bidirectional communication, making it a crucial component in various electronics projects.

 

 

Step-down module

This module allows Esp01 to be powered with 3.3v (it is not tolerant to 5v). Another important feature is that it can supply up to 800mA, which is more than enough to power the Esp01.

 

 

It as 3 pins:

  • Vin: In our case 5V from external regulated power suplly
  • Out: It will supply 3.3v to the Esp01
  • Gnd: Ground pin

Software Requirements

  • Arduino IDE
  • Libraries: Wire.h (The Wire library is included by default in the Arduino IDE), U8g2lib.h, DHT.h

 

 

 

 

Setting Up the Hardware

  1. Connect the DHT11 sensor to the ESP8266 as follows:
    • DHT11 VCC to ESP8266 VCC (3.3V)
    • DHT11 GND to ESP8266 GND
    • DHT11 data pin to GPIO3 (RXD) of the ESP01
  2. Connect the SSD1306 OLED display to the ESP8266:
    • Connect SCL pin of OLED display to GPIO0
    • Connect SDA pin of OLED display to GPIO2
    • Connect OLED VCC to ESP01 VCC (3.3V)
    • Connect OLED GND to ESP01 GND

Follow the schematic

Important:

  • PINK LINES CARRY THE 3.3v
  • RED LINES CARRY THE 5V

 

 

Code

 

#include <Wire.h>
#include <U8g2lib.h>
#include <DHT.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define I2C_ADDRESS   0x3C  // Replace with the correct I2C address

#define DHTPIN 3          // GPIO3 for DHT11 data pin
#define DHTTYPE DHT11     // DHT 11

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 0, /* data=*/ 2); // GPIO0 for SCL, GPIO2 for SDA

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  Wire.begin(0, 2); // GPIO0 for SCL, GPIO2 for SDA
  u8g2.begin();
  u8g2.clearBuffer();
  
  u8g2.setFont(u8g2_font_ncenB14_tr);  // Set font->Big one
  u8g2.drawStr(0, 20, "Stand By...");  // Draw the string
  u8g2.sendBuffer();  // Send the buffer to the display
  delay(3000);
  u8g2.clearBuffer();
  
}

void loop() {
  // Read sensor data
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

 
  // Display sensor data on OLED
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr); // Set font size to 8
  u8g2.setCursor(0, 20);
  u8g2.print("Humidity: ");
  u8g2.print(humidity);
  u8g2.println(" %");
  u8g2.setCursor(0, 40);
  u8g2.print("Temperature: ");
  u8g2.print(temperature);
  u8g2.println(" *C");
  u8g2.sendBuffer();

  delay(3000); // Delay for 3 seconds
}

Programming the ESP01

The provided Arduino code initializes the necessary libraries, sets up the OLED display, and reads temperature and humidity data from the DHT11 sensor. Let’s break down the code:

  • #include statements: Include necessary libraries for I2C communication, OLED display, and DHT sensor.
  • Constants and Variables: Define pin configurations, screen dimensions, and sensor type.
  • setup() function: Initialize serial communication, DHT sensor, I2C communication, and OLED display. Display a standby message on the OLED screen for 3 seconds.
  • loop() function: Continuously read temperature and humidity data from the sensor, display it on the OLED screen, and delay for 3 seconds.

Connecting the USB to TTL Converter

 

To put the CH340G adapter in programming mode, follow these steps:

  • Before plugging the USB ESP-01 programming adapter into a USB port on your computer, Press and hold the push button switch that you soldered in Step 2 for a few seconds.
  • Plug the USB ESP-01 programming adapter into a USB port on your computer with the button pressed.
  • Release the button: After a few seconds, release the push button switch. The Esp-01 should be in programming mode, and you can upload code to the ESP-01 module using your preferred development environment.

 

If having trouble, go to my previous blog post. It explains with images: https://www.edgemicrotech.com/preparing-the-usb-esp-01-programming-adapter-a-step-by-step-guide/

 

Running the Code

  1. Connect the ESP8266 to your computer via USB.
  2. Open the Arduino IDE and load the provided code.
  3. Select the appropriate board (ESP8266) and port.
  4. Upload the code to the ESP8266.
  5. Once uploaded, you should see temperature and humidity readings displayed on the OLED screen.

 

 

Conclusion

In this tutorial, we’ve learned how to create a weather monitoring system using an ESP8266 microcontroller and OLED display. By interfacing with a DHT11 sensor, we can obtain real-time temperature and humidity data, which can be invaluable for various projects and applications. Feel free to expand upon this project by adding additional sensors or integrating it into a larger IoT network. Happy tinkering!