How to Make a Wheeled Robot Using Raspberry Pi

In this post, I will show you how to make a wheeled robot using Raspberry Pi as the “brain” of the robot.

Special shout out to Matt Timmons-Brown for this project idea. He is the author of a really good book on Raspberry Pi robotics: (Learn Robotics with Raspberry Pi). Go check it out!

Requirements

Here are the requirements:

  • Make a wheeled robot using Raspberry Pi as the “brain” of the robot.

You Will Need

wheeled-robot-rpi-9

The following components are used in this project. You will need:

Directions

Building the Robot’s “Body”

Let’s start by building the body of the robot.

Grab the two 8×16 Lego plates, and place them apart from each other.

wheeled-robot-rpi-10

Connect the two 8×16 Lego plates with the two 2×8 Lego plates. Place them across to form a bridge.

wheeled-robot-rpi-11

Connect the other two 2×8 Lego plates to the underside of the 8×16 Lego plates to form a sandwich.

wheeled-robot-rpi-12
wheeled-robot-rpi-13

Solder male-to-male wires to both terminals of each motor (they might already be soldered). If you need a quick tutorial on how to solder, check out this video, or just Google “How to Solder”:

wheeled-robot-rpi-14
wheeled-robot-rpi-15
wheeled-robot-rpi-16
wheeled-robot-rpi-17

Pop the tires on to the white rod on both motors. Give it a strong push in there. The wheels should be on the other side of the motor terminals.

wheeled-robot-rpi-18

Stick the motor wires up through the gap in the robot body.

Mount the motors with tire to the underside of the robot’s body so that the tires are exactly in the middle of the body. Make sure the tires are exactly parallel to each other.

wheeled-robot-rpi-19

Secure the motors to the body of the robot using your hot glue gun (100W setting). If you don’t want the motors to be permanently stuck to the robot’s body, you can use Velcro or Scotch permanent mounting tape.

wheeled-robot-rpi-20
wheeled-robot-rpi-21
wheeled-robot-rpi-22

Stabilize the robot by adding five 2×4 Lego bricks to both the front of the body.

wheeled-robot-rpi-23

Mount the Raspberry Pi battery pack to the underside of the robot, slightly off-center of the body, using Velcro or Scotch permanent mounting tape. The small cable of the battery pack should face the front of the car.

Mount the 4xAA battery holder to the battery pack. Use Velcro or Scotch permanent mounting tape to secure it into place. Make sure that you are still able to reach the ON/OFF switch of the 4xAA battery pack.

wheeled-robot-rpi-24

Feed the negative (black) and positive (red) leads through the gap in the robot body.

wheeled-robot-rpi-25

Strip 1-2 cm of insulation off the end of the battery pack wires using the wire strippers.

Wrap the red and black wires of the battery pack around male-to-male jumper wires.

Solder the wires together so that they remain in place.

Apply black electrical tape around the connection once it has cooled.

wheeled-robot-rpi-26
wheeled-robot-rpi-27

Giving the Robot a “Brain” by Adding the Raspberry Pi

Our robot needs to have a brain. Otherwise, it is just a bunch of plastic parts that can’t do anything useful. In this project, we’ll use the Raspberry Pi as the robot’s brain.

Grab some Velcro and stick the Raspberry Pi on top of the front end of the robot’s body. Make sure it looks exactly like the image below.

wheeled-robot-rpi-28

Grab some Velcro and stick the 400-point solderless breadboard on the back end of the robot, opposite to where the Raspberry Pi is located. You could also peel off the back of the sticker on the solderless breadboard.

wheeled-robot-rpi-29
wheeled-robot-rpi-30
wheeled-robot-rpi-32

Giving the Robot a “Nervous System”

Now that the robot has a brain (Raspberry Pi mounted on the front of the robot) and a body, it needs a “nervous system,” communication lines that enable the brain to transmit signals to and from different parts of its body. In the context of this project, those communication lines are the wires that we need to connect between the different parts of the robot we’re building.

Setting up the Breadboard

Sink the 16 pins of the L293D motor controller down into the holes of the solderless breadboard so that the controller straddles the gap that runs the length of the breadboard.

Here is the diagram of the L293D.

L293D-with-motors

