A more smooth update of LCD

I have just built the project here TDS Sensor & Arduino Interfacing for Water Quality Monitoring

The problem is that the code makes the LCD to turn off and update in a flickering way. I want a smooth continious updating of the values. How do I change the code?



#include <EEPROM.h>
#include "GravityTDS.h"
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
#define ONE_WIRE_BUS 7
#define TdsSensorPin A1
 
OneWire oneWire(ONE_WIRE_BUS);
GravityTDS gravityTds;
 
DallasTemperature sensors(&oneWire);
 
float tdsValue = 0;
 
void setup()
{
    Serial.begin(115200);
    lcd.begin(16,2);
    sensors.begin();
    gravityTds.setPin(TdsSensorPin);
    gravityTds.setAref(5.0);  //reference voltage on ADC, default 5.0V on Arduino UNO
    gravityTds.setAdcRange(1024);  //1024 for 10bit ADC;4096 for 12bit ADC
    gravityTds.begin();  //initialization
}
 
void loop()
{
    sensors.requestTemperatures();
 
    gravityTds.setTemperature(sensors.getTempCByIndex(0));  // set the temperature and execute temperature compensation
    gravityTds.update();  //sample and calculate
    tdsValue = gravityTds.getTdsValue();  // then get the value
    
    Serial.print(tdsValue,0);
    Serial.println("ppm");
    Serial.print("Temperature is: ");
    Serial.print(sensors.getTempCByIndex(0));
    
    lcd.setCursor(0, 0);
    lcd.print("TDS: ");
    lcd.print(tdsValue,0);
    lcd.print(" PPM");
 
    lcd.setCursor(0, 1);
    lcd.print("Temp: ");
    lcd.print(sensors.getTempCByIndex(0));
    lcd.print(" C");
    
    delay(1500);
    lcd.clear();
}

Never use clear in the loop.
Always put the static stuff, TDS PPM etc., in setup
Only update what needs updating, i.e. the values.

2 Likes

You should not use lcd.clear. instead you should place the cursor at the place where the changing number should be and then overwrite the old number with the new one.
The things that do not change (your text) should be printed to the lcd in setup.

Put five "spaces" after PPM on this line:

    lcd.print(" PPM     "); // five spaces added so range can be 0 to -XXXX

Comment-out this line:

    // lcd.clear(); // commented-out

[edit]

And put six "spaces" after C on this line:

    lcd.print(" C      ");

Here is an example, if it can help : exemple_pour_francky_youpy_v2 - Wokwi ESP32, STM32, Arduino Simulator

An improvement to this code would be to first read all temperatures, then update all values on the lcd at once

Thanks!

Thank you! Must try this! Much appreciated!

Thanks you!

Not tried yet but it seems correct

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