In the last 60 minute activity the micro:bit became a magnetism data logger, but it had to stay tethered to the computer by its USB cable. This time we cut the cable. One micro:bit goes off on battery power to take the measurements, and a second micro:bit plugged into the computer catches the readings by radio and logs them. It works on a V1 micro:bit too, so it is a good use for those older boards sitting in the cupboard.
Activity Sheet: Remote Data Logging with a micro:bit (Python)
Overview
In this activity, you split a data logger across two micro:bits. A battery-powered transmitter measures light and temperature and sends the readings by radio. A receiver plugged into the computer picks them up and draws them as live graphs.
Time: about 60 minutes
Learning objectives
By the end of this activity you will be able to:
- Send sensor readings from one micro:bit to another using the built-in radio.
- Use a radio group so that two micro:bits only listen to each other.
- Use labels to tell different readings apart when they arrive.
- Log data from a remote sensor and save it as a CSV file.
What you need
Item | Notes |
|---|---|
2 × BBC micro:bit | V1 or V2. Label them Transmitter and Receiver |
USB cable | For programming, and to keep the receiver connected |
Battery pack | 2 × AAA, for the transmitter |
Computer with a web browser | Chrome or Edge works best for the MakeCode data viewer |
MakeCode editor |
Tip: program the two micro:bits one at a time. Plug in only one, and use a separate MakeCode browser tab for each program.
Part 1: Write the transmitter program (micro:bit 1)
The transmitter reads its light and temperature sensors repeatedly and broadcasts each reading over radio with a label.
- Plug in only the transmitter micro:bit.
- Open a new project in MakeCode called transmitter, switch to the Python tab and type:
radio.set_group(1)
def on_forever():
radio.send_value("light", input.light_level())
radio.send_value("temp", input.temperature())
basic.forever(on_forever)- Download the program, unplug the USB cable and connect the battery pack.
If you prefer blocks, flip to the tab for the blocks.
How the code works
Code | What it does |
|---|---|
| Puts the micro:bit on radio group 1. Both micro:bits must use the same number. |
| Reads the light level using the LED panel (0 = dark, 255 = bright). |
| Reads the temperature in °C from the processor chip. |
| Broadcasts a reading with a label, so the receiver knows what it is. |
| Repeats the readings for as long as the micro:bit has power. |
Class tip: if several pairs work in the same room, give each pair its own group number (1, 2, 3 ...) so their data doesn't get mixed up.
Check your understanding
- What would happen if the transmitter used group 1 and the receiver used group 2?
- Why can we unplug the transmitter (with a battery pack) once the program is downloaded?
Part 2: Write the receiver program (micro:bit 2)
The receiver stays plugged into the computer. It listens for radio messages, checks each label, and passes the value on to the computer over USB.
- Plug in only the receiver micro:bit.
- Open a new browser tab, start a MakeCode project called receiver, switch to Python and type:
radio.set_group(1)
light = 0
temp = 0
def on_received_value(name, value):
global light, temp
if name == "light":
light = value
serial.write_value("light", light)
elif name == "temp":
temp = value
serial.write_value("temp", temp)
radio.on_received_value(on_received_value)- Download the program and keep the USB cable connected.
How the code works
Code | What it does |
|---|---|
| Must match the transmitter's group number. |
| Runs |
| Checks the label to work out which reading has arrived. |
| Sends the reading to the computer for the data viewer, just like in the magnetism activity. |
Part 3: Log the data and run the experiment
- With the transmitter on battery power and the receiver plugged in, click Show data Device in the receiver's MakeCode tab. Two graphs appear: light and temp.
- Light: point the transmitter's LED panel at a lamp or window, then cover it with your hand.
- Temperature: this changes slowly. Hold the transmitter in your hands for a minute to warm it up.
- Click the blue download icon in the data viewer to save the readings as a CSV file.
Record your results
Part 4: Think about it
- Why do both micro:bits need the same radio group number?
- Why does the transmitter send a label ("light", "temp") with each value?
- Why did the temperature graph change much more slowly than the light graph?
- Name a place where remote data logging would be more useful than a micro:bit wired to a computer.
Extension challenges
Challenge 1: Show it's sending. Make the transmitter flash an LED each time it sends, so you can see it is working without a computer.
Challenge 2: Slow it down. A forever loop sends readings as fast as it can, which makes the graphs crowded. Add a pause so readings go every second (then try 5 seconds):
def on_forever():
radio.send_value("light", input.light_level())
radio.send_value("temp", input.temperature())
led.toggle(2, 2)
basic.pause(1000)Challenge 3: Go remote with magnetism. Combine this with the last activity: send input.magnetic_force(Dimension.X) and input.magnetic_force(Dimension.Y) from the transmitter, and add matching labels to the receiver.
Challenge 4: Analyse your data. Open the CSV file in a spreadsheet and make a chart of light or temperature over time.
Challenge 5: Out of sight. Put the transmitter in another room or outside. How far away can it be before the data stops?
Troubleshooting
Problem | Try this |
|---|---|
No data arrives | Check both micro:bits use the same group number and the battery pack is switched on. |
Data from another group appears | Pick a different group number on both micro:bits. |
Code downloads to the wrong micro:bit | Plug in only one micro:bit at a time. |
No Show data Device button | Check the receiver is connected by USB and use Chrome or Edge. |
Graphs are very crowded | Add a pause to the transmitter (Challenge 2). |
Further reading
- Original post: Remote data logging with V1 micro:bit
- Previous activity: 60 minute activity: Detecting and Logging Magnetism with a micro:bit (Python)
All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon


Comments
Post a Comment