Linux Format

Micro:bit walkie-talkies

Using a pair of BBC micro:bit devices, Calvin Robinson creates walkie talkies in Python.

- Calvin Robinson is a former assistant principal and Computer Science teacher with and a degree in Computer Games Design and Programmin­g BSC (Hons). Twitter.com/ Calvinrobi­nson

Calvin Robinson uses a pair of BBC micro:bit devices to code walkie-talkies in Python that can transmit to each other.

The BBC micro:bit is an open-source micro computer. Launched in 2015/16, the BBC gave UK secondary schools the opportunit­y to apply for one free unit for each Year 7 pupil. The idea was that every child could keep their micro:bit for free and develop a taste for coding.

Many readers will remember the original BBC Micro from our school days (1981-1994), an Acorn computer used for programmin­g in BASIC. The micro:bit is its spiritual successor, designed to be cheap, adaptable, and a fraction of the size, ideally to be used alongside a PC or Raspberry Pi. The micro:bit features 25 LEDS, two programmab­le buttons, as well as a non-programmab­le button, an accelerome­ter, magnetomet­er, Bluetooth Smart Technology and five Input/output rings that are ideal for attaching things like motors and buzzers with crocodile clips. The unit is specifical­ly designed for Blocks (a Javascript editor) and Python, although it is also possible to program it with Free Pascal, Simulink, C__, Forth, Lisp, Rust, Ada and Swift. You can even run Zephyr OS on it.

For this tutorial, we’re going to be coding in Python. There are a number of editors available for the micro:bit, including some simulators that will emulate the micro:bit hardware, so owning a unit isn’t a barrier to entry. Visit https://microbit.org/code/ and click ‘Let’s Code’ at the top of the page for access to the Python Editor. The editor Mu is a great native GUI interface

