Wireless Remote Control with micro:bit
Introduction
In this tutorial, we will utilize MakeCode's radio blocks to have one micro:bit transmit a signal to a receiving micro:bit on the same channel. Eventually, we will control a micro:bot wirelessly using parts from the micro:arcade kit!
Required Materials
To follow along with this tutorial, you will need the following materials at a minimum. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
You Will Also Need
The following materials are optional to make a battle bot.
- Scissors
- Electrical Tape or Glue
- Ping Pong Ball
- Skewers
Suggested Reading
If you aren’t familiar with the following concepts, we recommend checking out these tutorials before continuing.
Getting Started with the micro:bit
micro:bot Kit Experiment Guide
micro:arcade Kit Experiment Guide
Installing the Extensions for Microsoft MakeCode
To make the most out of the carrier boards with the least amount of coding, use the Microsoft MakeCode extensions written for the gamer:bit and moto:bit boards. If you have not already, check out the instructions for installing the extensions for both boards as explained in their respective guides whenever you make a new MakeCode project utilizing the boards.
gamer:bit Extension
To install the extension for the controller:bit (formerly known as the gamer:bit, head over to our micro:arcade kit experiment guide to follow the instructions.
moto:bit Extension
To install the extension for the moto:bit, head over to our micro:bot kit experiment guide to follow the instructions.
Experiment 1: Sending and Receiving a Signal
Introduction
There are a few methods of transmitting data between two or more micro:bits. You can send a number, or a string. For simplicity, we will send one number between two micro:bits.
radio
code blocks? Try looking at the MakeCode reference guide!
Parts Needed
You will need the following parts:
- 2x micro:bit Boards
- 2x Micro-B USB Cables
For a few items listed, you will need more than one unit (i.e. micro:bits, micro-B USB cables, etc.). You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
1.1: Sending
For this part of the experiment, we are going to send a number from one micro:bit to another micro:bit on the same channel.
Hardware Hookup
Connect the first micro:bit to your computer via USB cable.
Running Your Script
We are going to use Microsoft MakeCode to program the micro:bit. You can download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode.
Code to Note
When the micro:bit is powered up, we'll want it set up the radio to a certain channel. In this case, we head to the Radio blocks
, grab the radio set group __
block, and set it to 1
. We will use the built-in button A on the micro:bit. From the Input
blocks, we use the on button A pressed
, Once the button is pressed, we will transmit a number 0
wirelessly. For feedback, we will use the Basic
blocks to have the LEDs animate with a dot expanding out into a square to represent a signal being sent out. Once the animation is finished, we will clear the screen.
1.2: Receiving
For this part of the experiment, we are going to have a second micro:bit receive a number from the first micro:bit.
Hardware Hookup
To avoid confusion when uploading code, unplug the first micro:bit from your computer. Then connect the second micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode. You will need to open a new project to receive the data from the first part of this experiment.
Code to Note
When the second micro:bit is powered up, we'll want to set the radio to the same channel as the first micro:bit. In this case, it will be 1
. Using the on radio received __________
block, we will want to check to see what signal was received. In this case, we will want to check for a receivedNumber
that is equal to 0
since a number was sent from the first micro:bit. Using the if
statement from the Logic
blocks, we will want to check if the variable holding the received number matches 0
. If it matches, we will want the micro:bit to do something. Visually, using LEDs would be the easiest so an animation of a square shrinking to a dot was chosen to represent the received signal. Once the animation is finished, we will want to clear the screen until we receive another signal.
What You Should See
Pressing on button A with the first micro:bit (our transmitting) will send a number out in channel 1
. In this case, the number that is transmitted is 0
.
The second micro:bit (our receiving) will take that number that was sent on channel 1
and do something based on the condition statement. In this case, there is an animation using the LED array to indicate when we have received the number.
Experiment 2: Wirelessly Driving Forward
Introduction
Remember our micro:bot? The micro:bot was running around loose by itself the last time we were working with the robot. Let's give it some commands so that it only moves when you want it to using the controller:bit carrier board!
Parts Needed
You will need the following parts:
- 2x micro:bit Boards
- 2x Micro-B USB Cables
- 1x Assembled micro:bot Kit
- 1x controller:bit Carrier Board
- 4x AA Batteries
- 2x AAA Batteries (if you are using the battery pack with switch for the controller)
For a few items listed, you will need more than one unit (i.e. micro:bits, AA batteries, etc.). You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
2.1 Remote Control
For this part of the experiment, we will use the same method as experiment 1 to transmit a command. As soon as a certain combination of buttons are pressed on the controller:bit, the attached micro:bit will transmit one command to a receiving micro:bit. For debugging purposes, we are going to have a short animation when the buttons are pressed for feedback.
Hardware Hookup
If you have not already, insert one micro:bit into the controller:bit's carrier board. Make sure to insert the micro:bit with the LED array facing up (the same direction as all of the buttons) as explained in the micro:arcade kit experiment guide. We’re continuing on from experiment 3. Make sure to check out the guide before continuing on.
micro:arcade Kit Experiment Guide
July 21, 2017
Then connect the micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode. Remember, if you are building from scratch, you will need to install the appropriate extension when using the controller:bit.
Code to Note
When the micro:bit is powered up, we'll want it set up the radio to a certain channel. In this case, we set the radio block to 1
just like experiment 1. Then we'll want to check if the P16 button is pressed on the controller:bit, just like an accelerate button used in driving games. For simplicity, we will want the robot to move forward when pressing the up (P0) button on the direction pad. We will be using a nested if
statement to check on the buttons defined in the gamer:bit extension. A nested if statement is one or more if statements "nested" inside of another if statement. If the parent if statement is true, then the code looks at each of the nested if statements and executes any that are true. If the parent if statement is false, then none of the nested statements will execute. By using the logic statement, we will be using the gamer:bit ____ is pressed
block.
Once both buttons are pressed, we will send a command out. In this case, it will be number 0
. For feedback, to see if our code works as expected, we will have the LEDs animate with an arrow pointing up and a square growing bigger. We then give the micro:bit a short pause before clearing the screen and looping back to the beginning of the forever
loop.
2.2 Robot Receive
In this part of the experiment, we need to have a receiving micro:bit recognize the command that was sent from the first micro:bit in order to control the micro:bot. For simplicity, we will have the robot drive forward. To debug, the LED array will display an arrow to indicate the direction where the robot should move for feedback.
Hardware Hookup
At this point, you should have your micro:bot kit assembled! We're continuing on from experiment 5. Make sure to check out the guide before continuing on.
micro:bot Kit Experiment Guide
February 20, 2020
To avoid confusion when uploading code, unplug the first micro:bit from your computer. Connect the second micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. You can also use it as an example to build it from scratch in MakeCode. Remember, if you are building from scratch, you will need to install the appropriate extension when using the moto:bit.
Code to Note
When the second micro:bit is powered up, we'll want to set the radio to the same channel as the first micro:bit; again to 1
. As explained in the micro:bot experiment guide, we will want to set the left and right motors' logic based on the way we connected the motors to the motor:bit. For consistency, we will make both true
. Using the on radio received __________
block from the moto:bit extension, we will want to check to see what command was received. In this case, it should be a 0
that was sent from the first micro:bit. If the command that was sent matches 0
, we will want the moto:bit to drive forward at 100
%. For feedback, we will have an arrow point up. After a short pause of about 50ms, we will want to clear the screen and turn the motors off before checking again. If the pause is too long, the micro:bot will continue to drive forward even after you remove your fingers from the controller:bit's buttons.
What You Should See
Now that both micro:bits are programmed, let's the test code out. Disconnect the USB cables from both micro:bits and power each with the respective battery packs.
For the micro:bot, insert the 4xAA battery pack into the barrel jack.
Then move the switch labeled as RUN MOTORS to the ON position. Pressing on the drive button (P16) and up button (P0) on the controller:bit will cause the micro:bot to drive forward. You should also see the LED array animate.
Experiment 3: Wirelessly Controlling the micro:bot
Introduction
Now that we are able to wirelessly move our robot forward, we will want additional commands for turning and driving in reverse. While we are at it, we are going to give a special function to control the servos attached to our battle bot.
Parts Needed
You will need the following parts:
- 2x micro:bit Boards
- 2x Micro-B USB Cables
- 1x Assembled micro:bot Kit
- 1x controller:bit Carrier Board
- 4x AA Batteries
- 2x AAA Batteries (if you are using the battery pack with switch for the controller)
For a few items listed, you will need more than one unit (i.e. micro:bits, AA batteries, etc.). You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
3.1 Full Remote Control
For this part of the experiment, we will repeat the steps in 2.1 to transmit commands for turning or driving in reverse. The built-in accelerometer will be used to detect a shake. A command to control the servo motors will be sent out as soon as there is a shake detected.
Hardware Hookup
If you have not already, insert one micro:bit into the controller:bit's carrier board. Make sure to insert the micro:bit with the LED array facing up (the same direction as all of the buttons) as explained in the micro:arcade kit experiment guide. We’re continuing on from experiment 3. Make sure to check out the guide before continuing on.
micro:arcade Kit Experiment Guide
July 21, 2017
Disconnect the battery pack from micro:bit from the previous experiment. The JST connector is a tight fit in the micro:bit so you will need to carefully remove the connector out by wiggling it side to side using your index finger and thumb. It is easier to remove the micro:bit from the controller:bit carrier board.
Then connect the first micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode. You will need to open a new project to receive the data from the first part of this experiment. Remember, if you are building from scratch, you will need to install the appropriate extension when using the controller:bit.
Code to Note
Using the same template for driving forward, we assign a unique number to the right (P2), down (P8), and left (P1) buttons on the direction pad. For simplicity, we will just want the robot to move forward when turning right or left. There is not as much animation with the LED array in this example since it can slow down your micro:bit. Using the Input's on shake
block from the Input
, a unique number is sent out to control the servos. Again, an arrow will display briefly for feedback in each case.
3.2 Full Battle Bot Control
For this part of the experiment, we will repeat the steps in 2.2 to receive the commands before moving the robot. Instead of just driving forward, the robot can now turn, reverse, and control the servo motors.
Hardware Hookup
We'll be using the same servo connection on the battle bot as explained in experiment 5. Make sure to check out the guide before continuing on.
micro:bot Kit Experiment Guide
February 20, 2020
Remove the battery pack from the moto:bit carrier board from the previous experiment.
To avoid confusion when uploading code, unplug the first micro:bit from your computer. Then connect the second micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example and build it from scratch in MakeCode. You will need to open a new project to receive the data from the first part of this experiment. Remember, if you are building from scratch, you will need to install the appropriate extension when using the moto:bit.
Code to Note
We set up the second micro:bit to use the same channel and motor logic as explained in experiment 2.2. Four more commands were added to the on radio received __________
to turn right, reverse, turn left, and control the servo motors of our battle bot. When turning, the motor closest to the inside of the turn is set to a lower value. That way the robot will continue to drive forward but also move right or left. To reverse, both motors used the reverse
option to drive backwards. Finally, if the robot receives a command indicating that there was a shake from the first micro:bit, the servos will move. If you look closely at the receivedNumber
, each of the commands match the same number that was transmitted from the first micro:bit.
What You Should See
Now that both micro:bits are programmed with more commands, let's test the code out. Disconnect the USB cable from both micro:bits and power each with the respective battery packs again.
Pressing the controller:bit's drive button (P16) and any of the buttons located in the direction pad (P0, P2, P8, P1) will cause the robot to move. Shaking the first micro:bit will make the servos on the micro:bot move.
Powering Down Your Controller and Robot
When finished, make sure to power down your controller and micro:bot by disconnecting the JST connector and barrel jack on the battery packs. This will save you some power in the long run for more fun! Again, the JST connector is a tight fit in the micro:bit so you will need to carefully remove the connector out by wiggling it side to side using your index finger and thumb.
Coding Challenges
Transmitting Other Things
Instead of sending a number, try adjusting the code to transmit a value or string between the two micro:bits.
2-Way Communication
Try adjusting experiment 1's code to have the second micro:bit send a signal back to the first micro:bit.
Broadcasting
Have more than two micro:bits? Try modifying code to broadcast a signal from one micro:bit to several micro:bits by having the receiving micro:bits on the same channel! Or try coding with a friend to make two micro:bits fighting for control of a third micro:bit attached to a micro:bot.
Add More Movement!
Ok, so experiment 3 did not have "all" the specified movements. We just gave some basic movements so that the micro:bot can move around the floor. Try adjusting the codes to turn when the micro:bot is moving backwards. Can you adjust the code so that the robot can rotate without driving forward or back? Or maybe program the robot to dance when a certain button(s) are pressed.
Arcade Joystick and Buttons
Go retro and wire the gamer:bit with the joystick and buttons for a different style controller. The joystick and buttons are great for more intense gamers.
Sensor Control
Try flipping the control of the buttons and sensors. Instead of using the buttons to control the robot's motion, try using the accelerometer or magnetometer. Can you control the motor intensity based on the sensor reading? Then, to control the battle bot's servos, try using the buttons.
Monitoring Environment
Try using parts from the micro:climate kit to relay sensor data and monitor an environment at a distance between two micro:bits.
Troubleshooting
Not working as expected? Below are a few troubleshooting tips to get your micro:bits working as expected. For your convenience, relevant troubleshooting tips from the micro:arcade and micro:bot experiment guides were included below.
Not Receiving a Signal?
- Make sure that the micro:bits are on the same channel.
- Make sure that the number sent is the same.
- Make sure that your are sending and receiving a number.
The Second Micro:bit is Reacting Without Sending Data from the First Micro:bit.
- If you have more than one micro:bit transmitting on the same channel, the receiving micro:bit may be reacting to other micro:bits sending on the same channel. Make sure that each micro:bit pair is on their own unique channel. You can also try to adjust the number being transmitted on each micro:bit to react to certain number.
- If you have more than one micro:bit transmitting on the same channel, the receiving micro:bit may be reacting to other micro:bits sending on the same channel. Make sure that each micro:bit pair is on their own unique channel. You can also try to adjust the number being transmitted on each micro:bit to react to certain number.
Micro:bit is Not Showing Up.
- Try unplugging the USB cable and plugging it back in. Also, be sure that you have the cable inserted all the way into your micro:bit.
- Try unplugging the USB cable and plugging it back in. Also, be sure that you have the cable inserted all the way into your micro:bit.
Gamer:bit or Moto:bit Blocks are Not Visible.
- Make sure you have a network connection and you have added the extension to your MakeCode environment.
- Try installing it again from the add extension option in MakeCode.
Robot Not Driving Straight!
- This may be due to the floor that the micro:bot is driving on. The wheels and motors might have slight differences in the way they were manufactured. Having the robot drive on certain floors like carpet can make the slight differences more apparent. To correct for these differences, you may want to adjust the intensity of each motor moving
forward
orreverse
.
- This may be due to the floor that the micro:bot is driving on. The wheels and motors might have slight differences in the way they were manufactured. Having the robot drive on certain floors like carpet can make the slight differences more apparent. To correct for these differences, you may want to adjust the intensity of each motor moving
Robot is Not Moving After Sending a Command from the Gamer:bit.
- Make sure your motors are hooked up correctly in the motor ports and your motor power switch is set to "RUN Motors", the battery pack is plugged in, and you have fresh batteries.
- Make sure the transmitting and receiving micro:bits are using the correct code.
Moving in Reverse?!
- There are two options if you notice the micro:bot moving in the wrong direction. As explained above, you can flip the wiring of your motors or use the
set ____ motor invert to ____
block in youron start
block to change what is forward vs. reverse.
- There are two options if you notice the micro:bot moving in the wrong direction. As explained above, you can flip the wiring of your motors or use the
Servo Moves in the Wrong Direction.
- Flip your servos, or change your code so that it rotates the correct direction.
- Flip your servos, or change your code so that it rotates the correct direction.
Servo Doesn't Move at All!
- Double check your connections of the servos to the moto:bit to make sure that they are hooked up correctly.
Resources and Going Further
Now that you've successfully got your micro:bits communicating, it's time to incorporate it into your own project! Need some inspiration? Check out our other robots found in our robotics section.
Wireless Motor Driver Shield Hookup Guide
SparkFun Inventor's Kit Experiment Guide - v4.0
SparkFun Auto pHAT Hookup Guide
Looking for more wireless fun? Check out the following using wireless applications.
Experiment Guide for the Johnny-Five Inventor's Kit
MicroMod WiFi Function Board - ESP32 Hookup Guide
GNSS Correction Data Receiver (NEO-D9S) Hookup Guide
Or try checking out these cool robots from AVC.