The physical world does not come in HIGH and LOW. Temperature, light, pressure and position all vary continuously, and to act on them a microcontroller must convert a voltage into a number. That conversion is done by an analog-to-digital converter, and understanding what it can and cannot represent is the whole of this topic.
analogRead() returns 0 at 0 V and 1023 at 5 V, and each step is 5 ÷ 1023 ≈ 4.9 mV. Any voltage difference smaller than that is invisible to the program.| Arduino C | What it does |
|---|---|
int raw = analogRead(A0); | Read pin A0 as a value from 0 to 1023 |
float volts = raw * 5.0 / 1023.0; | Convert to volts — note the .0 to force float arithmetic |
int pct = map(raw, 0, 1023, 0, 100); | Rescale a reading into a different range |
int safe = constrain(pct, 0, 100); | Clamp a value so it cannot go outside the range |
raw * 5 / 1023 in C uses integer arithmetic and gives 0 for every reading below 205. Writing 5.0 and 1023.0 forces floating-point division. This produces no error message at all — just a program that reports zero volts forever.How a sensor becomes a voltage
Most simple sensors are variable resistors: a light-dependent resistor (LDR) has low resistance in bright light, a thermistor's resistance falls as it warms, a force sensor's resistance drops under pressure. But an analog pin measures voltage, not resistance. The voltage divider from the electronics unit is what bridges the gap — put the sensor and a fixed resistor in series across 5 V, and read the junction between them.
Choosing the fixed resistor matters more than it looks. The divider is most sensitive when the fixed resistor is near the middle of the sensor's working resistance range. An LDR that swings between 1 kΩ in bright light and 100 kΩ in darkness pairs well with a 10 kΩ fixed resistor: the output moves across most of the 0–5 V range. Pair it with a 1 MΩ resistor instead and the output barely moves at all.
| Sensor | What it measures | Output type |
|---|---|---|
| Potentiometer | Knob position | Already a divider — the wiper gives 0–5 V directly |
| LDR (photoresistor) | Light level | Variable resistance — needs a divider |
| Thermistor | Temperature | Variable resistance — needs a divider |
| TMP36 | Temperature | Outputs a voltage directly: 10 mV per °C, with a 500 mV offset |
| Ultrasonic (HC-SR04) | Distance | Digital — a pulse whose width encodes the time of flight |
| Push button | Pressed or not | Digital — use digitalRead |
Calibration
The number a sensor gives you is rarely the number you want. Calibration is the step of finding the relationship between raw readings and real units. The practical method is to record the raw reading at two known conditions, then use map() or a straight-line equation to convert between them. A TMP36 is easier because the datasheet gives you the relationship directly: the output is 10 mV per degree with a 500 mV offset, so °C = (millivolts − 500) ÷ 10.