Help setting analog read rate / lcd refresh rate

I would really appreciate some help with setting up my temperature sensor. I'm trying to display the temperature reading onto the LCD that came with the arduino kit but it refreshes far too fast so it's near impossible to read on the screen.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int tempPin = A0;
int sensorVal = 0;
void setup() {
  lcd.begin(16, 2);
  pinMode(tempPin,INPUT);
  lcd.print("Internal Temp");
  lcd.setCursor(0, 1);
  lcd.print("Sensor v.1.0");
  delay(5000);
}

void loop() {
  sensorVal = analogRead(tempPin);
  float voltage = (sensorVal / 1024.0) * 5.0;
  float temperature = (voltage - 0.5) * 100;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Internal Temp is");
  lcd.setCursor(0, 1);
  lcd.print(temperature);
  lcd.setCursor(6, 1);
  lcd.print("degrees C");
}

You can't change the analog read rate or the LCD refresh rate. Those are fixed by the hardware.

What you can change is WHEN you read the sensor and/or WHEN you change what is displayed.

Look at the blink without delay rate to change when you read the sensor. Change the displayed value only when the value to be displayed is not the same as the value being displayed. Of course, that means keeping track of what is being displayed.

1 Like

I've added in what i think should be in the code but now it won't even display the first line properly.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int tempPin = A0;
int sensorVal = 0;
long previousMillis = 0;
long interval = 1000;
void setup() {
  lcd.begin(16, 2);
  pinMode(tempPin,INPUT);
  lcd.print("Internal Temp");
  lcd.setCursor(0, 1);
  lcd.print("Sensor v.1.0");
  delay(5000);
}

void loop() {
  sensorVal = analogRead(tempPin);
  float voltage = (sensorVal / 1024.0) * 5.0;
  float temperature = (voltage - 0.5) * 100;
  unsigned long currentMillis = millis();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Internal Temp is");
  if (currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    lcd.setCursor(0, 1);
    lcd.print(temperature);
    lcd.setCursor(6, 1);
    lcd.print("degrees C");
  }
  else
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Internal Temp is");
  }
}

Try reading the sensor periodically, instead of displaying new data periodically.

   if(currentMillis - previousMillis >= interval)
   {
      // read the sensor
      // refresh the lcd
      previousMillis = currentMillis;
   }
1 Like

I tried this:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int tempPin = A0;
int sensorVal = 0;
long previousMillis = 0;
long interval = 1000;
void setup() {
  lcd.begin(16, 2);
  pinMode(tempPin,INPUT);
  lcd.print("Internal Temp");
  lcd.setCursor(0, 1);
  lcd.print("Sensor v.1.0");
  delay(5000);
}

void loop() {
  
  unsigned long currentMillis = millis();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Internal Temp is");
  if (currentMillis - previousMillis >= interval)
  {
    sensorVal = analogRead(tempPin);
    float voltage = (sensorVal / 1024.0) * 5.0;
    float temperature = (voltage - 0.5) * 100;
    lcd.setCursor(0, 1);
    lcd.print(temperature);
    lcd.setCursor(6, 1);
    lcd.print("degrees C");
    previousMillis = currentMillis;
  }
  else
  {
    lcd.clear();
  }
}

but it just made the entire screen refresh constantly so i could only read "Internal temp"

1 Like

but it just made the entire screen refresh constantly

Of course it did. You REALLY need to give some thought as to when you call lcd.clear().

Should it be called on every pass through loop()? Or, should it be called only when there is new data to display? Or, should it never be called. If you print() a couple of spaces after the "degrees C", then changes in the number of characters used to represent the temperature will not cause problems, and you never need to clear the lcd.

1 Like

Thanks it's working fine now sorry for being such a noob at this :smiley:

sorry for being such a noob at this

No problem. Happy to help.

What did you end up with, for code?

1 Like