Wire a potentiometer between 5 V and ground, run its middle pin to A0, and your sketch can call analogRead(A0) and get back a number from 0 to 1023. Turn the knob; the number changes smoothly. That tidy result hides two beautiful pieces of physics and one piece of arithmetic that took a Nobel laureate to figure out.

Step 1: a divider, not a magic line

The potentiometer isn't reporting its angle to the chip. It can't — there's no protocol, no wire telling the microcontroller "I am at twelve o'clock." What it does is much simpler: it's a 10 kΩ resistor with a sliding contact (the wiper) that taps off some fraction of the resistor's length.

With 5 V on top and 0 V (ground) on the bottom, the wiper sees a voltage that depends linearly on its position. Halfway up the resistor, it sees 2.5 V. A quarter of the way, 1.25 V. The chip never "knows" where the knob is. It just measures the voltage at the wiper.

Step 2: turning volts into bits

Voltage is analog: it can take any value at all between 0 and 5. But registers store integers. To bridge the two, the Atmega328P has a circuit called a successive approximation ADC — Analog-to-Digital Converter — and it works by binary search.

The ADC asks: is the wiper's voltage above 2.5 V? If yes, write a 1 in the highest bit; if no, write 0. Then: above 3.75 V (or 1.25 V)? Write the next bit. Then 0.625 V steps. Then 0.3125. Ten bits, ten guesses, each halving the uncertainty. After about 100 microseconds the ADC has bracketed the wiper's voltage to within one part in 1024 — a resolution of roughly 4.88 millivolts.

So when analogRead returns 512, the wiper is at 512/1023 × 5 V ≈ 2.50 V. When it returns 0, the wiper is at ground. When it returns 1023, the wiper is at the rail. Smooth, continuous, real-world voltage becomes a 10-bit integer.

Step 3: caveats nobody warns you about

The successive-approximation ADC has a small input capacitor that has to charge to the wiper's voltage before sampling. If your source impedance is too high — say, you're reading a 100 kΩ thermistor through a 100 kΩ pull-up — the capacitor charges sluggishly and your reading is wrong. The datasheet recommends source impedance under 10 kΩ. This is why your sensor circuit might "almost work."

That number that comes back from analogRead is the end of a story that starts with a knob and a battery, runs through Ohm's law, and finishes with binary search in silicon. Once you see it that way, you stop being surprised by sensors. They're all the same trick.