(https://codewith.mu/), whereas uflash and microrepl are command line options. There’s also a mobile app for ios/android from the official micro:bit website. For this tutorial we’ll be using the https://

create.withcode.uk/ as it provides a fancy simulated micro:bit tool to check if our code is functionin­g before exporting it onto the physical device.

We’re going to program some basic walkie-talkie devices that send messages from one to the other. We can send pre-coded images and lines of scrolling text between micro:bits at the press of a button.

The micro:bit supports Bluetooth (BLE) and radio transmissi­on, but BLE isn’t supported by Micropytho­n (the version of Python running on the micro:bit), so for that reason we’ll be using radio implementa­tion to make our micro:bits communicat­e with each other. There’s no limit to the number of micro:bits that can send/receive over radio transmissi­on, other than the usual 2.4GHZ signal traffic congestion. We would therefore recommend experiment­ing with no more than a handful of micro:bits to begin with (two is fine), and perhaps keep them in the same room until the code is fully functionin­g, so you can rule out the possibilit­y of walls or objects interferin­g with the signal.

Coding for the micro:bit

To get started on withcode, we’ll need to import the micro:bit module and the specific functions for displaying images and using the two buttons, labelled ‘a’ and ‘b’: from microbit import display, Image, button_a, button_b

Now, to display text, we can either ‘scroll’ or ‘show’ a string of text. The display function contains two options, scroll and show, which will display the text accordingl­y – either from left to right, or one letter at a time:

display.scroll(“hello”) display.show(“world”)

Displaying images is a similar process. There is a whole host of built-in images that the micro:bit will recognise, including basic emojis:

display.show(image.happy) sleep(1000) display.show(image.sad) sleep(1000)

But to draw your own images we’ll need to highlight precisely which LEDS we’d like lit up, and how bright we’d like them on a scale of 0-9 – 0 being off and 9 being full brightness:

i = Image(“00000:”

“22222:”

“44444:”

“66666:”

“88888”) display.show(i) sleep(1000)

For an easy life, or to save space, we can narrow this down to a single line. The colon remains in place between lines of lights, but we narrow the code down to one pair of speech marks:

LEDS = Image(“00000:11111:22222:33333:44444”) display.show(leds) sleep(1000)

Here we’ve used a slightly brighter LED on each line to highlight the difference in hue.

Since it’s December and we’re feeling a little festive, why not design a Christmas tree for our first image to send to another micro:bit later:

xmastree = Image(“00500:00500:05550:55555:00500:”) display.show(xmastree)

It’s all well and good being able to display our images on the LEDS, but before we’re able to send them to another device we’re going to need to work out the buttons. Here’s a basic counter that can be used as an example of how the buttons function. We’ve used an infinite loop, with a small pause (sleep) so as not to cause a heavy load on the processor.

from microbit import *

counter = 0 new_value = 0

while True:

# decrease by 1 if button A was pressed if button_a.was_pressed():

new_value = counter - 1

# increase by 1 if button B was pressed if button_b.was_pressed(): new_value = counter + 1

# reset to 0 if you touch pin 0 if pin0.is_touched():

new_value = 0

# stop counter from going less than 0 if new_value < 0:

new_value = 0

# scroll new value if it’s changed if new_value != counter: counter = new_value display.scroll(str(counter))

sleep(50)

Setting up the radio

In order to send these images or strings with our buttons, we’ll need to connect the radio. Alter the top of your code to reflect the following:

from microbit import * import radio radio.on()

Now that the radio is turned on, we can send or receive messages over it. However, we might also want to be more specific and set a channel before turning the radio on:

radio.config(channel=10)

Sending messages

Let’s start off by sending and receiving some basic messages. We’ll set our A button to ask for a handle, and the B button can reply with it – a throwback to CB radios, if you will. import radio from microbit import * radio.config(channel=0) radio.on()

while True: if button_a.was_pressed(): display.show(‘a’) radio.send(‘what is your handle?’) if button_b.was_pressed(): display.show(‘b’) radio.send(‘my handle is Calvin’) incoming = radio.receive() if incoming:

display.scroll(incoming) sleep(100)

If you’re using two micro:bits for this tutorial, don’t forget to set a different handle on each device for a real-world example of two-way communicat­ion. If you’re using more than two micro:bits, however, you may need a way to change channels on the fly, while the device is running. While we don’t have any more programmin­g buttons, we do have the accelerome­ter functional­ity. Using the following code, we can shake the micro:bit to randomly change channel between 1 and 10, in iterations of 1:

while True: if accelerome­ter.was_gesture(‘shake’): newchan = random.randrange(1,10,1) display.scroll(str(newchan)) radio.config(channel=newchan) if button_a.was_pressed(): display.show(‘a’) radio.send(‘what’s your handle?’) if button_b.was_pressed(): display.show(‘b’) radio.send(‘my name is Calvin) incoming = radio.receive() if incoming:

display.scroll(incoming) sleep(100)

Of course, we could change these strings to say anything, or swap them out for variables. If we were going to replace them with our Christmas tree, it’d look something like this:

import radio from microbit import * radio.config(channel=0) radio.on()

xmastree = Image(“00500:00500:05550:55555:00500:”)

while True: if button_a.was_pressed(): display.show(‘a’) radio.send(xmastree) if button_b.was_pressed(): display.show(‘b’) radio.send(xmastree)

incoming = radio.receive() if incoming:

display.scroll(incoming) sleep(100)

Our ‘while’ look is constantly looking out for an A or B button press but also for any incoming messages. We’ve set these messages to automatica­lly scroll across our LEDS. It might be beneficial to clear the screen first with display.clear(). You may also decide to display.show()

the message instead of scrolling it, depending on whether you plan to show images or text. We’ve found that text scrolls nicely but images are usually better to just pop up straight away with a display.show(). Again, add a sleep timer to the end of our loop to save processing power.

To take things to the next level, we could introduce a transition – a message to let the user know an incoming message has been received, before showing it. That way, the user has less chance of missing the message, which could easily happen if the text starts scrolling over the LEDS without prior warning.

if incoming: display.show(alertimage) sleep(10) display.scroll(alerttext) sleep(10)

This would require a little setting up, before the ‘while’ loop:

alertimage = Image(“00500:00500:00500:000000:009 00:”) alerttext = “Incoming message:“

If you haven’t got two micro:bit devices, you can test all of your code on the https://create.withcode.uk website, simulating communicat­ion both ways. Provided your code has imported the micro:bit module, turned on the radio and has a loop waiting to receive and display the incoming message, you can send yourself a message with the radio test tool. Make sure you’re using the same channel number (anything between 0 and 100), and send some test data (preferably a simple string). It should scroll across your simulator micro:bit LEDS. You can also press the fake buttons on the micro:bit image and they should function as intended.

When you’re happy with your code, download the HEX file. Plug your micro:bit into your computer with a Micro USB cable; it should automatica­lly mount. Then

simply drag the HEX file onto your micro:bit’s drive space. It should override any previously installed HEX file, since the micro:bit has extremely limited storage capacity and only room for a single HEX file.

As soon as the copying process is complete, the micro:bit will be running your Python code. Either keep the micro:bit plugged in via USB or provide power with a portable battery pack. Then press either the A or B button to begin.

 ??  ??
 ??  ?? Withcode provides a fantastic tool for testing micro:bit code without a micro:bit.
Withcode provides a fantastic tool for testing micro:bit code without a micro:bit.
 ??  ??
 ??  ?? Lighting up your life and LEDS, row by row.
Lighting up your life and LEDS, row by row.
 ??  ?? The Micro Bit on the left has sent a message to the one on the right displaying our primitive Christmas tree.
The Micro Bit on the left has sent a message to the one on the right displaying our primitive Christmas tree.
 ??  ?? As basic as pixel art goes – a 2-bit Christmas tree.
As basic as pixel art goes – a 2-bit Christmas tree.
 ??  ?? Paired with battery packs, these micro:bit walkie-talkies are completely wireless, and actually have a decent range.
Paired with battery packs, these micro:bit walkie-talkies are completely wireless, and actually have a decent range.
 ??  ?? Simulation of two-way communicat­ion over the micro:bit radio.
Simulation of two-way communicat­ion over the micro:bit radio.

Newspapers in English

Newspapers from Australia