Build your first robot
Do you want to get started with robotics, but you're not sure how? Start here with our guide to building a very simple Raspberry Pi bot!
Create your robot chassis
Any roaming robot device needs a chassis to hold the motors and wheels in place, and, in this case, to hold your Raspberry Pi. If you already have a chassis then you can skip this step and go onto the next one.
You may have access to a makespace that has a laser cutter, or maybe your school has one; you can ask an adult to help you cut the chassis that we have used in this tutorial. If you don't have access to a laser cutter, you can make the chassis out of strong cardboard.
Build our robot chassis
Unless you have used our robot chassis, build your chassis by following the manufacturer's instructions and move onto Step 3.
Lay out all the parts of your robot chassis.
Connect the wheels to their motor and gears by pushing the white rod into the centre of the wheel.
Take two male-to-male jumper wires and cut off the connectors at one end.
Ask an adult to strip and solder the exposed wires to the metal connectors underneath the motor, one on each side.
Repeat the above steps on the second wheel and motor.
Attach the wheel brackets to the wheel motors with some M3 screws so that your wheels look like the above image.
Using some M2.5 screws attach your Raspberry Pi, with its micro SD or SD card already inserted, to the mount board.
Sandwich the two wheels between the top bracket that your Raspberry Pi sits on, with the smaller bottom bracket. Use cable ties on each of the four corners to hold them together.
Add a castor to the front of the Raspberry Pi mount board with some screws.
Plug your Pibrella into the first 26 pins of your Raspberry Pi that has already been mounted on the chassis.
Finally, connect the jumper cables soldered to the wheel motors to the Pibrella board. With the bot facing you, take the jumper wires from the left hand wheel motor and plug one into input E on the Pibrella and the other next to it in the ground input. Repeat with the right hand wheel motor jumper wires, plugging one into input F and the other into the adjacent input on the ground bank.
Connect to your Raspberry Pi
Now that you have built your Raspberry Pi robot, you will need to start up your Raspberry Pi in order to test that it works, and to write programs to make it move how you want it to.
- Connect your Raspberry Pi to a monitor, keyboard and mouse before inserting the power cable into the micro USB port on your Pibrella.
- Log in and then load the graphical user interface by typing
- Open a
Terminalwindow by clicking on Main Menu, Accessories then Terminal.
sudo idle3 &to load the Python IDLE programming environment.
- Once it has loaded, open a new text editor file by clciking on File and New Window.
- Finally, click on File and Save As, then name your Python file first-robot.py.
Make your robot move
Now you are set up, you can write the code to make your bot move!
Begin by importing the Pibrella library by typing
import pibrellaon the first line.
On the next line type
import time; you will need this library to add pauses in the program.
pibrella.output.e.on() pibrella.output.f.on() time.sleep(2) pibrella.output.e.off() pibrella.output.f.off()
Click on File and Save to save your code or press
Ctrl + Son the keyboard.
Now it is time to test that it works.
Ensure that your robot is turned upside down on a table, so that when you run your program you will see the wheels moving but it will not run off the table. Remember it is connected by cables right now that might cause a problem!
Click on Run and Run Module or press
F5on your keyboard to run your program. You should see the wheels on our robot turn on for 2 seconds and then turn off. Both wheels should now move forward; if one of the wheels moves backwards, reverse the wires for that motor using its corresponding output on the Pibrella.
Make your robot turn
To make the wheels turn and move the robot forward, you only need a simple program that turns on both motors for a period of time. But what if you want to turn right?
The simplest way to turn is to make one motor on the left hand side turn on and keep the right motor switched off. This will turn the robot right.
Add the following code to your Python file at the bottom:
pibrella.output.f.on() sleep(5) pibrella.output.f.off()
Save the file and run your code to see if it works.
Using the big red button to start the program
Let's program the robot to turn in a square formation. You might need to get the robot into position before running the program. It makes sense to use the red button on the Pibrella board to start the movement. First, put the directions into functions.
Open your program and, above the first sequence you wrote to move forward, type:
Then indent the sequence to move forward underneath like this:
def forward(seconds): pibrella.output.e.on() pibrella.output.f.on() time.sleep(seconds) pibrella.output.e.off() pibrella.output.f.off()
Indentation is important in Python. The indented code here tells us that it belongs inside the function defined as
forward. Use four spaces to indent in Python.
Navigate to the next section of code to turn right and type:
Like before, add the sequence of code to turn right into the function you have just defined, so that it is indented by four spaces like this:
def right(seconds): pibrella.output.f.on() sleep(seconds) pibrella.output.f.off()
Now add a function to call the forward and right functions four times in a loop when the button is pressed, like this:
def button_pressed(): for i in range(0,4): forward(0.5) right(0.9)
To make a square, you would need to move forward and turn right four times. Think about how you might do this in Turtle to help visualise what is happening. See the Drawing snowflakes with Turtle resource for more info.
Now a loop is needed to check if the button has been pressed or not. To do this you can use a forever loop in Python called
while True:. At the bottom of your program type:
while True: if pibrella.button.read(): button_pressed()
Save your file and run your code. Disconnect the HDMI cable, keyboard and mouse from the Raspberry Pi but not the power cable! Then find a place for your robot on the floor or table.
Press the red button! Your robot should draw a square.