20. Thermistors, LDRs & Calibration
Convert resistance changes into calibrated measurements.
Learning outcomes
- Explain NTC and LDR behavior
- Calculate divider resistance
- Convert an NTC reading to temperature
- Calibrate thresholds with hysteresis
Parts and preparation
Uno, 10 k ohm NTC thermistor, 10 k ohm resistor, LDR, LEDs and breadboard.
Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.
Resistive sensors
An NTC thermistors resistance falls as temperature rises. An LDRs resistance usually falls as light rises. Arduino measures their divider voltage, not resistance directly.
Divider calculation
With sensor to 5 V and fixed resistor to GND: Rsensor = Rfixed * (1023.0 / ADC - 1.0). Swapping positions changes the formula and direction.
Beta equation
For an NTC: 1/T = 1/T0 + ln(R/R0)/B, using kelvin. R0 is specified at T0 (often 10 k ohm at 25 C) and B comes from the datasheet.
Calibration and hysteresis
Real sensors and tolerances vary. Record readings at known conditions. Use separate ON and OFF thresholds to prevent rapid output chatter.
Your own library
Once the maths works in a sketch, package it as a reusable library with a .h header and .cpp source file in Documents/Arduino/libraries/. See the course extension: How to create a SimpleThermistor library.
Wiring and safe build sequence

- 5 V -> NTC -> A0 junction -> 10 k ohm -> GND
- Use a separate similar divider on A1 for the LDR
- Drive any fan through the MOSFET circuit from lesson 12
Worked sketch
Download .ino sketch#include <math.h>
const float seriesR = 10000.0;
const float nominalR = 10000.0;
const float nominalK = 25.0 + 273.15;
const float beta = 3950.0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
if (raw <= 0 || raw >= 1023) {
Serial.println("Divider fault");
delay(500);
return;
}
float resistance = seriesR * (1023.0 / raw - 1.0);
float invT = 1.0 / nominalK + log(resistance / nominalR) / beta;
float celsius = 1.0 / invT - 273.15;
Serial.print(celsius, 1);
Serial.println(" C");
delay(500);
}How the code works
- The formula assumes the shown sensor/fixed-resistor order.
- Boundary checks avoid division by zero and identify open/short wiring.
- Use datasheet R0 and beta values for the actual thermistor.
Test and record evidence
Practical evidence checklist
Common faults and checks
- If temperature moves backwards or is unrealistic, check divider order and constants.
- Do not heat a thermistor with a flame or high-power source.
Check your understanding
Q1. Why use a voltage divider?
Show answer
The ADC measures voltage, while the sensor changes resistance.
Q2. What is hysteresis?
Show answer
Different switch-on and switch-off thresholds that prevent chatter.