BME280 i2C sensor and LCD-20x4 i2C

Hello all,

Hope you can tell me how to solve this issue. Thanks in advance.

I'm trying to use the BME280 sensor-i2C (from Adafruit) to display the values of Temperature, Pression, Attitude and Humidity on LCD-20x4-i2C.

With the code attached, I can see the values on Serial Monitor [with printValuesOnMonitor() function] but I can not display these values on LCD (with printValuesOnLCD() function) in the same time.

Regards,

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h> // SENSOR-i2C

// Library added for LCD-i2C (20x4)
#include "LCD.h" 
#include "LiquidCrystal_I2C.h" 

//Set the pins on the i2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the default i2C bus address of LCD

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; 

unsigned long delayTime;

void setup() {
    Serial.begin(9600);
    while(!Serial);    // time to get serial running
    Serial.println(F("BME280 test"));

    lcd.begin (20,4); // 20x4 i2C-LCD module
    lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
    lcd.setBacklight(HIGH);
   
    unsigned status;
    
    // default settings
    status = bme.begin();  
    // status = bme.begin(0x76); // 0x76 is the default i2C bus address of BME280
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
        Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
        Serial.print("        ID of 0x56-0x58 represents a BMP 280,\n");
        Serial.print("        ID of 0x60 represents a BME 280.\n");
        Serial.print("        ID of 0x61 represents a BME 680.\n");
        while (1) delay(10);
    }
    
    Serial.println("-- Default Test --");
    delayTime = 1000;

    Serial.println();

}

  /* Example: Set off LCD module
   lcd.begin (20,4); // 20x4 LCD module
   lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
   lcd.setBacklight(HIGH);
   lcd.print("Hello World !");
   lcd.setCursor(0,1);
   lcd.print(" Good Day !");
   delay(5000);   
   */

void loop() { 
    printValuesOnMonitor();
    
    printValuesOnLCD();
    
    delay(delayTime);
}

void printValuesOnMonitor() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");
   
    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

void printValuesOnLCD(){
    
    // lcd.clear();

    // Display Temperature
    lcd.setCursor(0,1);
    lcd.print("Temp = "); 
    lcd.setCursor(0,6);
    lcd.print(bme.readTemperature());
    lcd.println("*C");

    // Display Pressure
    // Diaplay Attitude
    // Display Humidity
}

BME280-monitor.jpg

BME280-monitor.jpg

What Arduino board? What LCD? Are you sure of the LCD I2C address? Are you sure of the LCD to I2C backpack pin mapping?

Have you tried the contrast adjustment?

Assuming a hd44780 controlled monochrome character LCD with an I2C backpack (PCF8574 or MCP23008):
For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.

Install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.

groundFungus:
What Arduino board? What LCD? Are you sure of the LCD I2C address? Are you sure of the LCD to I2C backpack pin mapping?

Have you tried the contrast adjustment?

Hello "groundFungus" and "noiasca",
Thanks for reply.
I use Arduino Uno R3 board with LCD-I2C standard 20x4. I follow this tutor.
I have scanned the I2C addresses for both BM280 (0x76) and LCD (0x72). It's fine. I can run the small example code to see LCD displayed "Hello world !" and "Good day" and see the values on Serial Monitor.
I have adjusted the contrast of LCD with the potentiometer to see the text displayed.

Thanks,

if this is true:

and LCD (0x72). I

than this wrong:

LiquidCrystal_I2C lcd(0x27

for a first try, take ONLY the hello world example - WHICH COMES WITH YOUR LIBRARY.
and ensure that you are using the right i2c address.

noiasca:
if this is true:

and LCD (0x72). I

than this wrong:

LiquidCrystal_I2C lcd(0x27

for a first try, take ONLY the hello world example - WHICH COMES WITH YOUR LIBRARY.
and ensure that you are using the right i2c address.

You’re right, I2C LCD address is 0x27.
Thanks,

Could you please tell me how to erase 2 stranged characters in the end of each line on this LCD-20x4 ?

See Photo attached

Thanks

Do not use println() on LCD but print(). What you see are characters 10 (LF) and 13 (CR).

I found... bc I use println("%"). Thanks.