← Teacher guide

Sensors that vary

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.

Circuits with the micro:bit · Reading inputs · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 6 min · Worked example 12 min · Vary 10 min · Tinkercad 8 min · Practise and Make 9 min

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

What you will seeA question to askThe fix
A student reports volts as the reading.What is the biggest number analog read can give?1023, for 3.3 V.
A student's threshold works backwards.Does darker make the reading bigger or smaller, in this circuit?Smaller here: lights on below the threshold.

Answers to the checks

  1. halfway: The knob is turned halfway. Analog read pin P0 gives about…
    About 512: halfway between 0 and 1023. The pin has about 1.65 V on it, but the reading is a number on a 0 to 1023 scale.
  2. Halfway: Turn the knob halfway. What does pin 0 read?

    512 (1.65 V): half of the 0 to 1023 scale.
  3. All the way: Turn it all the way up.

    1023, for 3.3 V: the top of the scale.
  4. Halfway, in numbers: The knob is a quarter of the way up. What does analog read give? (round to a whole number)
    256
    0.25 × 1023 = 255.75 ≈ 256.
  5. Number to volts: Pin 0 reads 818. About how many volts is that? (2 decimal places)
    2.64
    818 ÷ 1023 = 0.8, and 0.8 × 3.3 = 2.64 V.
  6. Find the mistake: Sam is worried about the knob. Click the line where Sam's reasoning first goes wrong.
    line 3
    Line 3: 1023 is the top of analog read's scale, not volts. It stands for 3.3 V, the most a micro:bit pin can have.
  7. Dark: Cover the sensor: make it dark. What happens to the reading?

    It falls towards 0. Wired this way (light sensor on the 3V side) less light means less voltage on pin 0.
  8. The 300 mark: Find the light level (%) where the reading passes 300.
    30
    300 ÷ 1023 ≈ 0.29, so about 29–30% light.
  9. Swap them: Swap the light sensor and the 10 kΩ resistor round. What happens to the reading as it gets darker?
    It goes up instead of down
    Which way the divider is wired decides whether brighter means a bigger or a smaller number.
  10. Volts to a reading: 3.3 V on pin 0 reads 1023. What does 0.66 V read? (whole number)
    205
    0.66 ÷ 3.3 = 0.2, and 0.2 × 1023 ≈ 205.
  11. A threshold: A plant alarm beeps when a soil sensor reads below 400. Which reading beeps?
    350
    Only 350 is below the threshold.
  12. a night light: 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.

    A threshold between 205 and 409 does it. Real night lights also leave a gap (hysteresis) so they don't flicker at dusk.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: a light sensor › Dark

Open task: any program that does what the step asks.

Vary: a light sensor › The 300 mark

Open task: any program that does what the step asks.

Vary: a light sensor › Swap them

Open task: any program that does what the step asks.

a night light

Open task: any program that does what the step asks.

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.

Exit ticket

Pin 0 reads 256. Roughly how many volts are on it, and what fraction of the way up is the knob?

About a quarter of the way up, so about 0.83 V (256 ÷ 1023 × 3.3).

What each step is for

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