16x2 lcd screen crashes after a while

I have built a simple temperature reader using Arduino UNO, and a NTC thermistor. It basically reads the voltage across the thermistor and converts into temperature. I also used an external volage reference IC. Internal interrupt is used to sleep the CPU during the measurement. Everything works well for a while, then LCD crashes and displays random characters. I am seeking help. I have checked the wiring, but everything been correct. External 9V-1A power supply is used.

Here is the sketch:

#include <avr/sleep.h>
#include <LiquidCrystal.h>
#include <math.h>

int const aREF = 0b00000000;
int ThermistorPin = 0;
int Vo;
float R1 = 20000;
float logR2, T, Tc, Vo_2, R2;
float c1 = 1.129241e-03, c2 = 2.341077e-04, c3 = 0.087755e-06;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
ADMUX = aREF;
ADCSRA |= 1 << ADEN;
ADCSRA |= 1 << ADIE;
ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0)); // Corrected ADPS setting
sleep_enable();
set_sleep_mode(SLEEP_MODE_ADC);
analogReference(EXTERNAL);
lcd.begin(16, 2); // Initialize LCD
}

void loop() {
sei();
sleep_cpu();
Vo = analogRead(ThermistorPin);
Vo_2 = ((float)Vo / 1023.0) * 5.0;
R2 = R1 * ((5.0 - Vo_2) / Vo_2);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15;

lcd.setCursor(0, 0); 
lcd.print("Temp = ");
lcd.print(Tc);
lcd.print(" C");

delay(500);
lcd.clear();

}

ISR(ADC_vect) {}

What the point to sleep cpu in fraction of the millisecond during the ADC if you have a delay 500 ms in your CPU active state?

Remove all sleeping code from the sketch and test how it works. "Keep it clean and simple"

Search for "ratiometric".
If you power the NTC + resistor with the 5V pin and use the default reference of the 5V, then you get the maximum accuracy.
With averaging/oversampling you can get a higher resolution.
If the NTC has a datasheet with a table, then you get the best accuracy.

Can you show a photo, so we can see the wiring and the display ?

I made the circuit in Wokwi simulation. Wokwi has a common NTC module with a 10k resistor. I can not get the right temperature.

After starting the simulation, click on the NTC module to change the temperature.

Please edit your opening post, select all code and click the <CODE/> button; next save your post.

This applies code tags (as described in How to get the best out of this forum) which makes the code easier to read, easier to copy and prevents the forum software from interpreting the code as formatting instructions.

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