[2020] Using Two (x2) Micro:bits to Control an RC Car
by hummingworks in Circuits > Remote Control
7126 Views, 17 Favorites, 0 Comments
[2020] Using Two (x2) Micro:bits to Control an RC Car
If you have two (x2) micro:bits, have you thought of using them for remotely controlling an RC car? You can control an RC car by using one micro:bit as the transmitter and another one as the receiver.
When you use MakeCode editor for coding a micro:bit, you can find an extension named Radio which allows one of your micro:bit to broadcast data over the air to another micro:bit in the same group. You can easily send and receive data using Radio extension for many projects.
In this tutorial, we will learn how to use two (x2) micro:bits to remotely control an RC car. We will check the steps to set up two (x2) micro:bits and use MakeCode editor for explaining how the sample files are coded. You can download ready-to-play sample codes in this project and you don't need to worry about coding everything from scratch. You can always customize the sample codes later for your own learning purposes.
Supplies
Let's get started! For the transmitter and the receiver, we will use two (x2) micro:bits. For batteries, we recommend using new and single-use 1.5V AA and AAA batteries.
- micro:bit x2
- battery case x1
- 1.5V AAA batteries x2 (for the battery case)
For the toy car in this project, we will use Valenta Off-Roader RC car. Valenta Off-Roader is a micro:bit powered RC car. It is Lego Technic compatible and equipped with two (x2) micro gear motors on the rear wheels and one (x1) built-in steering servo based on Roberval balance arm mechanism.
- Valenta Off-Roader x1
-
1.5V AA batteries x4 (for the car)
You can also refer to the instruction for assembling the car.
Copying Sample Files to Micro:Bits
For this project, we've prepared MakeCode sample files you can download to your computer. Since these sample files are ready to play, you can start playing immediately.
In this step, please download Transmitter.hex file and Receiver.hex file. Connect your computer and a micro:bit via USB cable, and copy each file to each of your micro:bit one at a time.
First, drag and drop Transmitter.hex file to a micro:bit and use this as "transmitter" micro:bit.
Second, drag and drop Receiver.hex file to another micro:bit and use this as "receiver" micro:bit.
Once you've copied the sample file to your micro:bit, disconnect it from your computer.
Connect the battery case to "transmitter" micro:bit and turn it on. (Alternatively, you can also provide the power to "transmitter" micro:bit by connecting it to the computer via USB cable.)
Mount "receiver" micro:bit on your car and turn on the power switch on the motor controller.
Reviewing Transmitter.hex Sample File
Transmitter.hex
Now we will examine Transmitter.hex sample file. Open MakeCode editor and click Import button. Open Transmitter.hex file you've copied to "transmitter" micro:bit.
on start block
This block is called initially at once when "transmitter" micro:bit is turned on. In Radio extension, you can find the block radio set group and 1 is set for example. This number must be the same for "transmitter" micro:bit and "receiver" micro:bit, so they can be paired together for communication.
The file uses Accelerometer features. By tilting your "transmitter" micro:bit down, up, right or left, it will send radio strings of "goForward" "goBackward" "goRight" or "goLeft" strings to your "receiver" micro:bit.
on logo down block
In Input functions, you can find the block on logo down. This block is executed whenever you tilt "transmitter" micro:bit down or forward. In the block, you can also find radio send string "goForward" that will send the radio string "goForward" over the air when "transmitter" micro:bit is tilted down. When "receiver" micro:bit receives this string, the car will go forward.
on logo up block
In Input functions, you can find the block on logo up. This block is executed whenever you tilt "transmitter" micro:bit up or backward. In the block, you can also find radio send string "goBackward" that will send the radio string "goBackward" over the air when "transmitter" micro:bit is tilted up. When "receiver" micro:bit receives this string, the car will go backward.
on tilt right block
In Input functions, you can find the block on tilt right. This block is executed whenever you tilt "transmitter" micro:bit to the right. In the block, you can also find radio send string "goRight" that will send the radio string "goRight" over the air when "transmitter" micro:bit is tilted to the right. When "receiver" micro:bit receives this string, the car will turn right.
on tilt left block
In Input functions, you can find the block on tilt left. This block is executed whenever you tilt "transmitter" micro:bit to the left. In the block, you can also find radio send string "goLeft" that will send the radio string "goLeft" over the air when "transmitter" micro:bit is tilted to the left. When "receiver" micro:bit receives this string, the car will turn left.
Adding Servos Extension
Servos extension
Does your MakeCode editor include Servos extension? Please open the editor and check if it includes Servos extension in the left menu. We will use it in Receiver.hex sample file. This Servos extension will be used for calibrating the steering angle. If you can't find Servos extension, click Extensions in the bottom of the menu. Click Servos extension and add it to the menu.
Reviewing Receiver.hex Sample File (Part 1)
Receiver.hex
Now we will examine Receiver.hex sample file. Open MakeCode editor and click Import button. Open Receiver.hex file you've copied to "receiver" micro:bit.
on start block
This block is called initially at once when "receiver" micro:bit is turned on. In Radio functions, you can find the block radio set group and 1 is set for example. This number must be the same for "transmitter" micro:bit and "receiver" micro:bit, so they can be paired together for communication.
From Functions extension, function steer block is created. Drag and drop call steer block inside on start block. It will call function steer to straighten your car's steering.
function steer block
By default, a car's steering is not always straight ahead because of its servo. If you look at the car from above, the steering might be a little right or left. This function steer block is used for calibrating the servo angle to its central position, so that the car's steering is adjusted straight.
Let's assume that the servo is attached to P2 pin on the motor controller. Let's configure that the servo swings from 0 to 180 degrees and its central angle is 90 degrees.
In Functions extension, function steer block has been created. In Variables extension, create a new variable center for calibrating the steering servo. Use Math function to make 90 + 0 bracket. Drag and drop set center to 90 + 0 block inside function steer block.
From Servos extension, drag and drop set servo P2 range from 0 to 180. Make sure to choose P2 pin and the rotation range from 0 to 180 degrees.
From Servos extension, drag and drop set servo P2 angle to center. Make sure to set the angle to the variable center.
Look at your car from above. How does it look?
If the steering is a little left, set center to 90 - 5 for offsetting -5 degrees to the right.
If the steering is a little right, set center to 90 + 5 for offsetting +5 degrees to the left.
(Continuing to the next step)
Reviewing Receiver.hex Sample File (Part 2)
Receiver.hex (continued)
How can we set direction and speed? The car has a micro gear motor M1 on the left rear wheel and M2 on the right rear wheel.
function goForward block
Left rear wheel M1 motor
P13 pin is used for direction. From Pins extension, drag and drop digital write pin P13 to 0 so M1 goes forward.
P12 pin is used for speed (max speed is 1023). From Pins extension, drag and drop analog write pin P12 to 1023 so M1 runs at max speed.
Right rear wheel M2 motor
P15 pin is used for direction. From Pins extension, drag and drop digital write pin P15 to 0 so M2 goes forward.
P14 pin is used for speed (max speed is 1023). From Pins extension, drag and drop analog write pin P14 to 1023 so M2 runs at max speed.
From Basic extension, drag and drop pause (ms) 1000 block to keep the car going forward for 1000 milliseconds (1 second) and execute call stop function to stop the car safely.
function goBackward block
Left rear wheel M1 motor
P12 pin is used for direction. From Pins extension, drag and drop digital write pin P12 to 0 so M1 goes backward.
P13 pin is used for speed (max speed is 1023). From Pins extension, drag and drop analog write pin P13 to 1023 so M1 runs at max speed.
Right rear wheel M2 motor
P14 pin is used for direction. From Pins extension, drag and drop digital write pin P14 to 0 so M2 goes backward.
P15 pin is used for speed (max speed is 1023). From Pins extension, drag and drop analog write pin P15 to 1023 so M2 runs at max speed.
From Basic extension, drag and drop pause (ms) 1000 block to keep the car going backward for 1000 milliseconds (1 second) and execute call stop function to stop the car safely.
function stop block
Left rear wheel M1 motor
P13 pin is used for direction. From Pins extension, drag and drop digital write pin P13 to 0 so M1 is set to forward direction.
P12 pin is used for speed (0 means no speed). From Pins extension, drag and drop analog write pin P12 to 0 so M1 stops.
Right rear wheel M2 motor
P15 pin is used for direction. From Pins extension, drag and drop digital write pin P15 to 0 so M2 is set to forward direction.
P14 pin is used for speed (0 means no speed). From Pins extension, drag and drop analog write pin P14 to 0 so M2 stops.
From Functions extension, drag and drop call steer block to straighten the car steering.
(Continuing to the next step)
Reviewing Receiver.hex Sample File (Part 3)
Receiver.hex (continued)
Whenever "receiver" micro:bit catches the radio string sent from "transmitter" micro:bit over the air, how can Receiver.hex sample file sort it out and call the relevant function to control the car?
on radio received receivedString block
Bring this block from Radio extension and it will trigger an action defined inside this block whenever a new radio string is arrived at "receiver" micro:bit.
if then block
Bring this block from Logic extension and it will sort out actions depending on the string received.
If the received string is "goForward" then the block will call goForward function.
If the received string is "goBackward" then the block will call goBackward function.
If the received string is "goRight" then set the steering servo's angle to -10 degrees to the right and the block will call goForward function.
If the received string is "goLeft" then set the steering servo's angle to +10 degrees to the left and the block will call goForward function.
The sample files explained in this tutorial are very basic, and you can customize the code to your own. Have fun!