Skip to main content

Activity: Remote Data Logging with a micro:bit (Python)

image of two microbits communicating to log data


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

makecode.microbit.org

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.

  1. Plug in only the transmitter micro:bit.
  2. 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)
  1. 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

radio.set_group(1)

Puts the micro:bit on radio group 1. Both micro:bits must use the same number.

input.light_level()

Reads the light level using the LED panel (0 = dark, 255 = bright).

input.temperature()

Reads the temperature in °C from the processor chip.

radio.send_value("light", ...)

Broadcasts a reading with a label, so the receiver knows what it is.

basic.forever(on_forever)

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.

  1. Plug in only the receiver micro:bit.
  2. 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)
  1. Download the program and keep the USB cable connected.


How the code works

Code

What it does

radio.set_group(1)

Must match the transmitter's group number.

radio.on_received_value(...)

Runs on_received_value each time a labelled value arrives.

if name == "light":

Checks the label to work out which reading has arrived.

serial.write_value("light", light)

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

  1. 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.
  2. Light: point the transmitter's LED panel at a lamp or window, then cover it with your hand.
  3. Temperature: this changes slowly. Hold the transmitter in your hands for a minute to warm it up.
  4. 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

  1. Why do both micro:bits need the same radio group number?
  2. Why does the transmitter send a label ("light", "temp") with each value?
  3. Why did the temperature graph change much more slowly than the light graph?
  4. 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


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

Popular posts from this blog

Programming Anki's Vector robot

With the sad news that Anki is shutting down ( https://www.vox.com/2019/4/29/18522966/anki-robot-cozmo-staff-layoffs-robotics-toys-boris-sofman ) I thought it was time I start playing with the SDK for the Vector robot. In this short post, I providing a quick overview of getting going with this with a simple program based on the tutorials Anki provide. Installation I am using a Mac (more details are available here  https://developer.anki.com/vector/docs/install-macos.html ) but there are instructions for Windows and Linux. - You need the Vector to have been set-up previous on a tablet, and an account set-up on the Anki Cloud. - Install Homebrew - available here  https://brew.sh/ - Using Homebrew to install Python3  brew install python3 -Now install the SDK  python3 - m pip install -- user anki_vector - Lst but not least configure the set-up  python3 - m anki_vector . configure - that is it. The configuration tool tells you where the serial...

Speech Recognition in Scratch 3 - turning Hello into Bonjour!

The Raspberry Pi Foundation recently released a programming activity Alien Language , with support Dale from Machine Learning for Kids , that is a brilliant use of Scratch 3 - Speech Recognition to control a sprite in an alien language. Do the activity, and it is very much worth doing, and it will make sense! I  would also recommend going to the  machinelearningforkids.co.uk   site anyway it is full of exciting things to do (for example loads of activities  https://machinelearningforkids.co.uk/#!/worksheets  ) . Scratch 3 has lots of extensions that are accessible through the Extension button in the Scratch 3 editor (see below) which add new fun new blocks to play with. The critical thing for this post is  Machine Learning for Kids  have created a Scratch 3 template with their own extensions for Scratch 3 within it  https://machinelearningforkids.co.uk/scratch3/ . One of which is a Speech to Text extension (see below). You must use this one ...

Scratch and web-cams in Scratch 3

Scratch 3 was launched on 2nd January 2019, so I wanted to know would Webcams still work with Scratch 3 as it did with Scratch 2. For example, in a previous post  Scratch, webcams, cats and explosions  the cat (Scratch) moved across the screen and a button burst when the object moved in the camera onto it.  Can the same thing be done in Scratch 3? The short answer is yes, but it is done slightly differently. The first change the video capture is not there in the blocks automatically; but is an extension that needs to be added. First, you need to add the extension blocks for video sensing. Go to the little icon at the bottom left of the screen (as shown below) this takes you to the extensions menu. Next, find the Video Sensing option and selected. The webcam, if enabled, with start automatically. A video sensing set of blocks is now in the list of block options.  The rest is very similar to doing this in Scratch 2. Moving ...