ESP32 Project #6: BMP180 sensor & I2C LCD Display

Hello, I’m back!. On this project, I’ll show you how to display temperature, pressure, and altitude on 1602 LCD. This project is a combination of my last 2 projects. Kindly read them before jumping to this project :)

Since my BMP280 was not working properly, I bought a new sensor: BMP180. Let’s see if this works.

Let’s start!

Parts and Components

  • Arduino IDE
  • ESP32 Board
  • Breadboard
  • Jumper wires
  • BMP180 sensor
  • I2C LCD
  • USB cable

I followed the tutorial from randomnerdtutorials. But I didn’t find the combination of BMP180 and LCD. Thus, I tried to follow only the wiring, and trying to combine the code myself.

At first, I couldn’t connect the LCD and BMP180 because of the wiring. I mistakenly put the VCC on the 3V3 pin. The LCD was on but not enough power. Then I followed this:

LCD/BMP180 — ESP32

  • GND — GND
  • VCC — VIN
  • SDA — GPIO 21
  • SCL — GPIO 22
The right schema

Yes! It worked! Let’s write the code.

The code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_BMP085.h>
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);Adafruit_BMP085 bmp;void setup() {
Serial.begin(115200);

bool status = bmp.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BMP180 sensor, check wiring!");
while (1);
}
lcd.begin(16,2);
lcd.init();
// turn on LCD backlight
lcd.backlight();
}void loop() {
float t = bmp.readTemperature();
float h = bmp.readAltitude();
float p = bmp.readPressure();
float sp = bmp.readSealevelPressure();

if (isnan(t) || isnan(h) || isnan(p) || isnan(sp)) {
lcd.print("Failed to read");
}
lcd.clear();lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Alt: ");
lcd.print(h);
lcd.print(" meters");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pres: ");
lcd.print(p);
lcd.print(" Pa");
lcd.setCursor(0,1);
lcd.print("Sealv pres: ");
lcd.print(sp);
lcd.print(" Pa");

delay(1000);
}

This code is a combination of the last 2 projects. I’ll only explain the void loop() since I’ve explained the setup().

float t = bmp.readTemperature();
float h = bmp.readAltitude();
float p = bmp.readPressure();
float sp = bmp.readSealevelPressure();

I assigned the temperature, altitude, pressure, and the sea level pressure on the variables t, h, p, and sp.

if (isnan(t) || isnan(h) || isnan(p) || isnan(sp)) {
lcd.print("Failed to read");
}
lcd.clear();

If one of the variables contains NaN value, the LCD will print “Failed to read”. Then I cleared the LCD display.

lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Alt: ");
lcd.print(h);
lcd.print(" meters");
delay(2000);
lcd.clear();

On the first display, I displayed the temperature and altitude in Celcius and meters. I printed the temperature on the first row and the altitude on the second row by setting the cursor. The display will stay for 2 seconds before cleared.

lcd.setCursor(0,0);
lcd.print("Pres: ");
lcd.print(p);
lcd.print(" Pa");
lcd.setCursor(0,1);
lcd.print("Sealv pres: ");
lcd.print(sp);
lcd.print(" Pa");

delay(2000);

On the second display, I displayed the pressure and sea level pressure in Pascal. It’s the same with the first display with 2 seconds delay.

The display looked like this:

Temperature, altitude, pressure, sea level pressure display on LCD

I tried to put my hand on the sensor then the temperature changed. I guess it was working properly :D

I also noticed that the LCD didn’t display the string fully. To display it, I used the scrolling message which I did on my recent project.

Scrolling message

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_BMP085.h>
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);Adafruit_BMP085 bmp;void scrollText(String message, String message2, int delayTime, int lcdColumns) {
for (int i=0; i < lcdColumns; i++) {
message = " " + message;
message2 = " " + message2;
}
message = message + " ";
message2 = message2 + " ";
for (int pos = 0; pos < message.length(); pos++) {
lcd.setCursor(0, 0);
lcd.print(message.substring(pos, pos + lcdColumns));
lcd.setCursor(0,1);
lcd.print(message2.substring(pos, pos + lcdColumns));
delay(delayTime);
}
}
void setup() {
Serial.begin(115200);

bool status = bmp.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BMP180 sensor, check wiring!");
while (1);
}
lcd.begin(16,2);
lcd.init();
// turn on LCD backlight
lcd.backlight();
}void loop() {
float t = bmp.readTemperature();
float h = bmp.readAltitude();
float p = bmp.readPressure();
float sp = bmp.readSealevelPressure();
String messageToScroll, messageToScroll2;
lcd.setCursor(0, 0);
lcd.clear();

if (isnan(t) || isnan(h) || isnan(p) || isnan(sp)) {
lcd.print("Failed to read");
}
lcd.clear();messageToScroll = "Temperature: " + String(t) + " C";
messageToScroll2 = "Altitude: " + String(h) + " meters";
scrollText(messageToScroll, messageToScroll2, 150, lcdColumns);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pres: ");
lcd.print(p);
lcd.print(" Pa");
lcd.setCursor(0,1);
lcd.print("Sealv pres: ");
lcd.print(sp);
lcd.print(" Pa");

delay(1000);
}

It was the same with procedure scrollText but with some modifications.

void scrollText(String message, String message2, int delayTime, int lcdColumns) {
for (int i=0; i < lcdColumns; i++) {
message = " " + message;
message2 = " " + message2;
}
message = message + " ";
message2 = message2 + " ";
for (int pos = 0; pos < message.length(); pos++) {
lcd.setCursor(0, 0);
lcd.print(message.substring(pos, pos + lcdColumns));
lcd.setCursor(0,1);
lcd.print(message2.substring(pos, pos + lcdColumns));
delay(delayTime);
}
}

I modified the parameter with 2 messages. I want the first row and the second row scroll at the same time, so I added the parameter message2. Then I just copied the algorithm from message to message2.

messageToScroll = "Temperature: " + String(t) + " C";
messageToScroll2 = "Altitude: " + String(h) + " meters";
scrollText(messageToScroll, messageToScroll2, 150, lcdColumns);

On the void loop(), I assigned the temperature and altitude to the messageToScroll and messageToScroll2 variables. I used the String() to convert a float number to a string. After that, the scrollText procedure did the rest to display the scrolling message.

Scrolling temperature and altitude display

Final say

I followed the tutorial from randomnerdtutorials but couldn’t find the right device combination. I tried to follow the wiring for OLED on the site and thankfully it worked. I learned a lot since I combine the codes from 2 projects myself and even tried the scrolling message. Hopefully this post will help :)

Thanks for reading and stay tune for the next projects!

Learner || Product and UX enthusiast