Get to Know ESP32 #5: Using LCD To Display Output from Weather Sensor

Audrey Betsy Rumapea
4 min readFeb 23, 2020

--

Hello guys welcome back to my Youtube channel, eh, Medium blog.

Two weeks ago, we have talked about one of the weather sensors — which is DHT11, while a week ago we’ve done displaying outputs to LCD. So, what’s gonna be our topic today? Yes, you’ve guessed it right, we’re gonna combine the two!

A little info: My team used DHT22 for this project. The DHT11 we used on previous project was borrowed from another team, and it turned out one of our team members had a DHT22.

Preparation

Equipments needed are below:

  • 1 Laptop with Arduino IDE installed and configured (check out my other story to configure Arduino IDE)
  • 1 USB-A to Micro-USB cable
  • 1 10k Ohm Resistor
  • 4 Female-to-Female Wire Jumpers
  • 2 Male-to-Male Wire Jumpers
  • 3 Male-to-Female Wire Jumpers
  • 1 DHT11/DHT22 Sensor
  • 1 Breadboard
  • 1 NodeMCU ESP-32S Development Board (mine with 38 pins)

Circuit

For the wiring, I combined the circuits from the LCD project and DHT project. On my DHT22, sensor pin number 2 and 3 are connected.

Circuit Reference

You can also use this wiring as a reference. It’s supposed to be a circuit for OLED, but we’ve also tried this wiring schema and it worked as well.

Here’s how my circuit actually looked like.

my circuit

Code

Next, we’re going to code. As I’ve said before, I combined the codes from my previous projects. These are the adjustments I have to do:

  • Include libraries needed for both DHT and LCD
  • Copy the setup() code from DHT and LCD to this project’s setup()
  • Copy the loop() code from DHT and LCD to this project’s loop()
  • Delete the Serial.begin(9600) and Serial.println(F(“DHTxx Test!”)) part
  • Adjust the #define DHTPIN 4 according to the GPIO you’re using to connect ESP32 to DHT
  • Change the Serial.<command>() to lcd.<command>()
  • Think about how outputs should be displayed, with idea in mind that LCD has only 2 rows with 16 characters each row

Here’s my code:

#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 4
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);void setup() {
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
lcd.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// set cursor to first column, first row
lcd.setCursor(0, 0);
lcd.print(F("Hum: "));
lcd.print(h);
lcd.println(F("% "));

lcd.setCursor(0, 1);
lcd.print(F("Temp: "));
lcd.print(t);
lcd.print(F("°C "));
lcd.print(f);
lcd.println(F("°F"));

delay(1000);
// clears the display to print new message
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("Heat index:"));
lcd.setCursor(0,1);
lcd.print(hic);
lcd.print(F("°C "));
lcd.print(hif);
lcd.println(F("°F"));

delay(1000);
// clears the display to print new message
lcd.clear();
}

Demonstration

As we always do every week, verify the code. Then, connect the ESP-32S development board to laptop/PC via cable. Next step is to upload the code, and it’s supposed to be displaying measurements from DHT22 on the LCD.

There are some errors I came accross.

  1. It shows ‘Failed to Read From DHT Sensor!’ After I checked, there are some mismatched cables.
  2. The code. I used println() instead of setting cursor in the second row.

After managing to deal with these errors, my project went like this.

Yeay, we’re done! Please drop comments if you need any assistance and I’ll try my best to help. See you next week, adios!

--

--

No responses yet