14. Radio


The IoT would be heavily constrained and far more expensive to build if every device had to be hardwired to a computer in order to connect to the internet. The children will know from their own experience the important part WiFi plays in their lives. The first question my children ask when the reach a new destination is 'can I have the WiFi code?' For many children WiFi IS the internet. The request is invariably 'can i have WiFi?' not 'can i have internet access?'.

How many children actually know that the Wi is short for wireless or indeed what wireless actually is. Most children these days have never used a radio and don't even know what a radio is.

A great way to inform children is to get them to set up a wireless control system using the Radio feature on a pair of micro:bits. Once the children understand the concept they quickly come up with lots of different applications for radio controlled systems. Here are a couple of good examples to get you started:


Key points to note about radio on the micro:bit:

  • Broadcast messages are of a certain configurable length (up to 251 bytes).

  • Messages received are read from a queue of configurable size (the larger the queue the more RAM is used). If the queue is full, new messages are ignored.

  • Messages are broadcast and received on a preselected channel (numbered 0-100).

  • Broadcasts are at a certain level of power - more power means more range.

  • Messages are filtered by address (like a house number) and group (like a named recipient at the specified address).

  • The rate of throughput can be one of three pre-determined settings.

  • Send and receieve bytes to work with arbitrary data.

  • As a convenience for children, it’s easy to send and receive messages as strings.

  • The default configuration is both sensible and compatible with other platforms that target the BBC micro:bit.

Task:

Create a radio controlled light switch

The children will know the problem. They are in bed and ready to go to sleep but the light switch is by the door at the far end of the room. How cool would it be to be able to switch off the light without getting out of bed?


The model:

You will need two micro:bits, one as the 'switch' by the bed and the other as the receiver, to control the light. You will also need a white LED.

Connect the LED to pin 0 as describe in page 1. Output.


Outline lesson plan - follow this link to find out how we used the apparatus in the children's science lessons.


Micro Python script:

N.B. For the script below to access the radio module you will need to use the http://python.microbit.org beta version of the Python editor.

Light switch micro:bit - hand held controller script:

from microbit import *

import radio

radio.on()


while True:

if button_a.is_pressed():

display.scroll("ON")

radio.send('code_on') # The code you send can be anything you like so long as you use the same code in the receiving micro:bit's script.

sleep(50)


if button_b.is_pressed():

display.scroll("OFF")

radio.send('code_off') # The code you send can be anything you like so long as you use the same code in the receiving micro:bit's script.

sleep(50)


Light controller micro:bit script:

from microbit import *

import radio

radio.on()


while True:


msgin = radio.receive()


if msgin == 'code_on':

# ------turn light on

pin1.write_digital(1)

display.scroll("ON")

sleep(50)


if msgin == 'code_off':

# ------turn light off

pin1.write_digital(0)

display.scroll("OFF")

sleep(50)


Automated light switch

For a MakeCode blocks version of this radio light switch that is automated so that it is operated in response to the light level falling below a default value see the scripts below.

The left hand script is for the micro:bit connected via pin 0 to an LDR and is used to sense the light level. It will send a signal to the receiving micro:bit if the analogue reading falls below 900. You will have to experiment with this default value to find the best switching threshold to bring on the light.

The default value that you select will depend on the type of LDR and the resistance of resistor R1 used in the potential divider part of the circuit.

The right hand script is for the micro:bit controlling the light (an LED) connected between pin 0 and GND.

It will receive a string if the light level is above the default value and will turn on.

It will receive a number if the light level is below the default value and will turn off.


See the diagrams below for wiring the micro:bits and writing the BlockCode scripts.