Overview
Ever wondered how technology can help us understand the world around us?
In this project, you are going to become an environment detective using your micro:bitThe micro:bit is a tiny programmable computer, about the size of a matchbox, made for learning to code. It has two buttons, a grid of little red LED lights and built-in sensors that can detect movement, light and temperature.Full entry → and its built-in sensors.
Just like your senses help you explore the world, the micro:bit can tell you:
- How warm or cold it is using its Temperature sensorA temperature sensor is a little part that measures how warm or cold it is around it, then turns that into a number your code can read. As the room heats up or cools down, the number changes too.Full entry →
- How bright or dark it is using its Light SensorA light sensor is a little part that measures how bright or dark it is around it, then turns that into a number your code can read. As the light changes, so does the number.Full entry →
- Which direction you are facing using its CompassA compass senses which way you are facing and turns it into a number your code can read, from 0 all the way round to 359. North is 0, east is 90, south is 180 and west is 270.Full entry →
You will make the readings appear on the micro:bit’s LED MatrixThe LED matrix is the grid of little red lights on the front of the micro:bit. By switching them on and off in patterns, your code can show numbers, letters and tiny pictures.Full entry →.
What you'll learn
- Read the temperature using the micro:bit’s temperature sensor
- Measure how bright or dark it is
- Find out which direction you are facing
- Show numbers, letters and a bar graph on the LED matrix
- Use a variable and if blocks to let your code make decisions
Are you ready to start exploring? Let’s begin our detective adventure!
What you'll need
- micro:bit
- Microsoft MakeCode or the micro:bit app
- Micro USB cable
- Battery pack (optional)
Key words
micro:bit
The micro:bit is a tiny programmable computer, about the size of a matchbox, made for learning to code. It has two buttons, a grid of little red LED lights and built-in sensors that can detect movement, light and temperature.
Real-world example: A bit like a pocket-sized computer you can program yourself, a micro:bit can become a step counter, a thermometer or even a simple game.
Temperature sensor
A temperature sensor is a little part that measures how warm or cold it is around it, then turns that into a number your code can read. As the room heats up or cools down, the number changes too.
Real-world example: Like the thermometer that tells you if you've got a temperature when you're poorly, a temperature sensor measures the warmth around it and gives your code a number to work with.
Light Sensor
A light sensor is a little part that measures how bright or dark it is around it, then turns that into a number your code can read. As the light changes, so does the number.
Real-world example: Like the automatic headlights on a car that switch on when it gets dark, a light sensor notices the light dropping and lets your code react.
Compass
A compass senses which way you are facing and turns it into a number your code can read, from 0 all the way round to 359. North is 0, east is 90, south is 180 and west is 270.
Real-world example: Like the little arrow on a map app that swings round as you turn, a compass tells your code which way you are pointing.
LED Matrix
The LED matrix is the grid of little red lights on the front of the micro:bit. By switching them on and off in patterns, your code can show numbers, letters and tiny pictures.
Real-world example: Like a mini scoreboard made of dots, the LED matrix lights up to spell out your step count.
Variable
A variable is something that can change. Think of it like a labelled box: you pop something inside, and the label tells you what it is. You can swap what's inside whenever you like.
Real-world example: A variable called score might start at 0, then change to 1, 2 and 3 as you collect points in a game.
If statement
An if statement checks whether something is true, and only runs its instructions when it is. It's how code makes decisions.
Real-world example: "If it's cold, put on a coat." If it's warm, you skip the coat, the action only happens when the check is true.
Comparison Operators
A comparison operator checks how two things measure up, for example whether they are equal or one is bigger than the other. It gives back a simple true or false answer.
Real-world example: Asking "is 6 == 6?" gives back true, but "is 6 == 2?" gives back false, so your code knows whether the two numbers match.
Set up MakeCode
You can complete this project on a computer, tablet or phone.
Using a computer
- Open your favourite web browser. Google Chrome or Microsoft Edge works well.
- Go to makecode.microbit.org.
- Select New Project.
- Name your project Environment Detective.
- Select Create.
Using a tablet or phone
- Open the micro:bit app.
- Select Create Code.
- Select New Project.
- Name your project Environment Detective.
- Select Create.
Whichever device you use, your coding area will look similar to this:

Prepare the code area
MakeCode gives you two blocks to begin with, but we do not need them for this project.
Let’s clear the coding area and add the three events that will control our environment detector.
- Drag the
on startblock over to the menu on the left and drop it into the bin. - Delete the
foreverblock in the same way. - Open Input.
- Drag an
on shakeblock into the coding area. - Open Input again.
- Drag an
on button A pressedblock into the coding area. - Right-click the
on button A pressedblock, or press and hold it on a touchscreen. - Select Duplicate.
- On the copied block, change A to B.
You should now have three event blocks:
on shakeon button A pressedon button B pressed

What is an event?
An event is something that makes part of your code run. In this project, shaking the micro:bit or pressing one of its buttons will trigger an event.
Sense the temperature
First, we will make the micro:bit show the temperature when you shake it.
- Open Basic.
- Drag a
show numberblock into theon shakeblock. - Open Input.
- Find the
temperatureblock. - Drag it onto the
0inside theshow numberblock.
Your code should look like this:

Here’s what this bit of code is doing:
- The micro:bit waits until it detects a shake.
- It reads the temperature sensor.
- It shows the temperature as a number on the LED matrix.
The micro:bit displays temperature in degrees Celsius.
Sense the light level
Next, we will use button A to measure the light around the micro:bit.
- Open LED.
- Drag a
plot bar graph ofblock into theon button A pressedblock. - Open Input.
- Find the
light levelblock. - Drag it onto the first
0in theplot bar graph ofblock. - Change the number after up to from
0to255.
Your code should look like this:

