⚡ Spark Academy53 lessons

Reading the Analog World

The ADC turns voltages into numbers your code can think about — and your old friends the divider and the Schmitt trigger come too.

lesson 2 of 2 in this unit

Builds on: 11.1 Hello, Microcontroller2.2 Voltage Dividers6.1 The Operational Amplifier

The border crossing

Software lives on numbers; the world speaks voltage. The analog-to-digital converter is the border post: it measures a pin’s voltage and reports it as an integer. Inside it is Unit 6 hardware — comparators judging the input against reference levels (many designs literally binary-search with one comparator and a DAC). The Pico’s ADC delivers 12 bits: 0 V → 0, 3.3 V → 4095, about 0.8 mV per step. (MicroPython scales readings to a 16-bit 0–65535 range for convenience.)

code = floor(V_in / V_ref × 2ⁿ)n bits → 2ⁿ levels · finite steps = quantisation, the price of going digital

The staircase in the lab makes the trade visible: between two steps, all input voltages read as the same number. More bits shrink the steps — but noise below one step vanishes either way, which is often a feature.

Sensors are mostly dividers

How do you get a temperature or light level as a voltage? Lesson 2.2’s answer, unchanged: put a sensing resistor — a photoresistor (LDR), a thermistor, a flex sensor — into a voltage divider with a fixed resistor, and the midpoint voltage tracks the physical quantity. Wire that midpoint to an ADC pin and your program knows how bright the room is. Most hobby sensing is exactly this, three components deep.

Deciding without dithering

Now the software must act: “dark enough → light on.” A naive if reading < threshold chatters at dusk for the same reason your comparator chattered in Lesson 6.1 — noise around a single threshold. The cure is the same too, now costing two lines instead of two resistors: hysteresis in software — switch on below one level, off above a higher one. This pattern (read → compare with hysteresis → act) runs your kettle, your thermostat and your car’s cooling fan. Next lesson, it runs your night-light.

Averaging: free extra quality

Take 16 readings and average them and random noise shrinks fourfold — a low-pass filter (5.3) implemented in arithmetic. Digital and analog keep being the same subject wearing different clothes.

⚡ Lab — The Staircase at the Border

An ADC transfer curve you can walk along, plus the night-light decision in live code.

  • At 2 bits, slide the input — four coarse plateaus. At 8 bits the staircase is nearly a ramp.
  • Read the quantisation error meter at each resolution.
  • Sweep slowly through 1.5 V and watch the software Schmitt trigger switch at two different points.
1.80 V
# the night-light decision, in software (Lesson 6.1 in code!)reading = adc.read_u16()          # voltage now: 1.80 Vif reading < DARK - HYST:  led.on()   # below 1.35 Vif reading > DARK + HYST:  led.off()  # above 1.65 V
software Schmitt trigger — slide the input slowly through 1.5 V and note the two different switching points
What an ADC is
a ladder of comparators (6.1) reporting which rung your voltage sits on
Where the voltage comes from
a divider (2.2): sensor on top, resistor below — the analog world's handshake

Check your understanding

Q1. A 12-bit ADC with a 3.3 V reference resolves steps of about…

Q2. To let a microcontroller sense light with an LDR, you typically…

Q3. Quantisation means…

Q4. Software hysteresis (two thresholds) prevents…