#include #include #include // LCD I2C address and dimensions LiquidCrystal_I2C lcd(0x27, 16, 2); // Pin definitions const int tempSensorPin = 33; // Analog pin connected to the voltage divider const int buttonPin = 12; // Digital pin connected to the button const int ledPin = 14; // Digital pin connected to the LED // Constants for temperature calculation const float Vref = 3.3; // Reference voltage const int ADC_Max = 4095; // Maximum ADC value for ESP32 (12-bit ADC) const float R_fixed = 10000; // Fixed resistor value in ohms const float Beta = 3950.0; // Beta parameter for the NTC thermistor const float T0_ref = 298.15; // Reference temperature (25°C in Kelvin) const float R0 = 10000.0; // NTC resistance at 25°C (10kΩ) void setup() { // Initialize serial communication Serial.begin(115200); // Initialize the LCD lcd.init(); lcd.backlight(); // Initialize the button pin pinMode(buttonPin, INPUT_PULLUP); // Initialize the LED pin pinMode(ledPin, OUTPUT); } void loop() { // Read temperature sensor value int tempValue = analogRead(tempSensorPin); // Avoid division by zero by checking if Vout is close to Vref if (tempValue == 0 || tempValue == ADC_Max) { Serial.println("Invalid sensor reading."); } else { // Convert analog value to voltage float Vout = tempValue * (Vref / ADC_Max); // Calculate the NTC resistance using the Voltage Divider formula float R_NTC = (R_fixed * (Vref - Vout)) / Vout; // Calculate Temperature using the Beta parameter equation float temperatureK = (Beta * T0_ref) / (Beta + (T0_ref * log(R_NTC / R0))); float temperatureC = temperatureK - 273.15; // Convert to Celsius // Print the temperature to Serial Monitor Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" °C"); // Display appropriate message based on temperature on LCD lcd.clear(); if (temperatureC < 15) { lcd.print("Cold"); } else if (temperatureC >= 15 && temperatureC <= 25) { lcd.print("Normal"); } else { lcd.print("Hot"); } // Display the temperature on the second line of the LCD lcd.setCursor(0, 1); lcd.print(temperatureC); lcd.print(" C"); } delay(1000); // Delay 1 second before next reading }