The objective of this tutorial is to design and implement a system capable of measuring and displaying real-time temperature and humidity using the DHT11 sensor and the ESP8266 NodeMCU microcontroller, with the results shown on an LCD I2C screen.
This tutorial aims to teach how to:
- interface the DHT11 sensor with the ESP8266 NodeMCU to acquire temperature and humidity data through a digital signal.
- connect and configure an I2C LCD display to the ESP8266 NodeMCU to visualize the measured values in real time.
- write and upload a MicroPython program that reads data from the DHT11 sensor and sends the results to the LCD screen for display.
- understand the operation of the DHT11 sensor, including its method of measuring temperature and humidity and its communication protocol.
Develop a compact and efficient environmental monitoring system, which can serve as the foundation for more advanced IoT applications such as smart weather stations or home automation systems.
1. ESP8266 NodeMCU
The ESP8266 NodeMCU acts as the main controller that reads the temperature and humidity data from the DHT11 sensor and sends the results to the LCD screen.
2. DHT11 Temperature and Humidity Sensor
The DHT11 sensor is a digital sensor that measures both temperature and humidity.
An internal microcontroller that processes and transmits the data in digital form.
The DHT11 provides an accuracy of about ±2°C for temperature and ±5% for humidity.
It communicates with the ESP8266 via a single digital pin.
3. LCD I2C 20x4 Display
The 20x4 LCD display can show two lines of 20 characters each, allowing you to visualize temperature and humidity readings clearly.
In this project, it is connected to the ESP8266 through an I2C module.
This display is used to show real-time temperature and humidity values measured by the DHT11 sensor.
4. Jumper Wires
Jumper wires are used to make electrical connections between the ESP8266 NodeMCU, DHT11 sensor, and LCD I2C display on a breadboard.
5. Breadboard
A breadboard is used to assemble the circuit without soldering.
It serves as the platform for connecting the ESP8266 NodeMCU, DHT11 sensor, and I2C LCD display.
Attaching the I2C LCD Display :
- connect the VCC pin of the display to VIN pin of the ESP8266
- connect the GND pin of the display to GND pin of the ESP8266
- connect the SDA pin of the display to D1 (GPIO5) pin of the ESP8266
- connect the SCL pin of the display to D2 (GPIO4) pin of the ESP8266
Mounting the DHT11 Sensor:
- connect the VCC(+) pin of the DHT11 Sensor to 3V pin of the ESP8266
- connect the GND(-) pin of the DHT11 to GND pin of the ESP8266
- connect the DATA pin of the DHT11 to D0 (GPIO16) of the ESP8266
Here is a complete MicroPython program to read temperature and humidity using the DHT11 sensor connected to the ESP8266 NodeMCU board:
Ensure you have the necessary libraries installed: dht, 'i2c_lcd' and 'lcd_api'.
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 |
import machine from machine import Pin, SoftI2C from time import sleep from lcd_api import LcdApi from i2c_lcd import I2cLcd import dht I2C_ADDR = 0x27 totalRows = 4 totalColumns = 20 # === Initialisation LCD I2C === i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266 lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) #lcd2 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) p16=Pin(16, Pin.IN) # Configuration du capteur DHT11 d=dht.DHT22(p16) lcd.clear() while True: d.measure() t=d.temperature() #read temperature h=d.humidity() #read humidity lcd.clear() lcd.move_to(0,0) lcd.putstr("temperature:"+str(int(t))+"C") lcd.move_to(0,1) lcd.putstr("humdidity:"+str(int(h))+"%") sleep(4) |
1- Imports modules:
machine for GPIO and I2C control
time for delay handling
dht for the DHT11 sensor library
lcd_api and i2c_lcd for controlling the LCD screen
2- Initializes the I2C LCD:
Defines SDA and SCL pins, sets the LCD address, and prepares the display.
3- Sets up the DHT11 sensor:
The sensor is connected to D0 (GPIO16) (you can change this if needed).
4- Main loop:
Reads temperature and humidity values from the DHT11.
Displays the results both on the serial monitor and LCD I2C screen.
Updates the readings every 4 seconds.
La robotique éducative joue un rôle important dans l'éducation des enfants et des jeunes en les aidant à acquérir des compétences en science et technologie.
Dans ce cadre notre site web représente une excellente ressource pour les parents, les enseignants et les enfants qui souhaitent découvrir la robotique.
Zaouiet Kontech-Jemmel-Monastir-Tunisie
+216 92 886 231
medaliprof@gmail.com
Site robotique réalisé par Mohamed Ali-Prof Info