Proximity Beacon
<h2 id="step-1:-make-it"><strong>Step 1: Make it</strong></h2>
<p> </p>
<h3 id="what-is-it?"><strong>What is it?</strong></h3>
<p>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.</p>
<h3> </h3>
<h3><strong>Introduction</strong></h3>
<p><iframe allow="encrypted-media" allowfullscreen="" frameborder="0" src="https://www.youtube-nocookie.com/embed/ww3l4PTkY-Y?rel=0&cc_load_policy=1" title="Introduction video"></iframe></p>
<p> </p>
<h3><strong>Coding guide</strong></h3>
<p><iframe allow="encrypted-media" allowfullscreen="" frameborder="0" src="https://www.youtube-nocookie.com/embed/U6-JOYrg030?rel=0&cc_load_policy=1" title="Coding guide video"></iframe></p>
<p> </p>
<h3 id="how-it-works"><strong>How it works</strong></h3>
<ul>
<li>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.</li>
<li>When the receiver picks up a message from the beacon, it stores its strength in a variable called <strong>signal</strong> and shows it on its LED display.</li>
<li>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.</li>
<li>If the radio signal is weak, the other micro:bit is probably further away.</li>
<li>It<strong> </strong>displays a bar graph which gets bigger the stronger the signal and the closer you are. It uses the maths <strong>map </strong>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.</li>
</ul>
<h4> </h4>
<h4 id="python-version"><strong>Python version</strong></h4>
<ul>
<li>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.</li>
<li>It takes radio strength readings using the <strong><code>radio.receive_full()</code></strong> command. This provides the message, the signal strength and a timestamp. We only want to know the signal strength, so we use <strong><code>signal = message[1]</code></strong> to extract this and store it in a variable called <strong>signal</strong>.</li>
<li>The signal strength may be in the range -98 (weakest) to -45 (strongest), and the Python program defines a <strong>function</strong> called <strong>map</strong> 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 <strong>map</strong> block in MakeCode).</li>
<li>The Python program creates a blank 5x5 image called <strong>light</strong> using the command <strong><code>light = Image(5,5)</code></strong><br />
Its brightness is changed using the <strong><code>light.fill()</code></strong> command.</li>
</ul>
<h3> </h3>
<h3 id="what-you-need"><strong>What you need</strong></h3>
<ul>
<li>2 micro:bits and battery packs</li>
<li>MakeCode or Python editor</li>
<li>battery pack (optional)</li>
</ul>
<h2> </h2>
<h2 id="step-2:-code-it"><strong>Step 2: Code it</strong></h2>
<p> </p>
<h3 id="transmitter-/-beacon"><strong>Transmitter / beacon</strong></h3>
<p> </p>
<h3><u>MakeCode</u></h3>
<p><img alt="" src="/api/storage/uploads/legacy-images/zsvurhg5hwvha74q1lq5t2uz10pi/download-6.png" style="width: 600px; height: 174px;" /></p>
<p> </p>
<h3><u>Python</u></h3>
<pre>
<code>from microbit import *
2import radio
3radio.config(group=1, power=1)
4radio.on()
5
6while True:
7 radio.send('1')
8 sleep(200)
9</code></pre>
<p> </p>
<p><a data-testid="editor-link" href="https://python.microbit.org/v/3#import:#project:XQAAgAAKAQAAAAAAAAA9iImmlGSt1R++5LD+ZJ36cRz46B+lhYtNRoWF0nijpaVyZlK7ACfSpeoQpgfk21st4ty06R4PEOOnNFb5jFDe/sdN9iRe3api7D2f9eq770M/SopOimNLX8a1iRyRTmBfFeGK9GkgtdpVnOJMImkUTRpOj/Z6sJuNYmiZl+fwkd2qGd7pNY5ENogD+dM6+Om6PjO4wBXf3SmkWm6uGUYFkx+WVJfHaBYc5IXTm29c8K7+56IyurUDvG4EO2OFld80aE/D/4uCAAA=">Open in Python</a></p>
<p><a href="https://classroom.microbit.org/?id=proximity-beacon-proximity-beacon-transmitter&project=Project%3A%20Proximity%20beacon&name=proximity%20beacon%20transmitter&editors=makecode%2Cpython">Open in Classroom</a></p>
<p><a data-testid="editor-link" href="https://makecode.microbit.org/#pub:_2hRFJ6WTWKgW">Open in MakeCode</a></p>
<p><a href="https://microbit.org/downloads/proximity-beacon-proximity-beacon-transmitter-makecode.hex">Download HEX</a></p>
<h3> </h3>
<h3 id="receiver"><strong>Receiver</strong></h3>
<h3> </h3>
<h3><u>MakeCode</u></h3>
<p><img alt="" src="/api/storage/uploads/legacy-images/oin6eykid0ig6qsqajzgimalte2o/download-7.png" style="width: 700px; height: 176px;" /></p>
<p> </p>
<h3><u>Python</u></h3>
<pre>
<code>from 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</code></pre>
<p> </p>
<p><a data-testid="editor-link" href="https://python.microbit.org/v/3#import:#project:XQAAgAABAwAAAAAAAAA9iImmlGSt1R++5LD+ZJ36cRz46B+lhYtNRoWF0nijpaVyZlK7ACfSpeoQpgfk21st4ty06R4PEOOnNFb5jFDe/sdN9iRe3Z7vbfHi2bCoWZGTig+6/Pau+F6V1gqR3ZXNVlKvVdJxi2Hn8H5ZyPzS4YuHt0oNjjWFqJD+dA7Iltgazzw2dxtoXrFjAFgn1BtLtnZV1XbVHaZx5EsvEsMwmgjpzOB1Q7TZg5KnBUYieKA45UFj1zfyTCQyOAby2PZd82hz0kAH9GAlw9Ppbd3VxAkVE9RsCC20WoknPgoxI8nTyuPWrVKgdgqtLYt3rsqrTTRCIJAs9odl/qZp/mQyxpyidZ6FY6BwKkTZH9vvqHZCh1ugek0bEcw5MHvIa69uKfgx5zGkXETiR3k75mWYQJ7eCF76kePIEGfVqJNhUNYn2GHNYHinluGa9HB4QLmrMqhPgRA5iCxmx3xEXtXSr6SRfkXOmlnTim0Lpm7kntE5bK/vtWUa/m+FzvZQeeNs9dp97UiVYQRDS0INdX47I0tnKn2GR+8E6IKQZ0UO8UkY8VSijuIOSEoTv8f/365OGg==">Open in Python</a></p>
<p><a href="https://classroom.microbit.org/?id=proximity-beacon-proximity-receiver&project=Project%3A%20Proximity%20beacon&name=proximity%20receiver&editors=makecode%2Cpython">Open in Classroom</a></p>
<p><a data-testid="editor-link" href="https://makecode.microbit.org/#pub:_MciderdAVEfM">Open in MakeCode</a></p>
<p><a href="https://microbit.org/downloads/proximity-beacon-proximity-receiver-makecode.hex">Download HEX</a></p>
<h2> </h2>
<h2 id="step-3:-improve-it"><strong>Step 3: Improve it</strong></h2>
<ul>
<li>Combine the beacon and receiver code so you can have one micro:bit that does both tasks.</li>
<li>Make wrist bands so you can wear your proximity detectors.</li>
<li>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.</li>
<li>Use these programs to make a treasure hunt game: hide the beacon and put the receiver code on lots of micro:bits</li>
<li>If you're outdoors or in a large space, experiment by changing the transmitter power. It can be any number from 0 to 7</li>
</ul>
