Skip to main content

22 May 2022

Using the micro:bit's Radio functionality for Datalogging

CAS logo
Written by

Computing at School

So far in this series of blogs posts about using the micro:bit to datalog, we have used a single micro:bit to log, display and record data. The last post looked  at how the micro:bit V2 can be used to log data remotely. The same can be achieved with micro:bit V1 , by using two microbits , connected via radio and transmitting data between them.

Firstly you will need two micro:bits, one will act as the transmitter of the data, collecting the data via it's sensors. The second will be the receiver, receiving the data and sending it to a computer for  real time display and storage into a  CSV file.

Setting up the transmitter

The first thing to do is code a micro:bit to act as the transmitter, this micro:bit will collect the data and send it to the other micro:bit using radio signals.

Download the code here >

In the on start command block add the radio set group command found in the Radio commands list. You need to set this to a number that is the same for both the reciever and transmitter. This is because If you haven’t set a radio group for the micro:bit, it will use one selected randomly. To be certain that your program will send or receive data using the same radio group, you will need to first choose and set a radio group for your program if you want it to work between different micro:bits. Also in a classroom situation if students do not set a group number for their own microbits, data will sent to all microbits, chaos will ensue. Students can choose a group number from 0-255.

This is a great example to get students to think about networking and how computers communication with one another. You can find out a lot more with this great free resource from Nominet , Networking with the micro:bit. 

The other commands here are just to show the code is functioning.

The next command event takes a light and temperature reading every minute and sends the value as a data packet via radio. You could use a forever loop here, but I found that I got errors in the data as was trying to read and send the data too quickly. With measurements like light and temperature, they will change slowly. So a measurement every minute (or longer) is adequate. Also, it will save on battery power. The radio send value command captures the value generated by the sensor, and attaches a label to it. which can be used to identify the value on the micro:bit receiving the data. Students may want to an led flash image here and a clear screen to indicate when data is sent.

You may want students to increase the transmission range. You can do this by adding the radio transmit power command, to the onstart block, the default value is 6 , and the maximum is 7. This could increase the range between the two micro:bits to 70m. This command can be found in Radio > more

Setting up the receiver

On the second micro:bit set up the code to recieve the data.

Download the code here>

Connect this micro:bit to your computer via USB cable. See this blog post for details

The on start command block is as before. Notice the radio set group command has the same number as the transmitting micro:bit.

The on radio received command waits for a radio signal. When data is received it sends the data via the USB cable to the computer. 

To view and record the data, make sure the reciever micro:bit is attached to the computer. Makecode is open. Click on Show Console Device, which should appear under the micro:bit simulator. (Press the reset button on the back of the micro:bit if it isn't). You should now see the data being logged. Once you have finished logging the data. Download the data as a CSV file and graph in a spreadsheet application (make sure the computer does not go to sleep whilst this takes place otherwise you will lose your data)



Discussion

Please login to post a comment

Pete Dring
23/06/2022 17:57

Thanks Stuart. I do like the new python editor for the micro:bit. I just wish it had a simulator as I think this is really important for making tinkering and debugging easier for students and helping teachers share limited physical resources (or allow students to experiment at home).

Here’s an animation that one of my bottom set Y9s made this week: Create with code - we haven’t got on to radio functionality yet but we’ve enjoyed looking at iteration and the data representation of images within an animation. You can press run (or Ctrl + Enter) to simulate the code even if you haven’t got a micro:bit plugged in.

anon15714985
23/06/2022 16:37

@pdring have a look at the new Python Editor for micro:bit -Python Editor for micro:bit might be a nice transition tool for students from makecode to python to IDE ? Also have a look at this , made by fellow micro:bit champion @aorczyk - My micro:bit just shows what is possible … enjoy

Pete Dring
23/06/2022 11:58

Thanks Stuart - I’ve never tried this using makecode but I was doing it the other day with python with Y10s using Mu which has a really handy live data plotter. It’s a great way to engage with and visualise real life sensor data and practice reading that data in from a file.
Thanks for sharing.