Analog read turns the voltage on a pin, 0 to 3.3 V, into a number from 0 to 1023. A knob or a light sensor in a divider changes that voltage, so the number changes. A program compares the number with a threshold to decide, as a night light does.
A button is on or off. But a room isn't just light or dark, and a volume knob isn't just loud or silent. How does a micro:bit measure something in between?
A digital input is 0 or 1. An analog input can be anything in between. Analog read pin P0 gives a number from 0 to 1023. A knob (a potentiometer) is a resistor with a slider: turning it moves the slider between 3V and GND, so the voltage on pin 0 changes.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Halfway
TryTry it on the board and compare the result with your prediction.
Try: turn the knob
Try it yourselfYour edits are saved in this browser.
Turn the knob halfway. What does pin 0 read?
Use the board. This ticks when you get there.
Turn it all the way up.
Use the board. This ticks when you get there.
InvestigateWork out why it happens, one step at a time.
Worked example: number and volts
Worked exampleBefore each step, say what you think comes next.
position=50%=0.5The knob is halfway.
reading=0.5×1023≈512Analog read scales 0 to 3.3 V onto 0 to 1023.
volts=1023512×3.3≈1.65 VBack to volts: the fraction of the scale, times 3.3.
1023→3.3 V,0→0 VThe ends of the scale.
Now a quarter of the way up.
The knob is a quarter of the way up. What does analog read give? (round to a whole number)
Pin 0 reads 818. About how many volts is that? (2 decimal places)
Sam is worried about the knob. Click the line where Sam's reasoning first goes wrong.
ModifyChange the problem on purpose and see what happens.
Vary: a light sensor
Try it yourselfYour edits are saved in this browser.
Cover the sensor: make it dark. What happens to the reading?
Use the board. This ticks when you get there.
Find the light level (%) where the reading passes 300.
Modify TypicalChange the problem on purpose and see what happens.
Build it in Tinkercad
Tinkercad's photoresistor is not linear like the board's model: expect the numbers to bunch up at one end.
PractiseA few on your own, to check you have it.
Readings
3.3 V on pin 0 reads 1023. What does 0.66 V read? (whole number)
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
A night light
Then build it in Tinkercad with an LED on another pin, or on a real micro:bit.
Try it yourselfYour edits are saved in this browser.
Need a hint?
Work out the reading at 20% light and at 40% light.
Another hint
20% reads about 205 and 40% about 409.
Another hint
Set the threshold between them (such as 300), then turn the light down to check the lights come on.
Set the threshold so the lights come on when the light drops to 20%, but stay off at 40%. Then test it by making it dark.
Use the board. This ticks when you get there.
For teachers
Students turn a knob on pin 0 and see analog read give 0 to 1023, convert between position, reading and volts, swap in a light sensor in a divider, and set a threshold for a night light. About 45 minutes.
I can choose a threshold so a program decides at the right moment.
Before class
Model: analog read is 0–1023 for 0–3.3 V (the micro:bit's 10-bit reading); the knob and the light sensor give a voltage in proportion to the position or the light. Real light sensors (photoresistors) are far from linear, which Tinkercad will show.
Links: Digital systems › binary (10 bits make 1024 patterns, 0 to 1023).
Running the lesson
Predict: ask those who chose 1.65 what unit analog read gives.
Worked example: draw the three scales one above the other.
Vary, swap them: draw both dividers and ask which end the sensor is on.
Make: compare thresholds. Who picked one close to 205, and what could go wrong?
The hard part (on purpose)
Three scales at once (percent, reading, volts) blur together. Keep a number line on the board with all three marked.
When students get stuck
A student reports volts as the reading. Ask: What is the biggest number analog read can give? Fix: 1023, for 3.3 V.
A student's threshold works backwards. Ask: Does darker make the reading bigger or smaller, in this circuit? Fix: Smaller here: lights on below the threshold.
Stretch and support
Stretch: Why 1023? How many bits does the micro:bit use for an analog reading?
Support: A printed number line with 0, 512 and 1023 above 0 V, 1.65 V and 3.3 V.
Where it leads
Next: the Arduino unit (the same circuits on an Uno: analogRead gives 0 to 1023 there too).
Digital systems: How many patterns? (10 bits). Code: micro:bit Foundations sensors (light level 0–255 is a different scale).
halfway: Committing to the reading at halfway tests the new move: analog read maps 0–3.3 V onto 0–1023. Each wrong option is a real slip: the volts, a percentage, a digital 1.
Try: turn the knob: Turning the knob settles the prediction and shows both ends of the scale.
Worked example: number and volts: Converting between position, reading and volts models the move; Sam's 'the number is volts' is planted after the method.
Vary: a light sensor: Swapping the knob for a light sensor in a divider shows the same move with a real-world input: light changes the voltage, so the number.
Build it in Tinkercad: Tinkercad's photoresistor and potentiometer, read with analog read and shown on the micro:bit, check the idea in a fuller simulator.
readings: Readings, volts and percentages both ways, and a threshold decision.
a night light: Setting a threshold so the lights switch on at the right darkness reverses the process: from a behaviour to a number.
Now you can
Explain what analog read gives.
Convert between a reading and volts.
Choose a threshold so a program decides at the right moment.