LCD problem : increasing power of 10 when reading dips below 100

Hi there,

I'm using an Arduino Uno and an MQ-135 to detect ethanol and send readings on an LCD screen.
Everything works fine, until the reading goes above 100 and then dips back below 100 : the LCD adds a power of 10 to the readings from that point on for all values with 2 digits, for example : 90 on the serial monitor becomes 900. Readings above are correct (for example, 110 on the serial monitor becomes 110). The error only applies to the 2 digit values.

Here is my code :

#include <LiquidCrystal.h>
const int rs= 12 , en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int buz=8;
int led=13;
int aqsensor=A0;
int threshold = 150;

void setup() {
  // put your setup code here, to run once:

pinMode (buz,OUTPUT);
pinMode (led,OUTPUT);

Serial.begin(9600);

lcd.clear();
lcd.begin(16,2);

}

void loop() 
{
  // put your main code here, to run repeatedly:

int AQ= analogRead(aqsensor);

Serial.print("Air Quality: ");
Serial.println(AQ);

lcd.setCursor(0,0);
lcd.print("Air Quality: ");
lcd.print(AQ, DEC);

if (AQ > threshold) 
{
  digitalWrite(buz,HIGH);
  digitalWrite(led,HIGH);
  tone(buz,1000,200);
  lcd.setCursor(1,1);
  lcd.print("AQ Level High");

  

}
else 
{
  digitalWrite(led,LOW);
  digitalWrite(buz,LOW);
  lcd.setCursor(1,1);
  lcd.print("AQ Level Good");
}

delay(500);

}

Here is the LCD screen I am using. A datasheet is included in the page :

Thanks for your help.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience this is the easiest way to tidy up the code and add the code tags

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

The reason is that the display does not automatically erase the old text, but prints the new one on top. Once you move from three-digit numbers to two-digit numbers, one zero remains on the screen.
To prevent this, you can output two-digit numbers with a trailing space - that is, not "90", but "90 "

1 Like

No it doesn't. What is happening is that the zero printed at the end of the value of 100 is not being overwritten so is still visible

The easiest way to fix this is to print a space after a value so that the previously printed zero is removed from the display

2 Likes
#include <LiquidCrystal.h>
const int rs= 12 , en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int buz=8;
int led=13;
int aqsensor=A0;
int threshold = 150;
char buffer[17];

void setup() {
  // put your setup code here, to run once:

pinMode (buz,OUTPUT);
pinMode (led,OUTPUT);

Serial.begin(9600);

lcd.clear();
lcd.begin(16,2);

}

void loop() 
{
  // put your main code here, to run repeatedly:

int AQ= analogRead(aqsensor);
sprintf(buffer, "Air Quality: %3d", AQ);

Serial.println(buffer);

lcd.setCursor(0,0);
lcd.print(buffer);

if (AQ > threshold) 
{
  digitalWrite(led,HIGH);
  tone(buz,1000,200);
  lcd.setCursor(1,1);
  lcd.print("AQ Level High");

  

}
else 
{
  digitalWrite(led,LOW);
  noTone(buz);
  lcd.setCursor(1,1);
  lcd.print("AQ Level Good");
}

delay(500);

}
1 Like

Thank you very much everyone for your help, it works great now!

The problem is resolved, but I was wondering why lcd.clear() in void loop() wouldn't erase the third character of a 3-digit number.

In your code lcd.clear() is in the setup(), and not in loop()

lcd.clear() will indeed clear the third digit along with everything else on the screen. This tends to cause horrendous flicker and is best avoided if possible

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