#include <math.h>
// Define constants
const float BETA = 3950; // Beta coefficient of the thermistor
#define ADC_CHANNEL 0 // ADC channel to which the thermistor is connected
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure the ADC
ADCSRA |= (1 << ADEN); // Enable the ADC
ADCSRA |= (1 << ADPS2) | (1 << ADPS1); // Set prescaler to 64 for ~250kHz ADC clock
ADMUX = (1 << REFS0); // Use AVcc as the reference voltage (5V)
}
uint16_t readADC(uint8_t channel) {
// Select ADC channel (MUX bits in ADMUX register)
ADMUX = (ADMUX & 0xF0) | (channel & 0x0F);
// Start the conversion
ADCSRA |= (1 << ADSC);
// Wait for the conversion to complete (ADIF flag set)
while (!(ADCSRA & (1 << ADIF)));
// Clear the ADIF flag by writing 1 to it
ADCSRA |= (1 << ADIF);
// Return the ADC value
return ADC;
}
void loop() {
// Read ADC value from channel 0
uint16_t analogValue = readADC(ADC_CHANNEL);
// Convert ADC value to temperature in Celsius using the Steinhart-Hart equation
float resistance = 10000.0 / (1023.0 / analogValue - 1.0); // Assuming 10k fixed resistor
float celsius = 1.0 / (log(resistance / 10000.0) / BETA + 1.0 / 298.15) - 273.15;
// Print the temperature
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
// Delay for 1 second
delay(1000);
}