When you press button A, the LED matrix will draw a bar graph showing the light level.
- In a dark place, fewer LEDs will light up.
- In a bright place, more LEDs will light up.
Try covering the micro:bit with your hand, then shine a torch towards it. Press button A after each test and watch how the graph changes.
Build the compass code
Now we will use button B to read the compass.
Let’s begin with the easy version, which shows the compass direction as a number.
- Open Basic.
- Drag a
show numberblock into theon button B pressedblock. - Open Input.
- Find the
compass headingblock. - Drag it onto the
0in theshow numberblock.
Your code should look like this:

When you press button B, the micro:bit will display a number between 0 and 359.
The number tells you which direction the micro:bit is pointing:
| Direction | Compass heading |
|---|---|
| North | 0 |
| East | 90 |
| South | 180 |
| West | 270 |
The compass works like a circle. After 359 degrees, it returns to 0 degrees.
Show compass letters
Numbers are useful, but letters are easier to recognise quickly.
We will now replace the number with N, E, S or W.
Create the direction variable
A VariableA variable is something that can change. Think of it like a labelled box: you pop something inside, and the label tells you what it is. You can swap what's inside whenever you like.Full entry → is like a labelled box that stores information.
- Remove the
show number compass headingblock fromon button B pressed. - Open Variables.
- Select Make a Variable.
- Name the variable
direction. - Drag
set direction to 0into theon button B pressedblock. - Open Input.
- Drag
compass headingonto the0.
Your block should now say:
set direction to compass heading
Add the north check
- Open Logic.
- Drag an
if true then elseblock underneathset direction to compass heading. - Open Logic again.
- Drag an
orblock ontotrue. - Add a comparison block to each side of
or. - Build these two comparisons:
direction > 315direction < 45
- Open Basic.
- Put a
show stringblock inside the firstifsection. - Change the text from
Hello!toN.
This checks whether the direction is near the top of the compass circle.
Add east
- Select the + underneath the
elsesection to add anelse if. - Add a comparison that says
direction < 135. - Put a
show stringblock inside it. - Change its text to
E.
Add south
- Select the + again to add another
else if. - Add a comparison that says
direction < 225. - Put a
show stringblock inside it. - Change its text to
S.
Add west
- Put a
show stringblock inside the finalelsesection. - Change its text to
W.
Your finished compass code should look like this:

The code uses If statementAn if statement checks whether something is true, and only runs its instructions when it is. It's how code makes decisions.Full entry → and Comparison OperatorsA comparison operator checks how two things measure up, for example whether they are equal or one is bigger than the other. It gives back a simple true or false answer.Full entry → to decide which letter to show.
How your code decides
Imagine the compass is a pizza cut into four slices. The code checks which slice the compass number has landed in, then shows the letter for that direction.
Download your code
Your environment detector is ready to transfer to the micro:bit.
Using a computer
- Connect your micro:bit to the computer with the micro USB cable.
- In MakeCode, select the three dots next to Download.
- Select Connect Device.
- Follow the instructions on the screen.
- Choose your micro:bit from the list.
- Select Connect.
- Select Download to send the code to the micro:bit.

Using a tablet or phone
- Select Download.
- Follow the instructions on the screen to pair the micro:bit with the app.
- Send the code to your micro:bit.

If the computer cannot find your micro:bit, try another USB cable. Some cables can only charge devices and cannot transfer code.
Test your environment detector
You can test the project using the MakeCode simulator or a real micro:bit.
Test the simulator
The simulator is the pretend micro:bit on the left of the MakeCode screen.

Temperature
- Select the white circle next to Shake.
- Watch the temperature appear on the LED matrix.
- Move the temperature slider and try shaking it again.
Light
- Find the light control above button A on the simulator.
- Drag the yellow section to change the light level.
- Press button A.
- Watch the LED bar graph change.
Compass
- Press button B.
- Watch the compass direction appear.
- Drag the micro:bit logo at the top of the simulator to turn it.
- Press button B again and see whether the letter changes.
Test a real micro:bit
- Temperature: Shake the micro:bit to display the temperature.
- Light: Press button A. Try covering the micro:bit or shining a torch towards it.
- Compass: Turn in a different direction and press button B.
Compass calibration
The first time you use the compass, the micro:bit may ask you to tilt it around until every LED is switched on.
This is called calibrating. It helps the compass work out which direction is north.
Try it yourself
Challenge: Build an environment warning system
Can you make the micro:bit warn you when the room gets too cold?
Add an if block to the on shake code. Make it check whether the temperature is below a number you choose, such as 15.
If it is too cold, make the micro:bit show:
- A sad face
- A snowflake
- Your own warning picture
Can you create another warning for the light sensor? Try showing a picture when the room gets too dark.
Stuck? Quick fixes
| Problem | Try this |
|---|---|
| Nothing happens when I shake the micro:bit | Check that show number temperature is inside the on shake block. Give the micro:bit one firm, clear shake. |
| The temperature looks too warm | This is normal. The sensor is on the micro:bit itself, so it can warm up after the board has been switched on or held in your hand. |
| The light graph is always full or empty | Check that the up to value is 255. Cover the micro:bit with your hand or shine a torch towards it, then press button A again. |
| Button B shows the wrong direction | Complete the compass calibration by tilting the micro:bit until every LED has lit up. |
| The compass always shows W | Check the numbers in your comparisons: 315 and 45 for north, 135 for east and 225 for south. |
| I cannot find a block | Check the correct category: Basic, Input, LED, Logic or Variables. Some blocks may be underneath More. |
| My code will not download | Try another USB cable or USB socket. Check that your micro:bit is paired with MakeCode. |
Don’t worry if it doesn’t work first time. That’s how coding works! Check one block at a time and compare it with the pictures.
