#include <Arduino.h>
#include <LiquidCrystal.h>
#include <math.h>  // log is the NATURAL logarithm used in the example's thermistor equation.
const uint8_t TEMPERATURE_PIN = A0;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() { lcd.begin(16, 2); }
void loop() {
  const int reading = analogRead(TEMPERATURE_PIN);
  // The divider has the NTC toward +5 V and a fixed 10 kΩ resistor toward GND.
  // Reject rail readings before division/logarithms; these commonly indicate a broken divider.
  lcd.setCursor(0, 0);
  if (reading == 0 || reading == 1023) {
    lcd.print("Check sensor    ");
    delay(500); return;
  }
  const double logR = log(10000.0 * (1024.0 / reading - 1.0));
  // Preserve ELEGOO's Steinhart-Hart coefficient assumptions, not a calibration for every 10 kΩ NTC.
  const double kelvin = 1.0 / (0.001129148 + (0.000234125 + 0.0000000876741 * logR * logR) * logR);
  const float celsius = kelvin - 273.15;
  lcd.print("Temp         C  ");  // Clear all 16 columns so an old extra digit cannot remain.
  lcd.setCursor(6, 0);
  lcd.print(celsius);  // Celsius is the supplied active view; F = C*9/5+32 is an optional edit.
  delay(500);
}
