What happens when already have a couple of accessory boards connected to your micro:bit and want to add more? For example, like our Program a micro:bit with Python to Control a Water Pump tutorial.
Although it is possible to break out the micro:bit connector to access all the General Purpose Input/Output (GPIO) pins, we will eventually run out of space to store our code, and memory to run our code. Luckily, we can use radio to allow our micro:bit to communicate with another micro:bit connected to yet another accessory board!
This works equally well across the V1 or V2 micro:bits or a combination of both; since we'll be spreading the load across two micro:bits.
This project will let you add an servo motor controller to your self-watering micro:bit project. The servo can be used to raise a flag or some sort of sign to indicate the status of your plant, and can even be located a few metres away from your self-watering plant!
The project can be run as a group activity, with one person programming the first micro:bit and their lab partner programming the second micro:bit. Alternatively, one person could program both micro:bits by having separate code in tabs within mu or Thonny editors. Using two separate computers to program can be useful for testing your code more easily, since you can monitor both micro:bits while testing or debugging.
In this project, you will learn:
- how to use radio to allow multiple micro:bits to communicate;
- how to distribute your code across multiple micro:bits; and
- how to add more micro:bit accessories to your projects.
You will need:
- To have already completed our Program a micro:bit with Python to Control a Water Pump tutorial
- An second micro:bit (V1 or V2), in addition to the one from your original Moisture Sensor project. This project will also work with a combination of V1 and V2 micro:bits.
- A Kitronik Simple Servo Control Board for micro:bit
- A matching 6 volt battery cage
- Some matching servos with cables and accessories e.g., FS90 Micro 180 Degree Servo
Step 1: Configure the Radio
For this project, you'll be starting with the earlier Moisture Sensor project.
First, let's add some commands to our earlier Python code, to:
- import the radio module
- configure the radio channel
- turn the radio on
Add the following lines to your original code (the new lines are bolded so you know which ones to add).
Note: you can download the Python code stored on your micro:bit by connecting it via USB and clicking on "Files" in the mu or Thonny editors. This assumes that you used either mu or Thonny for editing and flashing the code, previously.
from microbit import * import radio radio.config(channel=1) radio.on() while True: moisture = pin1.read_analog() if moisture > 400: pin0.write_digital(0) else: pin0.write_digital(1)
Step 2: Send Something
We can send the
moisture value over radio by converting it to text using the
str() function. Later we will convert it back to a number and decide what action to perform on our second micro:bit.
To send this value over radio, add a
radio.send() command after the moisture value is read.
from microbit import * import radio radio.config(channel=10) radio.on() while True: moisture = pin1.read_analog() radio.send(str(moisture)) if moisture > 400: pin0.write_digital(0) else: pin0.write_digital(1)
Flash your code back to the first micro:bit and make sure it still works. If it doesn't work, check your code to make sure you have the correct indentation and that everything is typed in correctly. Click on the REPL button in your editor while the micro:bit is still connected to your computer to see any errors when your code runs. Use the reset() command to restart your code once you enter the REPL with your micro:bit connected to the USB port of your computer.
Step 3: Write Some Code for the Second micro:bit
Again, let's set up the radio to receive on the same channel as our first micro:bit.
Then we will receive our radio message in the
received_data variable, convert it back into a number with
print() it. This code will need to be stored and run on your second micro:bit.
from microbit import * import radio radio.config(channel=1) radio.on() while True: received_data = radio.receive() if received_data: print(int(received_data))
Flash your code to the second micro:bit and turn both on. As before, use the REPL to view the moisture value on the second micro:bit. If the radio communications are working, you should see the moisture value displayed continuously.
Step 4: Connect Servo(s) to a Second micro:bit
Once we have our two micro:bits talking to each other over radio, we can create some events using
if..then..else to do something based on the received moisture data! Since we have sent this information to another micro:bit, we now have more memory and storage space and can make use of all the input/output pins on our second micro:bit.
Although it is possible to control a single servo from a micro:bit, we can also leverage a micro:bit accessory board to let us control multiple servo motors; and perform other useful tasks based on our plant's environment.
Kitronik Simple Servo Control Board
This board has some handy features:
- Instead of using the standard micro:bit input/output pins 1-3, it uses other pins via a connector slot, and breaks out the standard pins. You can then use the standard micro:bit pins for connecting even more accessories to your micro:bit.
- There's a built-in battery cage on the back to power and control up to 3 servo motors and a switch to save batteries when you're not running it.
- Cutout hooks allow you to attach it to your project with rubber bands.
You can download the KitronikSimpleServo.py library from Kitronik's github repository here. Place the file in the
mu_code directory of your computer. We'll copy this to your micro:bit after flashing your main code.
To use this board on your second micro:bit (the one that receives the moisture value via radio), you can use the following Python code. It waits for an incoming moisture value, converts it back to a number using int(), and will move a servo connected to the
servo2 pins on the Kitronik board.
Note: Make sure you connect the brown wire of the servo to the pin marked,
from microbit import * import radio from KitronikSimpleServo import * radio.config(channel=10) radio.on() servo2 = simpleServo(pin15) while True: incoming = radio.receive() if incoming: moisture = int(incoming) print(moisture) if moisture > 400: servo2.write_angle(180) else: servo2.write_angle(0)
Flash this code onto your second micro:bit and use the
Files button and drag across the
KitronikSimpleServo.py file. This allows your code to import the required functions to control the servos.
With both micro:bits turned on, touch the moisture sensor on the first micro:bit. You should see the servo move when you touch the moisture prongs, and move again when you release them.
Congratulations! You now have a self-watering plant that also controls multiple servos on a second micro:bit. Since there is now more storage and working memory available on the second micro:bit, you have just increased your options!
For instance, you could:
- Calibrate your code to have a cardboard arrow attached to a servo, that points to a scale and shows how much moisture is in your plant's soil
- Have your micro:bit check multiple radio channels, to receive data from more than one micro:bit soil moisture sensor
- Add a higher quality temperature sensor or humidity sensor to the second micro:bit, using the free pins on the Kitronik Simple Servo control board
- Use servos and piano wire to activate a shade for your plant, based on a light sensor reading
- ..and more!
If your radio communications don't seem to be working, you can connect each micro:bit to a computer and try sending and receiving data. Use the
print() command to print data to the in the mu editor REPL, and type
reset to run the code once you enter the REPL window.
Next Steps and Challenges
You can add multiple micro:bits, each on their own radio channel, and have your receiving micro:bit switch between channels; either by using the buttons on the front of the micro:bit, or just by using a loop counter to switch to different channels periodically. With three servos, you could even show the status of three separate micro:bit plants, or pop up a different cardboard sign, depending on the amount of moisture, or even control shades for each plant.
You can also learn more about the Python radio functionality of the BBC micro:bit, here. It's possible to increase the range or save power, depending on how you configure the radio.