One of the simplest classic projects a maker can spend their time on is building a robot buggy with the Raspberry Pi. If somebody told me I could build robots with the Raspberry Pi, I would have gotten into it sooner! This article will show you how you can build a robot buggy and program it to move around under your control with simple Python commands!

Materials needed:

  • Raspberry Pi 3
  • Motor controller board
  • 2 × 3V – 6V DC motors
  • 2 × wheels
  • 1 × AA battery holder (for 4 AA batteries)
  • 4 × AA batteries
  • Ball caster
  • Wire or jumper leads
  • A USB Battery pack
  • Screwdriver
  • Soldering iron and solder
  • Wire strippers
  • Small cardboard or plastic box and glue/tape

Optional materials:

  • Breadboard
  • VL53L0X time-of-flight range finder or ultrasonic distance sensor
  • 2 × line following sensors
  • 18650 battery, battery clip, USB charge/discharge board (or USB power bank)

Software:

  • The latest version of Raspbian

Additional Extras:

  • Small cardboard box
  • Adhesives (duct tape/putty/glue)

Assembling your motors and board

The first step to building your robot buggy pi is connecting the motor controller to the Raspberry Pi, the battery pack, and your two motors. This will test if your components are all working properly. For this project, we will be using the L298N Dual H Bridge DC Stepper Motor Driver Controller board. Don’t worry if you don’t have the exact model that will be used in this article, because these instructions will be applicable to most motor controller boards. You can check your board’s documentation if you are using a different model.

Soldering wires to your motors

If you have managed to buy motors that have wires, you can skip this part! However, most motors on the market do not have wires attached to them so you will need to solder them on yourself.

  1. Strip the ends of your wires to reveal the metal core
strip wires
  • For easier soldering, take the plastic clip off from your motor (you can use a screwdriver for this).
remove clip
  • Solder your wires to the terminals of your motor and reattach the plastic clips you removed earlier.
solder wires
  • In order to ensure that the wires do not touch the motor’s metal casing, trim them off. You may also wrap the ends of your motors with tape for durability.

Connecting the Motors to the Board

Ready your small screwdriver to connect your motors to your board.

  1. Proceed to loosen the screws in terminal blocks OUT1, OUT2, OUT3, and OUT4. If your terminal blocks are labeled differently, consult the documentation for your board.
  2. Strip the ends of the wires and insert the stripped ends into the terminal blocks. If needed, you may cut off the male/female ends.
inserted wires
  • Tighten the screws to the terminal blocks to ensure that the wires stay in place.
terminal block

Powering the Motors

The raspberry Pi alone will not be able to supply enough power for your motors. This is where your AA batteries come in!

  1. Grab your screwdriver and loosen the screws for terminal blocks VCC, GND, and 5V.
  2. With your AA battery holder in-hand, insert the red wire into terminal block VCC and the black wire into terminal block GND (these are not interchangeable).
battery holder
  • Once again, tighten the screws to ensure that your wires stay in place.
battery terminals

Connecting the Board to your Raspberry Pi

You will need to wire the board to the Raspberry Pi for this project to work. There are boards that will require you to follow a different procedure for connecting, and there are boards that act as a hat that you can simply place onto the Raspberry Pi GPIO pins. You may check your board’s documentation if you are not using a different board than the one being used in this article.

For our board, we will be utilizing pins labeled In1, In2, In3, and In4 as well as the two GND pins. The GPIO pins that you will be using on your Raspberry Pi is completely up to you! In this article, we will be using GPIO 7, 8, 9, and 10.

  1. Grab the five female-to-female jumper leads and connect your preferred Raspberry Pi GPIO pins to the motor controller board pins. If the board you’re using does not have a GND pin, you may strip the end of a female-to-female and insert it into terminal block GND that supplies your battery pack. Make sure it’s secure!

The connections are as follows:

7 – In1

8 – In2

9 – In3

10 – In4

GND – GND

GPIO to board

Configuring Directions

It is time to assign a direction to your motors! To configure your left and right motors, you will need to know which way they go forward and which way they go backwards.

  1. Pick one of your motors and use a marker pen to label it ‘right.’ You will want to draw an arrow to configure which way it will go forward. Repeat this process with your other motor but label it ‘left.’
labeled motors
  • Open mu (this can be found in the Raspberry Pi programming menu), and type
from gpiozero import Robot
robby = Robot(left=(7,8), right=(9,10))


in order to import the Robot class and create a Robot project. ‘Robby’ stands for the robot’s name, but you can name it whatever you want to name it!

  • Save the file as ‘robby.py’ or whatever you decided to name your robot.
  • Click Run!
  • Now open a python shell (do this by clicking the terminal icon located in the taskbar, which is at the top of the screen), type ‘python,’ and press Enter.
  • Type
robby.forward()

to find out which way the motors turn. If you wish to stop them, type

robot.stop()
  • This next step will allow you to determine which motor is left and right. Type
robby.forward(0.4)
robby.right(0.4)

to do this (the 0.4 will make the motor go slower than usual, and make it easier for you to see which way they turn). The motor that changes direction is the right-hand motor.

  • If you guessed correctly and right-hand motor is indeed the motor you labeled ‘right,’ then you do not have to changed anything. However, if the right-hand motor is the one you labeled ‘left,’ then you will need to change your Robot object in your file to swap the left and right pin numbers:
## e.g. change
robby = Robot(left=(7,8), right=(9,10))
## to
robby = Robot(left=(9,10), right=(7,8))
  • We will now proceed to check if you have the forward and backward movements set up correctly. Again, drive both of your motors forward with robby.forward(0.4) and check if both motors are turning as follows:
direction of motors

If the right-hand motor isn’t turning as shown, you will have make another change.

## e.g. change
robby = Robot(left=(9,10), right=(7,8))
## to
robby = Robot(left=(9,10), right=(8,7))

If the left-hand motor is turning in the wrong direction, however, do the same but replace the right-hand pin numbers with the left-hand pin numbers.

Assembling the robot

Keep in mind that you do not have to follow these directions if you wish for your robot body to look differently. However, keep in mind that:

  • The body needs to be big enough for the Raspberry Pi, the motor controller, and the batteries to fit into.
  • The body needs to be able to accommodate mounting the wheels.

The Raspberry Pi website recommends that you build a prototype chassis first in order to grasp a feel of what an appropriate robot body is. You can then learn to make better bodies with laser-cutting or 3D printing! This article, however, will utilize a cardboard box for now.

  1. Place your motors into the box, preferably arranged in the way you want them to stay in permanently. Mark where you want the motors’ axle to pass through—and make sure your wheels have enough room to spin around!
chassis-1
chassis-2
  • Poke holes through the sides of the box where you will fit the motors’ axles into.
chassis-3
  • To make sure the motors are held in place, use an adhesive putty or tape to secure them.
chassis-4
chassis-5
  • When you are content with the motor placement, proceed to attach the wheels to the axles.
chassis-6
  • Once you have attached the wheels, you can screw a ball caster at the front section as a third wheel!
chassis-7
  • You can use a power bank to power up your Raspberry Pi!
chassis-8

Enjoy your Robot Buggy Pi!

All images were taken from https://www.raspberrypi.org/