#include <Arduino.h>
#include <math.h>
// Define constants
const float B = 3540; // Beta value
const float T0 = 25; // Reference temperature (in Celsius)
const float R0 = 10000; // Reference resistance (in ohms)
const int adcPin = 34; // Analog pin connected to thermistor
// Function to calculate temperature from resistance
float calculateTemperature(float R) {
return 1.0 / (B * log(R / R0)) + 273.15; // Convert temperature to Kelvin
}
void setup() {
Serial.begin(9600);
pinMode(adcPin, INPUT); // Set ADC pin as input
}
void loop() {
// Read ADC value
int adcValue = analogRead(adcPin);
// Debug: Print raw ADC value
Serial.print("ADC Value: ");
Serial.println(adcValue);
// Convert ADC value to voltage
float voltage = adcValue * (3.3 / 4095.0); // Assuming 3.3V reference voltage and 12-bit ADC
// Debug: Print calculated voltage
Serial.print("Voltage (V): ");
Serial.println(voltage, 4); // Print with 4 decimal places
// Calculate resistance using voltage divider formula
float R = (3.3 * R0) / (voltage) - R0; // Calculate thermistor resistance
// Debug: Print calculated resistance
Serial.print("Resistance (Ohms): ");
Serial.println(R);
// Calculate temperature
float temperature = calculateTemperature(R);
// Print temperature
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
delay(2000); // Delay for demonstration purposes
}