This example demonstrates how to measure temperature using a 10k NTC thermistor with a B-parameter of 3950. The thermistor is connected in a voltage divider with a 10kΩ resistor, and the Arduino reads the voltage at the midpoint of the divider. Using the **Steinhart-Hart equation**, the thermistor resistance is converted to a temperature value. The temperature is displayed in all SI units: **Kelvin (K)**, **Celsius (°C)**, and **Fahrenheit (°F)**.
/*
* © 2024 Copyright Peter I. Dunne, all rights reserved.
* Prepared for educational use.
* The ADC is 10 bit, this is of relatively low accuracy, use professional test equipment for accuracy.
* Released under the Mozilla Public License
* Arduino UNO with 10k NTC Thermistor and B-parameter of 3950
* Measures temperature and converts it to Kelvin, Celsius, and Fahrenheit.
*/
const int thermistorPin = A0; // Thermistor connected to A0
const int referenceResistor = 10000; // 10k reference resistor (Ohms)
const float B_coefficient = 3950; // B-parameter of thermistor
const float nominalTemperature = 298.15; // 25°C in Kelvin
const float nominalResistance = 10000; // 10k ohms at 25°C
const float VRef = 5.0; // Arduino reference voltage
const int maxADCValue = 1023; // 10-bit ADC
void setup() {
Serial.begin(115200); // Start serial communication
Serial.println("Arduino NTC temperature measurement, by Peter Ivan Dunne, ©2024, all rights reserved");
Serial.println("Released under the Mozilla Public License");
Serial.println("https://jazenga.com/educational");
}
void loop() {
// Read analog value from the thermistor
int adcValue = analogRead(thermistorPin);
// Convert the ADC value to voltage
float voltage = (adcValue / float(maxADCValue)) * VRef;
// Calculate resistance of the thermistor
float thermistorResistance = referenceResistor * ((VRef / voltage) - 1);
// Steinhart-Hart equation to calculate temperature in Kelvin
float steinhart;
steinhart = thermistorResistance / nominalResistance; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= B_coefficient; // 1/B * ln(R/Ro)
steinhart += 1.0 / nominalTemperature; // + (1/To)
steinhart = 1.0 / steinhart; // Invert to get temperature in Kelvin
// Convert temperature to Celsius
float temperatureC = steinhart - 273.15;
// Convert temperature to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
// Output temperature in Kelvin, Celsius, and Fahrenheit
Serial.print("Temperature: ");
Serial.print(steinhart, 2);
Serial.print(" K, ");
Serial.print(temperatureC, 2);
Serial.print(" °C, ");
Serial.print(temperatureF, 2);
Serial.println(" °F");
delay(1000); // Wait 1 second before next reading
}
thermistorResistance = referenceResistor * ((VRef / voltage) - 1);
steinhart = thermistorResistance / nominalResistance;
steinhart = log(steinhart);
steinhart /= B_coefficient;
steinhart += 1.0 / nominalTemperature;
steinhart = 1.0 / steinhart; // Result in Kelvin
temperatureC = steinhart - 273.15; // Kelvin to Celsius
temperatureF = (temperatureC * 9.0 / 5.0) + 32.0; // Celsius to Fahrenheit