Display issues using multiple Sensors

Hey there, im new and got a question regarding displays used for micro controllers.

I usa a arduino nano esp32 board which is connected to multiple sensors (BMP180, SGP30 & GY302) and the values should be printed on the display.

Till yet everything works like it should beside one thing: If one of the printed values on the display extends its digits (e.g. 300 ppb => 3000 ppb) and afterwards decreases the digits again (3000 ppb => 300 ppb) i get the correct repond for 300 ppb inside the serial monitor. On the display the last digit remains and does not dissapear again. Did anybody had this issue as well and has a solution ? :slight_smile:

Please see my code below. Maybe there is an issue?

#include <LiquidCrystal.h>
#include <Wire.h>
#include <SimpleDHT.h>
#include <BMP180I2C.h>
#include <SGP30.h>
#include <BH1750.h>

#define I2C_ADDRESS 0x77

//create an BMP180 object using the I2C interface
BMP180I2C bmp180(I2C_ADDRESS);

// Variable SGP innerhalb SGP30 Klasse definieren
SGP30 SGP;

// Variable light Meter innerhalb BH1750 Klasse definieren
BH1750 lightMeter;

//Bytezähler auf 0 setzen
uint8_t count = 0;

// Pin Belegung des LCD Displays definieren.
const int rs=7, en=8, d4=9, d5=10, d6=11, d7=12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {
// Displaygrösse definieren.
lcd.begin(16, 2); 

// Baudrate auf 9600 setzen zur unviersellen Kommunikation.
Serial.begin(9600); 


//wait for serial connection to open (only necessary on some boards)
while (!Serial);

// Wire ĂĽbertragung aktivieren
Wire.begin();

// SGP30 Sensor aktivieren
SGP.begin();

//begin() initializes the interface, checks the sensor ID and reads the calibration parameters.  
if (!bmp180.begin()){
		Serial.println("begin() failed. check your BMP180 Interface and I2C Address.");
		while (1);
	}

//reset sensor to default parameters.
bmp180.resetToDefaults();

//enable ultra high resolution mode for pressure measurements
bmp180.setSamplingMode(BMP180MI::MODE_UHR);


// Lichtsensor starten
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {

// Start Temperaturmessung BMP180 Sensor
if (!bmp180.measureTemperature()){
		return;
	}

//wait for the measurement to finish. proceed as soon as hasValue() returned true. 
do {
		delay(100);
	} while (!bmp180.hasValue());

//wait for the measurement to finish. proceed as soon as hasValue() returned true. 
Serial.print("Temperature: "); 
Serial.print(bmp180.getTemperature()); 
Serial.println(" °C");

//start a pressure measurement BMP180. pressure measurements depend on temperature measurement, you should only start a pressure measurement immediately after a temperature measurement. 
if (!bmp180.measurePressure()){
		return;
	}

//wait for the measurement to finish. proceed as soon as hasValue() returned true. 
do {
		delay(100);
	} while (!bmp180.hasValue());

Serial.print("Pressure: "); 
Serial.print(bmp180.getPressure()/100);
Serial.println(" hPa");

// Start SGP30 Sensor Messung fĂĽr TVOC, CO2, H2, ETOH
SGP.measure(true); 

Serial.print("TVOC = ");
Serial.print(SGP.getTVOC());
Serial.print(" ppb");
Serial.print("\t");
Serial.print("CO2 = ");
Serial.print(SGP.getCO2());
Serial.print(" ppm");
Serial.print("\t");
Serial.print("H2 = ");
Serial.print(SGP.getH2());
Serial.print(" ppm");
Serial.print("\t");
Serial.print("ETOH = ");
Serial.print(SGP.getEthanol());
Serial.print(" ppm");
Serial.println();

// Lichtsensor zur ausgabe der aktuelle Lichtintensität

 while (!lightMeter.measurementReady(true)) {
    yield();
  }
float lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
lightMeter.configure(BH1750::ONE_TIME_HIGH_RES_MODE);

// Displayanzeige definieren

lcd.setCursor(0, 0); 
  // Position setzen von wo aus der näcchste input kommen soll. column 0, line 0 (note: line 1 is the second row, since counting begins with 0)

lcd.print(round(bmp180.getTemperature()), 1); // Ausgabe der Temperatur des BMP180 Sensors
lcd.setCursor(4, 0);
lcd.print("C");
lcd.setCursor(8, 0);
lcd.print(round(bmp180.getPressure()/100), 1);
lcd.print("hPa");
lcd.setCursor(0, 1);
lcd.print(SGP.getTVOC());
lcd.setCursor(4, 1);
lcd.print("ppb");
lcd.setCursor(8, 1);
lcd.print(SGP.getCO2());
lcd.setCursor(13, 1);
lcd.print("ppm");


delay(2000);

}

It's a common beginner problem.

I'll take a look at your code and suggest something.

Try this:

lcd.setCursor(0, 1);
lcd.printf("%4d ppb", SGP.getTVOC());
1 Like

Awesome!! It worked :slight_smile: Thanks a lot! May i ask where is exactly the difference between the 2 commands?

You are lucky that you are using an ESP chip. The printf() function is not available on most Arduino models.

The difference between print() and printf() is that the second allows a format string to be given into which values can be inserted.

The format string contains the following special characters:

% indicates that a value should be inserted into the format string at that point.

4 indicates that the value should be at least 4 characters long, even if fewer than 4 characters are needed. When they are not needed, spaces are used to pad out the value to make it 4 characters long.

d indicates that the value should be printed in decimal.

The other characters in the format string are not special, they are printed as they are.

There are many other special characters and styles that can be used, and multiple values can be included into a single string.

If the printf() weren't available, you could do one of three things you'll see

  • print (again) the previously printed number using the background colour
  • print a bunch of spaces and reset the column and row
  • draw a rectangle in the background colour over the output area

Each effectively erases the screen before you print the new number or whatever.

In other code you'll see the entire screen being redrawn, this can often be avoided and it is worth doing as it can really make a display more responsive and professional looking.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.