← micro:bit Foundations

Sensors are numbers

Read light, temperature and tilt as numbers, and show them.

A phone dims its screen in the dark and turns the picture when you tilt it. Inside, a sensor is just handing the phone a number.

The micro:bit has sensors: parts that measure the world around it, such as how bright it is or how warm. Each sensor gives the program a number. You find them in Input.

In the simulator, sensors appear as controls when your program uses them: drag the light slider (the sun), drag the temperature slider (the thermometer), or move the mouse over the micro:bit to tilt it.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    basic.showNumber(input.lightLevel())
})

light level is a sensor block. It is inside forever.

What will the micro:bit show?

RunRun it and compare the result with your prediction.

Run

Drag the light slider (the sun on the simulator) up and down. Try the darkest and brightest settings.

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    basic.showNumber(input.lightLevel())
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

This program keeps each reading in a variable called reading, then shows it. The world changes the light; forever reads it again and again.

The program as blocks
Show as code
lesson.ts
let reading = 0
basic.forever(function () {
    reading = input.lightLevel()
    basic.showNumber(reading)
})

Match each sensor with its number.

Find the top of the light scale.

Sam's program:

The program as blocks
Show as code
basic.showNumber(input.temperature())

Match each sensor with what its number means.

light level
temperature
acceleration x

Drag the light slider all the way up. What is the biggest number the light level can be?

Sam's program shows the temperature. He drags the temperature slider, but the number never changes. Why?

ModifyChange the code on purpose and check the result.

Modify

Show the temperature instead of the light level. Drag the temperature slider to test it.

Need a hint?

temperature is in Input. Drag it into show number, in place of light level.

Read a sensor only when asked: A shows the light level, B shows the temperature.

Need a hint?

Use on button A pressed and on button B pressed from Input.

Another hint

Put show number inside each one, with a different sensor in each.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Show the temperature instead of the light level.

This ticks by itself when your blocks do what is asked.

A shows the light level, B shows the temperature.

This ticks by itself when your blocks do what is asked.

InvestigateTrace how each line of code connects to what you see.

Mix it up

A points counter, no sensors in sight.

The program as blocks
Show as code
lesson.ts
let points = 0
input.onButtonPressed(Button.A, function () {
    points += 2
    basic.showNumber(points)
})
input.onButtonPressed(Button.B, function () {
    points = 0
    basic.showNumber(points)
})

A changes points by 2 and B sets it to 0. Fill in points after each press.

presspoints after
(start)0
A
A
B
A

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make a thermometer. Again and again, it shows the temperature, then the letter C (for Celsius), then waits two seconds.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

Inside forever: show the temperature, then show the letter C, then pause.

Another hint

show string is in Basic. Type C into it. 2000 ms is two seconds.

A thermometer: show the temperature and a C, then wait, again and again.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Change a light meter

This light meter draws a bar instead of a number: the brighter it is, the more LEDs light up. plot bar graph needs the biggest value the sensor can give (255 for light). Change it into a temperature meter, where 40 is the biggest value.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
For teachers

Students read light, temperature and tilt as numbers, and see that a sensor read in forever follows the world while one read in on start gives a single moment. About 40 minutes.

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

Students will be able to

Before class

A sensor gives the program a number that the world keeps changing. In the simulator the controls appear only once a program uses a sensor: the light slider (sun), the temperature slider (thermometer), and tilt by moving the mouse over the micro:bit. The key idea is the forever loop from lesson 2: a sensor read in forever is read again and again; in on start, once.

Running the lesson

  1. Predict: ask whether the number will follow the light. The key is that forever reads it again each time. Ask the 'same number over and over' group what forever does each time round.
  2. Run: drag the light slider from dark to bright. The number lags a little because it scrolls.
  3. Investigate: Sam's thermometer is lesson 2's on start mistake again, in a new place. Ask the class which lesson it reminds them of.
  4. Modify: Read on request shows that an event decides when a sensor is read.
  5. Mix it up: a quick trace of set and change from last lesson.
  6. Make: temperature, C, pause: three blocks in forever.

The hard part (on purpose)

A sensor is read once in on start and many times in forever. When the number never changes, ask where the sensor block is.

When students get stuck

Stretch and support

Stretch: Show tilt as a number with acceleration x, then make A show tilt left and right and B show tilt forward and back (acceleration y).

Support: Give students the thermometer's forever block with show number already in it, and ask them to add the C and the pause.

Where it leads

Next unit: the micro:bit decides what to do using these numbers (if it is dark, light up). Class ideas: measure the brightest and darkest spots in the room with real micro:bits; record the temperature by a window every lesson for a week.

What each step is for

Now you can
Next lesson6. IfMake the micro:bit decide what to do, using a sensor and an if block.