/**********************************************
This code defines values for a thermistor's resistance,
reference resistance, B-coefficient, and a known temperature
in Celsius. It then reads an analog value from a sensor
(presumably connected to a thermistor) and calculates the temperature
in Celsius using the Steinhart-Hart equation.
The temperature is displayed on an LCD2004 I2C display
and printed to the serial monitor with a delay,
allowing you to monitor temperature readings. This code is
suitable for interfacing with a thermistor to measure and
display temperature in Celsius.
custonchip created by arvind patil 14/10/23
******************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27 for a 20x4 LCD display
// Define the thermistor resistance and reference resistance values
const float thermistorResistance = 10000.0; // Resistance of the thermistor (in ohms)
const float referenceResistance = 10000.0; // Reference resistance (in ohms) at a known temperature
// Define the B-coefficient for the thermistor
const float betaCoefficient = 3950.0; // Beta coefficient of the thermistor
// Define the temperature at a known resistance (usually room temperature)
const float knownTemperatureCelsius = 25.0; // Known temperature in Celsius
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print("Celsius:");
lcd.setCursor(0,2 );
lcd.print("customchip ky-13");
lcd.setCursor(0,3);
lcd.print("by arvind 14/10/23");
pinMode(A2, INPUT);
}
void loop() {
// Read the analog value from the sensor
int analogValue = analogRead(A2);
// Calculate the resistance of the thermistor
float resistance = referenceResistance / (1023.0 / analogValue - 1.0);
// Calculate the temperature in Celsius using the Steinhart-Hart equation
float steinhart = resistance / thermistorResistance; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= betaCoefficient; // 1/B * ln(R/Ro)
steinhart += 1.0 / (knownTemperatureCelsius + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
float temperatureCelsius = steinhart - 273.15;
// Print the temperature in Celsius to the LCD display
lcd.setCursor(11, 1);
lcd.print(" "); // Clear previous temperature
lcd.setCursor(11, 1);
lcd.print(temperatureCelsius);
// Print the temperature in Celsius to the serial monitor
Serial.print("Temperature (Celsius): ");
Serial.println(temperatureCelsius);
delay(5000); // You can adjust the delay as needed
}