About the Author
Les is a maker and trainer who has worked with the Raspberry Pi Foundation and the BBC to deliver computing training.
Robots are cool and they are even more cool when you make your own. In this feature we take a look at building a really simple budget robot that will provide you with a foundation to learn more about robot building. We shall code the robot in two ways. Firstly using the Makecode editor, and secondly via MicroPython. But before a line of code is written, we need to make sure that we understand what a robot is and what components makes a robot.
A robot will follow instructions blindly, if we tell it to go forward, it will and it will never stop until we tell it to do so. So what does this mean? Well it means that we must be careful when writing and issuing instructions. If we get something wrong then the robot will quickly show us, typically by driving off in any direction.
The components that make a robot are two (or more) motors with wheels. These are typically bright yellow DC motors that can be found cheaply online. But these motors need a controller in order for them to safely work and these controllers come in all manner of configurations. From large industrial precisely calibrated controllers to simple hobbyist level models, every motor needs a controller and there is one for every budget. But what is the brains behind the robot? Well in our case it is the humble micro:bit on to which we will write code that will send signals from the micro:bit GPIO to the controller board, turning the motors on and off to drive in any direction we choose.
Building the robot is where the fun begins!
For this example robot you will need
A micro:bit
Pimoroni Pinbetween
L9110S motor controller
LD1117AV33 3.3V Regulator
4 x AA battery box
4 x AA batteries
Breadboard
3 x Female to male jumper wires
5 x Female to female jumper wires
1 x Male to male jumper wire
A robot chassis (from eBay)
The build starts by inserting the micro:bit into the pinbetween board. This will open up all of the usable GPIO pins on the micro:bit.
The complete circuit sees the micro:bit connect to the L9110S motor controller using female to female jumper wires. This connection will enable the micro:bit to control the L9110S which in turn will control the motors connected to the two screw terminals of the L9110S.
The connections from the micro:bit to L9110S motor controller are:
From the pinbetween to the L9110S motor controller we need to make a connection between the GND of the micro:bit and the L9110S. This ensures that the two components share a common GND.
To create a single power source to power both the micro:bit and motors we need to use a voltage regulator to drop the 5V from the AA batteries to 3.3V for the micro:bit. Into the breadboard insert the voltage regulator so that the black side of the chip is facing you. Using a female to male jumper wire, connect the firs-to-g (from the left) of the regulator to GND on the micro:bit then connect the GND (black) wire from the battery pack to the same leg. For the second leg connect this to the 3V pin of the micro:bit using another female to male jumper wire. Lastly connect t-to-hird leg of the regulator to the the VCC pin of the L9110S motor controller and then connect the VC (red) wire to the same leg. This completes the power supply for the micro:bit and motor controller.
When programming the micro:bit, or powering it from the micro USB lead, remove the VCC wire from the battery to the voltage regulator from the breadboard. But reconnect when the robot is ready to run.
The last step is to connect the motors to the screw terminals of the L9110S motor controller. Don’t worry about the polarity, motors do not have a polarity.
For this part of the project we shall use the Makecode editor found on ,the micro:bit website microbit.org.
Create a new project and for now ignore the “on start” and “forever” blocks.
To control the robot we shall create a series of functions that store a series of commands. To use them we simply call the function and the function runs. To make a function we need to go to Advanced >> Functions and click on “Make a Function…”
In the Edit Function window, change the name of the function from “doSomething” to “stop” and click Done.
The code for the function is made using blocks from the Pins section. Here we see the blocks to turn off pins 0,1,2,8, which will turn off any running motors. Then the “show string” block from Basic will print a STOP message to the LED matrix.
Let's make another function, again go to the Functions blocks and make a new function. But this time our “forward” function requires a parameter (an extra instruction) which is going to be a number between 0 and 1023. Click on Number and then Done.
To write the code for the forward function we use “analog write pin” from Pins. We number the pins to match those used to control the motor (0,1,2,8) and then set pins 0 and 8 to zero. But pins 1 and 2 will use the parameter number. To use this drag the red “num” block next to “forward” and drop it over the correct space.
The process is repeated to create a function to turn the robot left. Note that the pins that will receive the parameter are different as to turn left, we need the right wheel to go forward, and the left wheel to move backwards.
The next function will turn the robot to the right.
The final function will trigger the robot to reverse.
With the functions created, lets create a short test script that will call each of the functions in turn, so that we can check that our robot is working. Inside “on start” we shall use the “call” blocks from Functions and call each of the functions. We start with “stop”, then forward at half speed (512) then use a pause from Basic to force the robot to travel for 2 seconds. And the process carries on with each function call, and pause until we reach the end and add another stop.
Download the code and copy it to your micro:bit. When it has finished, remove the micro USB lead and connect the micro:bit to the 3.3V output of the voltage regulator. Your robot will come to life and start driving around. Check that it follows the correct sequence, forward, backward, left, right. If a motor moves in the wrong direction, unscrew the wires from the motor controller and swap them around.
When you are happy, change the sequence of code so that your robot draws a square, navigates around the room, delivers you food! This is your robot, and it will do what you tell it to do!
To take this project further we can code the robot using MicroPython using Mu. Mu can be downloaded by visiting https://codewith.mu/ and installing for your operating system.
from microbit import * motorA1 = pin0 motorA2 = pin1 motorB1 = pin2 motorB2 = pin8
We start writing the code for the robot by importing the micro:bit library and then create four variables that refer to the connections between the micro:bit and the L9110S motor controller.
def stop(): motorA1.write_digital(0) motorA2.write_digital(0) motorB1.write_digital(0) motorB2.write_digital(0) display.scroll("STOP")
The next step is to write a series of functions that will group the instructions needed to perform a task and by calling the name of the function, the task is completed.
The first function is called “stop” and it will turn off all of the pins connected to the motor controller, ensuring that the motors stop moving. Lastly we scroll “STOP” across the LED matrix of the micro:bit.
def backward(speed): motorA1.write_analog(speed) motorA2.write_analog(0) motorB1.write_analog(0) motorB2.write_analog(speed)
The next function will drive the motors backwards (reverse) and the way in which our motors are wired we turn on A1 and B2 and turn off the other connections. This will flip the motors to turn in the direction that we require. This function also takes an argument (an extra instruction) which in this case is the speed, as a value between 0 and 1023.
We then create more functions.
For forward
def forward(speed): motorA1.write_analog(0) motorA2.write_analog(speed) motorB1.write_analog(speed) motorB2.write_analog(0)
For turning left
def left(speed): motorA1.write_analog(0) motorA2.write_analog(speed) motorB1.write_analog(0) motorB2.write_analog(speed)
And finally to turn right.
def right(speed): motorA1.write_analog(speed) motorA2.write_analog(0) motorB1.write_analog(speed) motorB2.write_analog(0)
stop() forward(512) sleep(3000) backward(512) sleep(3000) left(512) sleep(3000) right(512) sleep(3000) stop()
In the last section of code we create a sequence that will test each of the functions to ensure that our robot has full movement. For functions that involvement movement, for example forward, we pass a value to control the speed of the motor. We chose to use 512 as this provides half speed for the motors. Fast enough to move around freely, but the robot will not travel too far. Between each function we add a sleep so that the robot performs the movement/task for a set amount of time.
Flash the code to your micro:bit and check that the robot performs the sequence of code. If the motors move in the wrong direction, simple unscrew their wires from the motor controller and swap them over. Re-test and when happy design a new sequence to perform a cool task.
So there we have it, one simple, cost effective robot which can be programmed in Makecode or MicroPython. This is your robot, decorate and add to the project, have competitions with your friends and their robots and above all else. Have fun building new robots!