Skip to content

Activity

Proximity beacon

Advanced | MakeCode, Python | LED display, Radio | Arithmetic operators, Communication, Radio waves

Step 1: Make it

What is it?

Use radio to sense how close another micro:bit is and then make a treasure hunt game or use it to help people know they’re at a safe social distance.

Introduction

Coding guide

How it works

  • You need at least 2 micro:bits for this. We’ll create two different programs, one for the beacon which constantly sends a low-power radio message. The other program goes on the receiver.
  • When the receiver picks up a message from the beacon, it stores its strength in a variable called signal and shows it on its LED display.
  • Radio signals get stronger the closer you are to the transmitter, so if the signal is strong it means the other micro:bit is probably close.
  • If the radio signal is weak, the other micro:bit is probably further away.
  • It displays a bar graph which gets bigger the stronger the signal and the closer you are. It uses the maths map block to map radio signal strength numbers from the range -95 (weak) to -42 (strong) to a range 0 to 9 we can use to draw a bar graph.

Python version

  • Python doesn’t have a built-in bar graph or map function, so it works a bit differently. All the LEDs light up when you get close to the beacon, and the closer you get the brighter they glow.
  • It takes radio strength readings using the radio.receive_full() command. This provides the message, the signal strength and a timestamp. We only want to know the signal strength, so we use signal = message[1] to extract this and store it in a variable called signal.
  • The signal strength may be in the range -98 (weakest) to -45 (strongest), and the Python program defines a function called map to convert numbers in this range to the range 0 – 9 which we can use for changing the brightness of the LEDs: 0 means off, 9 is the brightest an LED can be. (You might want to re-use this function in other Python projects as it works very much like the map block in MakeCode).
  • The Python program creates a blank 5x5 image called light using the command light = Image(5,5)
    Its brightness is changed using the light.fill() command.

What you need

  • 2 micro:bits and battery packs
  • MakeCode or Python editor
  • battery pack (optional)

Step 2: Code it

Transmitter / beacon

1from microbit import *
2import radio
3radio.config(group=1, power=1)
4radio.on()
5
6while True:
7    radio.send('1')
8    sleep(200)
9

Receiver

1from microbit import *
2import radio
3radio.config(group=1)
4radio.on()
5light = Image(5,5) # create an empty image
6
7# function to map signal stength to LED brightness
8def map(value, fromMin, fromMax, toMin, toMax):
9    fromRange = fromMax - fromMin
10    toRange = toMax - toMin
11    valueScaled = float(value - fromMin) / float(fromRange)
12    return toMin + (valueScaled * toRange)
13
14while True:
15    message = radio.receive_full()
16    if message:
17        signal = message[1]
18        brightness = map(signal, -98, -44, 0, 9)
19        light.fill(round(brightness))
20        display.show(light)
21

Step 3: Improve it

  • Combine the beacon and receiver code so you can have one micro:bit that does both tasks.
  • Make wrist bands so you can wear your proximity detectors.
  • How strong is the signal when you're 1 or 2 metres apart? Modify the code to trigger a visual or audible alarm when someone is too close.
  • Use these programs to make a treasure hunt game: hide the beacon and put the receiver code on lots of micro:bits
  • If you're outdoors or in a large space, experiment by changing the transmitter power. It can be any number from 0 to 7