This project is about building a temperature monitor using an ESP-32, BME280 temperature sensor and a Liquid Crystal Display to monitor temperature, humidity and pressure. In this first part we're going to make the microcontroller read values from the sensor and show them on the display.
ESP-32 Weather Display
The final version of this project will hopefully be a temperature controlled smart fan for my homelab, integrated into Home Assistant. In this first phase we will read information from the temperature sensor and in the next phase we will communicate the output via WiFi.
The display I used is a LCD1602 module with 16 columns on 2 lines and I2C interface. The temperature sensor is a BME280 also with I2C interface. The two devices are hooked up to the ESP-32 by connecting each SCL and SDA to the appropriate pin on the ESP-32 - in parallel. The BME280 has a range of 1.8V to 5V so I connected to 3.3V. The display has an input range of 2.5V to 6V but I found the contrast was too low running at 3.3V.
ESP-32 Temperature Sensor - Connected
First there are some software modules to be installed in the Arduino IDE:
/**
* Brief : Display temperature, humidity and pressure on LCD
* Date : 2024.08.19
* Author: Lyndon Hill, based on code from randomnerdtutorials.com
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// the message being displayed and the number of delays it has been shown for
int messageNumber = 0;
int messageCount = 0;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
String messageStatic = "ESP32 Weather";
Adafruit_BME280 bme; // I2C
unsigned long delayTime;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
Serial.println(F("BME280 and LCD test"));
bool status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while(1);
}
delayTime = 250;
}
void loop() {
// print first row message to LCD
lcd.setCursor(0, 0);
lcd.print(messageStatic);
// print values on second row of LCD
updateValues();
delay(delayTime);
}
// Read temperature sensor and print values to LCD
void updateValues() {
float temperature = bme.readTemperature(); // Celcius
float pressure = bme.readPressure()/100.0F; // hPA
float humidity = bme.readHumidity(); // %
char messageBuffer[16];
switch(messageNumber) {
case 0:
snprintf(messageBuffer, 16, "Temp: %2.2f %cC \0", temperature, char(223));
break;
case 1:
snprintf(messageBuffer, 16, "Humidity: %2.0f%% \0", humidity);
break;
case 2:
snprintf(messageBuffer, 16, "Pressure: %2.0f\0", pressure);
break;
}
lcd.setCursor(0, 1); // move cursor to start of row 1
lcd.print(messageBuffer);
messageCount++;
if(messageCount > 8) {
messageNumber++;
messageCount = 0;
}
if(messageNumber > 2) messageNumber = 0;
}
Compile and upload the code in the usual way.
This code was based mainly on these tutorials ESP32 with BME280 Sensor and How to Use I2C LCD with ESP32 on the Random Nerd Tutorials website, so if you use my version please go and check out their work.
< Back