ESP8266 with DHT11 Sensor and 2004 LCD via ESP-NOW
In this blog post, we will explore how to connect two ESP8266 modules, one with a DHT11 temperature and humidity sensor and the other with a 2004 LCD display, using the ESP-NOW protocol. ESP-NOW is a wireless communication protocol developed by Espressif that enables devices to communicate directly without the need for a Wi-Fi network. This makes it an ideal choice for IoT applications where low power consumption and fast communication are essential.
Requirements
1. ESP8266 development board (e.g., NodeMCU or Wemos D1 Mini) (Affiliate) – https://s.click.aliexpress.com/e/_DD3JQhj
2. DHT11 temperature and humidity sensor (Affiliate) – https://s.click.aliexpress.com/e/_DFb2JzR
3. 2004 LCD module (with I2C interface) – (Affiliate) – https://s.click.aliexpress.com/e/_DE4GaZb
4. Breadboard and jumper wires (Affiliate) – https://s.click.aliexpress.com/e/_Dl5kuk1
5. Arduino IDE with ESP8266 board support installed
Step 1: Wiring the Components
First, let’s wire the components to the ESP8266 boards:
DHT11 Sensor:
– VCC to 3.3V on ESP8266
– GND to GND on ESP8266
– Data to D2 on ESP8266
2004 LCD Display (External regulated power supply):
– VCC to 5V
– GND to GND
3. Connect the SDA pin of the 2004 LCD to the D2 pin of the Wemos D1 Mini.
4. Connect the SCL pin of the 2004 LCD to the D1 pin of the Wemos D1 Mini.
Step 2: Installing the Required Libraries
In the Arduino IDE, go to Sketch > Include Library > Manage Libraries and install the following libraries:
- DHT sensor library by Adafruit
- LiquidCrystal_I2C by Frank de Brabander
Step 3: Programming the ESP8266 with DHT11 Sensor
Upload the following code to the ESP8266 with the DHT11 sensor:
#include <ESP8266WiFi.h> #include <espnow.h> #include <DHT.h> #define DHTPIN D2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); typedef struct { float temperature; float humidity; } SensorData; SensorData sensorData; void setup() { Serial.begin(115200); dht.begin(); WiFi.mode(WIFI_STA); WiFi.disconnect(); if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); esp_now_add_peer((uint8_t *)"\x40\xF5\x20\x06\x26\xDB", ESP_NOW_ROLE_SLAVE, 1, NULL, 0); } void loop() { sensorData.temperature = dht.readTemperature(); sensorData.humidity = dht.readHumidity(); if (isnan(sensorData.temperature) || isnan(sensorData.humidity)) { Serial.println("Failed to read from DHT sensor!"); return; } esp_now_send(NULL, (uint8_t *)&sensorData, sizeof(sensorData)); delay(2000); }
- Put your MAC address of the board that as the 2004LCD values in here –> esp_now_add_peer((uint8_t *)”\x40\xF5\x20\x06\x26\xDB“, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
Step 4: Programming the ESP8266 with 2004 LCD Display
#include <ESP8266WiFi.h> #include <espnow.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 20, 4); typedef struct { float temperature; float humidity; } SensorData; SensorData sensorData; void setup() { Serial.begin(115200); lcd.init(); lcd.backlight(); WiFi.mode(WIFI_STA); WiFi.disconnect(); if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); esp_now_register_recv_cb(recv_cb); } void loop() { // Nothing to do here } void recv_cb(uint8_t *mac, uint8_t *data, uint8_t len) { memcpy(&sensorData, data, sizeof(sensorData)); displayData(); } void displayData() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Temperature: "); lcd.print(sensorData.temperature); lcd.print(" C"); lcd.setCursor(0, 1); lcd.print("Humidity: "); lcd.print(sensorData.humidity); lcd.print(" %"); }
Step 5: Testing the Setup
Power both ESP8266 boards and observe the 2004 LCD display. It should show the temperature and humidity readings from the DHT11 sensor. The data is transmitted wirelessly using the ESP-NOW protocol. If NOT and all the connections were well made, try push RESET button on the board, the info should show in the screen (it happened to me).
Problems?
Conclusion
In this blog post, we demonstrated how to connect two ESP8266 modules with a DHT11 sensor and a 2004 LCD display using the ESP-NOW protocol. This setup can be easily extended to include other sensors and actuators, making it a versatile solution for IoT applications.