← Circuits with the micro:bit

Sensors that vary

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.

WidthMore contexts, same idea.
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

The knob is turned halfway. Analog read pin P0 gives about…

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.
  1. position=50%=0.5\text{position} = 50\% = 0.5The knob is halfway.

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.

Swap the light sensor and the 10 kΩ resistor round. What happens to the reading as it gets darker?

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)

A plant alarm beeps when a soil sensor reads below 400. Which reading beeps?

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.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

  1. Predict: ask those who chose 1.65 what unit analog read gives.
  2. Worked example: draw the three scales one above the other.
  3. Vary, swap them: draw both dividers and ask which end the sensor is on.
  4. 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

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).

Sources

What each step is for

Now you can
You reached the endBack to Circuits with the micro:bit