ATmega328PA0+5 Vupper10 kΩlowerNTCLDR1int raw = analogRead(A0);2float r = R0*raw/(1024-raw);3float k = 1/(1/T0+log(r/R0)/B);4float c = k - 273.15;
Back: the chip (pin A0), the 5 V to GND row with two slots, and a voltmeterFront: the four lines of Arduino codeMoving dots: current flowing from 5 V to GNDPin A0 of an Arduino Uno R3 reads a voltage as a number from 0 to 1023. A potentiometer splits 10 kΩ in two, so its wiper voltage follows the knob: 1.5 V reads 307. A thermistor or light cell under a fixed 10 kΩ makes a voltage divider; the code turns the reading back into ohms, then degrees.

Voltage in, number out

  1. Pin A0 has an ADC (analog-to-digital converter) behind it: it measures a voltage from 0 to 5 V and gives a whole number from 0 to 1023. It cannot feel heat or light. A sensor's job is to turn the world into that voltage.

The potentiometer

The thermistor

The light cell

What happens

;; step 1/9 · voltage-only
board     Arduino Uno R3 (ATmega328P, 5 V)
line 1    int raw = analogRead(A0);
ADC       raw = Vin x 1024 / 5 V, top value 1023
one count 4.88 mV
the job   world -> resistance -> voltage -> number

The B equation is a simple fit through 25 °C and 85 °C. With the datasheet's real resistances it gives 0.8 °C at 0 °C, under one degree off. Vishay's datasheet gives a longer formula for better accuracy. The ATmega328P's ADC works best when the source has 10 kΩ or less behind it. A divider with a 10 kΩ resistor on top never has more than 10 kΩ, even with a light cell at 1 MΩ.