Put pin 1 (the pin just to the left of the half-circle notch in the L293D into pin e3 of the solderless breadboard. You’ll have to bend the legs a bit of the L293D to get it to sink down. Note: Ignore the AAA on the batteries below. They are actually AA.

pin1
wheeled-robot-rpi-33
wheeled-robot-rpi-34

Here is the pin diagram of the Raspberry Pi.

rpi_pin_diagram_2

Power up one set of positive/negative rails of the solderless breadboard:

  • 5V pin (pin 4) of the Raspberry Pi connects to the red (positive) power rail of the breadboard using a male-to-female jumper wire.
  • Connect the Ground pin (pin 6) of the Raspberry Pi to the blue (negative) power rail of the solderless breadboard.
pin2
wheeled-robot-rpi-36

Power up the other set of positive/negative rails of the solderless breadboard:

  • Connect the blue (negative) power rail to the other blue (negative) power rail using a male-to-male jumper wire.
  • Put the red positive lead of the 4xAA battery holder into a hole on the unused red (positive) rail of the solderless breadboard.
  • Put the black lead of the 4xAA battery holder into the blue (negative) rail of the solderless breadboard.
pin3
wheeled-robot-rpi-38
wheeled-robot-rpi-39

Connecting the 16 Pins of the L293D

Here is the diagram of the L293D motor controller.

L293D-with-motors-1

The L293D motor controller needs a power supply:

  • Connect pin 16 (vss 1) to the 5V red (positive) power rail of the solderless breadboard, the rail that is powered by the Raspberry Pi. This pin is the one that will provide power to the L293D motor controller. You can stick a male-to-male pin in g3 of the solderless breadboard and connect that to the red rail.
  • Connect all the GND pins of the L293D (pins 4, 5, 12, and 13) to the closest blue (ground) power rail of the solderless breadboard.
pin4

The motors need a power supply:

  • Connect a male-to-male wire from the red 6V power rail (the rail connected to the 4xAA battery pack) to pin 8 (vcc) of the L293D integrated chip.
pin5

In order for the motors to accept commands from the Raspberry Pi, we need to connect both enable pins (pins 1 and 9) of the L293D to red (positive) 5V power rails. Here are the steps:

  • Take a male-to-male jumper wire and make a connection between pin 1 of the L293D and the the red (positive) rail of the breadboard (the one connected to the 5V pin of the Raspberry Pi).
  • Take a male-to-male jumper wire and make a connection between pin 9 of the L293D and the the red (positive) rail of the breadboard (the one connected to the 5V pin of the Raspberry Pi).
pin6


We need to connect the motors to the output pins of the L293D.

  • Motor 1 (Right Motor)
    • Connect one of the leads to Output 1 (pin 3) of the L293D.
    • Connect the other lead to Output 2 (pin 6).
  • Motor 2 (Left Motor)
    • Connect one of the leads to Output 3 (pin 11) of the L293D.
    • Connect the other lead to Output 4 (pin 14).
pin8

Now, we need to connect the input pins of the L293D to the Raspberry Pi. There are two input pins for each motor.

  • Connect Pin 11 (GPIO 17) of the Raspberry Pi to pin 2 (Input 1) of the L293D.
  • Connect Pin 12 (GPIO 18) of the Raspberry Pi to pin 7 (Input 2) of the L293D.
  • Connect Pin 13 (GPIO 27) of the Raspberry Pi to pin 10 (Input 3) of the L293D.
  • Connect Pin 15 (GPIO 22) of the Raspberry Pi to Pin 15 (Input 4) of the L293D.
pin7

Insert fresh AA batteries into the 4xAA battery holder.

wheeled-robot-rpi-35

Whew! That was a lot of work. If you made it this far, congratulations! You have completed construction of your Raspberry Pi wheeled robot.

In order for it to do something useful (e.g. move from one place to another), we need to program its brain, the Raspberry Pi. We will tackle this in the next post.

How to Read Input from a Push Button Switch on Raspberry Pi 3 Model B+

In this post, I’ll show you how to read input from a push button switch on Raspberry Pi 3 Model B+. This project shows you how to use the Raspberry Pi’s GPIO pins as an input (instead of an output) to receive information from the outside world.

Requirements

Here are the requirements:

  • Detect if a switch button is pressed.
  • When button is pressed, print “Button is pressed!”.
  • When button is not pressed, print “Button is not pressed!”.

You Will Need

push-button-2

The following components are used in this project. You will need:

Directions

Set up the Raspberry Pi

Set up the Raspberry Pi as explained in this video:

Wire the Push Button Switch to the Breadboard

push-button-3
push-button-4

Write the Program and Execute

Now we need to write a program in Python.

I have a folder in my Home Directory named robot. I get to this directory by opening up a terminal window in Raspberry Pi and typing:

cd robot

Now, we open up the Nano text editor to enable us to write the Python program. We name it push_button.py. Here is the terminal command:

nano push_button.py

We type in this python code:

import gpiozero  # We are using GPIO pins

button = gpiozero.Button(17) # GPIO17 connects to button 

while True:
  if button.is_pressed:
    print("Button is pressed!")
  else:
    print("Button is not pressed!")

We then press CTRL-X, then Y, then press Enter to save the program and return to the terminal.

To run the program, we type:

python3 push_button.py

When you press the push button switch, you should see a message that says “Button is pressed!”. Otherwise, you will see a message that says “Button is not pressed!”.

push-button-5
push-button-1

To stop the program, you press CTRL-C.

How to Blink an LED on Raspberry Pi 3 Model B+

In this post, I’ll show you how to blink an LED on Raspberry Pi 3 Model B+. This project shows you how to use the Raspberry Pi’s GPIO (General Purpose Input Output) pins as an output to manipulate an external device (in this case the LED).

Requirements

Here are the requirements:

  • Make an LED blink on Raspberry Pi 3 Model B+.

You Will Need

The following components are used in this project. You will need:

Directions

Set up the Raspberry Pi

Set up the Raspberry Pi as explained in this video:

Explore the Raspberry Pi (Optional)

From the Raspberry Pi desktop, click the Pi logo, and go to Preferences -> Raspberry Pi Configuration. Make sure your settings look like the image below and reboot (we’ll make use of these settings in future posts):

blink-led-11

Before we dive into the LED project, let’s have a look at the Raspberry Pi terminal.

The terminal is a way to communicate with your computer. Back in the 1980s and early 1990s when I first started using computers, the command-line interface of the terminal was the main way to send commands to your computer.

Back in those days computers did not have the processing power they have now. If you were born in the 1990s or later, you probably have only interacted with your computer via a graphical user interface. I like to use the command-line interface for robotics projects because it is more efficient, and you can tell the computer exactly what to do.

To open the terminal, click the Raspberry Pi logo in the upper left of the Raspberry Pi desktop and go to Accessories -> Terminal.

That black window you are looking at is the terminal. Typing ls will display all the files and folder in that directory. Blue items are directories. Green text shows our username (i.e “pi”).

blink-led-12

To change to a directory, you use the cd Directory Name command. For example cd Documents, gets you to the Documents directory.

Determine What Resistor to Use

Find out what the forward voltage is of your 5mm LED. Forward voltage is the minimum voltage required in order for the LED to light up. The forward voltage for my red LED is 1.8-2.2V.

Raspberry Pi is powered by 5V micro USB (2.5A). Each GPIO (General Purpose Input Output) pin supplies 3.3V (our source voltage) and can provide 16mA of current. Since 3.3V > 2.2V, we know that Raspberry Pi has enough voltage to power the LED.

Forward voltage is also the amount of voltage lost when a current runs through the LED. Consider it the “voltage drop” across the LED. To understand the basics of this concept, check out this 3D animation.

Find out what the maximum forward current of the 5mm LED is. Maximum forward current is the maximum current the LED can handle before it is at risk of getting damaged. The maximum forward current of my LED is 20mA (0.02A), which I obtained from the LED’s datasheet.

Now, we calculate the value of the resistor we need using Ohm’s Law (V = I * R):

  • Source voltage in volts = 3.3V
  • Forward voltage of LED = 1.8V
  • Maximum current of LED = 20mA = 0.02A
Resistor in ohms = ((Source voltage in volts) - (Forward voltage of LED in volts)) / (Maximum current in amps) = (Voltage leftover after the LED drops some of it) / (Maximum current)
Resistor in ohms = (3.3 - 1.8) / 0.02 = 75 ohms

So, we need at least a 75 ohm resistor. I’ll chose 330 ohms. The higher the resistor value you use, the dimmer the LED.

What is the current (represented as the capital letter ‘I’) in this case?

(3.3V - 1.8V) = I * 330
I = 4.5mA (which is well under the 20mA max current)

Now, we need to calculate how much power the resistor the LED can dissipate before it fails. We use this equation (P = VI):

Power in watts = Voltage in volts * Current in amps
Power in watts = (3.3 - 1.8) * 0.02A = 0.03 watts

Our resistor is rated at 0.25 watts, so we have more than enough cushion. We are good to go!

Wire the LED to the Breadboard

Here is the diagram to use to wire (using male to female jumper wire) the 330 ohm resistor and 5mm LED to the Raspberry Pi. That kink in one of the LEDs represents the long leg of the LED:

blink_led
blink-led-2

Blink the LED

Now we need to write a program in Python to blink the LED.

I have a folder in my Home Directory named robot. I get to this directory by opening up a terminal window in Raspberry Pi and typing:

cd robot

Now, we open up the Nano text editor to enable us to write the Python program. We name it led_blink.py. Here is the terminal command:

nano led_blink.py

We type in this python code:

import gpiozero  # The GPIO library for Raspberry Pi
import time  # Enables Python to manage timing

led = gpiozero.LED(17) # Reference GPIO17

while True:
  led.on() # Turn the LED on
  time.sleep(1)
  led.off() # Turn the LED off
  time.sleep(1)  # Pause for 1 second

We then press CTRL-X, then Y, then press Enter to save the program and return to the terminal.

To run the program, we type:

python3 led_blink.py

Your LED should be blinking. If it doesn’t blink, try connecting the red positive lead to another GPIO pin on the Raspberry Pi.

blink-led-3
blink-led-4

To stop the program, you press CTRL-C.

You can also try different color LEDs, as shown below.

blink-led-5
blink-led-6
blink-led-7
blink-led-8
blink-led-9
blink-led-10