Moving from a serialprint to an external I2C LCD display?

Good afternoon
I have some sketches which work well with a serialprint display, from the PC. Now I would like it to be external display with a I2C LCD display.
Straightforward merging such a displaying function with the output of my sensor does not work.
What am I missing ?
Is there a tutorial for this kind of moves ?
SThe same question will apply to move from the LCD display to a graphic OLED one.

Thanks !

Hello billy60
Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

Your I2C LCD should support some sort of print function. There is an overhead with the I2C LCDs in that you need to know what you are printing where and manage the display accordingly.

For example, if you were to print "1000" on the top row starting at column 1, and then print "999" in the same place, you can end up with the display showing "9990". You could use sprintf() to format your output to 4 digits before printing, or manage it in your code yourself.

Post your code if you want further help.

Help us help you.

Here is the sketch:

#include <Wire.h>
#include <LiquidCrystal_I2C.h> // display is I2C


#include <Adafruit_BMP280.h>
#define BMP280_ADDRESS 0x76
Adafruit_BMP280 bmp; // I2C also

void setup() 

{
 LiquidCrystal_I2C lcd(0x27); // I2C lcd display address is 0x27,  
  lcd.init(); // initialize the lcd
  lcd.setBacklight(255);
  lcd.begin(16,2); //16 column and 2 rows
  lcd.clear();

   
   Serial.begin(9600);
 
  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  status = bmp.begin(0x76);
  if (!status) 
  {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.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);
 
     // Default settings from the datasheet. 
  } 

}

void loop() 

bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
{
  //for the LCD display:
  
    lcd.clear(); 
    lcd.setCursor(1, 0);
    lcd.print("Temp");
    lcd.print(bmp.readTemperature());
    lcd.println("*C");
    
    lcd.setCursor(2, 0); 
    
    lcd.print(" Pression");
    lcd.print(bmp.readPressure());
    lcd.println(" mB");
    
// for the Serial port display:
  
    Serial.print(F("Temp = "));
    Serial.print(bmp.readTemperature()); // also display on the Serial port
    Serial.print(F("Pressure = "));// also display on the Serial port
    Serial.print(bmp.readPressure());// also display on the Serial port
  
  
    Serial.print(F("Approx altitude = "));  // also display on the Serial port
   Serial.print(bmp.readAltitude(1020.35)); /* Adjusted to Senlis altitude 53m, on the serial port */
   

    delay(2000);
}

This sketch is for a mobile altimeter.
I get a compiling error which I don't understand (Arduino : 1.8.13 (Windows 10), Carte : "Arduino Nano, ATmega328P (Old Bootloader)"

Arduino : 1.8.13 (Windows 10), Carte : "Arduino Nano, ATmega328P (Old Bootloader)"

C:\Users\Papa\Documents\Arduino\Altimetre_bme280_LCD\Altimetre_bme280_LCD.ino: In function 'void setup()':

Altimetre_bme280_LCD:14:28: error: no matching function for call to 'LiquidCrystal_I2C::LiquidCrystal_I2C(int)'

Thanks for your help !

void loop() 

bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
{
  

Perhaps the OP may look into auto format and every { needs a } and your code is not properly inside the loop() function.

OK, I checked on some mistakes. 2nd version, still has a compiler error msg::`



#include <Wire.h>
#include <LiquidCrystal_I2C.h> // display is I2C
#define LiquidCrystal_I2C lcd
#define lcd_ADDRESS(0x27) // I2C lcd display address is 0x27,

#include <Adafruit_BMP280.h>
#define BMP280_ADDRESS 0x76
Adafruit_BMP280 bmp; // I2C also

void setup()

{

  lcd.init(); // initialize the lcd
  lcd.setBacklight(255);
  lcd.begin(16, 2); //16 column and 2 rows
  lcd.clear();


  Serial.begin(9600);

  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  status = bmp.begin(0x76);
  if (!status)
  {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                     "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.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);

    // Default settings from the datasheet.
  }

}

void loop()
{
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
{
  //for the LCD display:

  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Temp");
  lcd.print(bmp.readTemperature());
  lcd.println("*C");

  lcd.setCursor(2, 0);

  lcd.print(" Pression");
  lcd.print(bmp.readPressure());
  lcd.println(" mB");

  // for the Serial port display:

  Serial.print(F("Temp = "));
  Serial.print(bmp.readTemperature()); // also display on the Serial port
  Serial.print(F("Pressure = "));// also display on the Serial port
  Serial.print(bmp.readPressure());// also display on the Serial port


  Serial.print(F("Approx altitude = "));  // also display on the Serial port
  Serial.print(bmp.readAltitude(1020.35)); /* Adjusted to Senlis altitude 53m, on the serial port */


  delay(2000);
}
void loop()
{
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
{
  //for the LCD display:

  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Temp");
  lcd.print(bmp.readTemperature());
  lcd.println("*C");

  lcd.setCursor(2, 0);

  lcd.print(" Pression");
  lcd.print(bmp.readPressure());
  lcd.println(" mB");

  // for the Serial port display:

  Serial.print(F("Temp = "));
  Serial.print(bmp.readTemperature()); // also display on the Serial port
  Serial.print(F("Pressure = "));// also display on the Serial port
  Serial.print(bmp.readPressure());// also display on the Serial port


  Serial.print(F("Approx altitude = "));  // also display on the Serial port
  Serial.print(bmp.readAltitude(1020.35)); /* Adjusted to Senlis altitude 53m, on the serial port */


  delay(2000);
}

What did you fix because this still looks like the code from post#5 and there is still code outside of loop().

see this { starts void loop()

and this

ends void loop()

what's this do:

which function is that code in?

and this

  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */

should be in setup.

I have a hard time answering you !
here is a sketch which I found and it does work. The sampling part is in the setup, indeed it happens only once at the beginning.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#define BMP280_ADDRESS 0x76
Adafruit_BMP280 bmp; // I2C


void setup() 
{
  Serial.begin(9600);



  
  
  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  status = bmp.begin(0x76);
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.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);
  }

    /* Default settings from the datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1020.35)); /* Adjusted to Senlis altitude 53m */
    Serial.println(" m");
    Serial.println();
    delay(5000);
}

So I worked from this sketch.
After correcting some mistakes which you pointed out, mine still gives out compiler error messages which I don't understand...
I duplicated the define and includes, for the LCD, but still no success.
The Compiler sends error messages which are so cryptic !

Those error messages you are getting, that you are not sharing, are really cryptic to me.

Here they are:

Arduino : 1.8.13 (Windows 10), Carte : "Arduino Nano, ATmega328P (Old Bootloader)"





















C:\Users\Papa\Documents\Arduino\Altimetre_bme280_LCD\Altimetre_bme280_LCD.ino: In function 'void setup()':

Altimetre_bme280_LCD:18:3: error: 'lcd' was not declared in this scope

   lcd.init(); // initialize the lcd

   ^~~

C:\Users\Papa\Documents\Arduino\Altimetre_bme280_LCD\Altimetre_bme280_LCD.ino: At global scope:

Altimetre_bme280_LCD:29:1: error: expected unqualified-id before '{' token

 {

 ^

C:\Users\Papa\Documents\Arduino\Altimetre_bme280_LCD\Altimetre_bme280_LCD.ino: In function 'void loop()':

Altimetre_bme280_LCD:58:3: error: 'lcd' was not declared in this scope

   lcd.setCursor(0, 0);

   ^~~

Plusieurs bibliothèque trouvées pour "Adafruit_BMP280.h"

Utilisé : C:\Users\Papa\Documents\Arduino\libraries\Adafruit_BMP280_Library

Non utilisé : C:\Users\Papa\Documents\Arduino\libraries\Adafruit_BMP280_Library-2.6.6

Plusieurs bibliothèque trouvées pour "Adafruit_Sensor.h"

Utilisé : C:\Users\Papa\Documents\Arduino\libraries\Adafruit_Unified_Sensor

Non utilisé : C:\Users\Papa\Documents\Arduino\libraries\ArdUAV

Plusieurs bibliothèque trouvées pour "LiquidCrystal_I2C.h"

Utilisé : C:\Users\Papa\Documents\Arduino\libraries\LiquidCrystal_I2C

Non utilisé : C:\Users\Papa\Documents\Arduino\libraries\New-LiquidCrystal-master

Non utilisé : C:\Users\Papa\Documents\Arduino\libraries\LiquidCrystal_I2C-1.1.2

exit status 1

'lcd' was not declared in this scope



Ce rapport pourrait être plus détaillé avec
l'option "Afficher les résultats détaillés de la compilation"
activée dans Fichier -> Préférences.

Did you check this recommendation?

The code from post#9 codes not have a reference to lcd. Which code is causing the... Never mind.

Good luck.