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 Add Sound to a Wheeled Robot | Arduino

In this post, I’ll show you how to add sound to a wheeled robot.

Shout out to the late Gordon McComb for this project idea. He is the author of an excellent book that I recommend buying if you’re getting started with robotics: How to Make a Robot.

Requirements

Here are the requirements:

  • Build a wheeled robot that makes sound before it backs up.

You Will Need

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

Directions

Get the Piezo Transducer.

Stick the positive lead of the transducer into cell j26. Stick the other lead of the transducer into cell j29.

Connect digital pin 5 of the Arduino board to cell f26 of the breadboard.

Connect f29 to e29 with a male to male jumper wire, or just make sure that j29 is electrically connected to Ground.

sound-robot

Upload the following sketch to the Arduino breadboard to test the Piezo transducer.

#define SPKR 5

/**
 * Test the piezo transducer 
 * connected to pin 5
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-15
 */
 
void setup() {
}

// Play sounds over and
// over again
void loop() {
   tone(SPKR, 247, 300); //247 Hz, 300ms
   delay(200);
   tone(SPKR, 131, 300);
   delay(200);
   tone(SPKR, 1175, 300);
   delay(200);
   tone(SPKR, 262, 300);
   delay(200);
   tone(SPKR, 1175, 300);
   delay(200);
   tone(SPKR, 131, 300);
   delay(200);
   tone(SPKR, 262, 300);
   delay(200);
   tone(SPKR, 1175, 300);
   delay(200);
   tone(SPKR, 247, 300); //247 Hz, 300ms
   delay(200);
}

Upload the following sketch to the Arduino breadboard in order to cause the robot to make sound just before it backs up.

#include <Servo.h> 

/**
 *  This robot will move around a room and when it 
 *  bumps into an object, it will turn around and 
 *  go in another direction. It will make a noise
 *  just before it backs up.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-12
 */

#define SPKR 5

// Create two servo objects, one for each wheel
Servo right_servo;
Servo left_servo;

// Volatile keyword is used because these variables
// can change at any time without any action having been
// taken by the compiled code. 

volatile int left_switch = LOW;   // Left switch flag
volatile int right_switch = LOW;  // Right switch flag
boolean already_started = false;  

/*   
 *  This setup code is run only once, when Arudino is 
 *  supplied with power.
 */
void setup() {
  // Set the pin modes for the switches
  pinMode(2, INPUT); // Right switch is input
  pinMode(3, INPUT); // Left switch is input
  pinMode(4, OUTPUT); // Pin 4 is ground
  
  // Turn on the internal pull up resistors for the switches
  // Keeps input from floating when the switches are not
  // pressed
  digitalWrite(2, HIGH);     // Right switch default to high
  digitalWrite(3, HIGH);     // Left switch default to high
  digitalWrite(4, LOW);      // Pin 4 default is ground

  right_servo.attach(9);      // Right servo is pin 9
  left_servo.attach(10);      // Left servo is pin 10
 
  // Declare the interrupts
  // attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
  // Interrupt when go from high to low
  attachInterrupt(digitalPinToInterrupt(2), hit_right, FALLING); 
  attachInterrupt(digitalPinToInterrupt(3), hit_left, FALLING);  
  
  already_started = true;  // Bot can now move
}

void loop() {
  if (left_switch == HIGH) {       // If the left switch is hit
    go_backwards();                // Go backwards for one second
    delay(1000); 
    go_right();                    // Turn to the right for one second
    delay(1000);
    go_forward();                  // Move forward
    left_switch = LOW;             // Reset the flag
  }
  
  if (right_switch == HIGH) {      // If the right switch is hit
    go_backwards();                // Go backwards for one second
    delay(1000); 
    go_left();                     // Turn left for one second
    delay(1000);
    go_forward();                  // Move forward
    right_switch = LOW;            // Reset the flag
  }
}
  
// Interrupt routine for left switch bumping into an object
void hit_left() {
  if (already_started)              // Valid if the program has begun
    left_switch = HIGH;             
}

// Interrupt routine for right switch bumping into an object
void hit_right() {
  if (already_started)              // Valid if the program has begun
    right_switch = HIGH;
}

/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  right_servo.write(0);
  left_servo.write(180);
}
void go_backwards() {

  // Make a noise before you go backwards
  tone(SPKR, 247, 300); //247 Hz, 300ms
  delay(200);
  tone(SPKR, 131, 300);
  delay(200);
  tone(SPKR, 1175, 300);
  delay(200);
  right_servo.write(180);
  left_servo.write(0);
}
void go_right() {
  right_servo.write(180);
  left_servo.write(180);
}
void go_left() {
  right_servo.write(0);
  left_servo.write(0);
}
/*
void stop_all() {
  right_servo.write(90); // Tweak the 90
  left_servo.write(90);  // Tweak the 90
}
*/

How to Add Lights to a Wheeled Robot | Arduino

In this post, I’ll show you how to add lights to a wheeled robot so that the light is red when the robot is moving backwards and is green when the robot is moving forwards.

Shout out to the late Gordon McComb for this project idea. He is the author of an excellent book that I recommend buying if you’re getting started with robotics: How to Make a Robot.

Requirements

Here are the requirements:

  • Add lights to a wheeled robot so that the light is red when the robot is moving backwards and is green when the robot is moving forwards.

You Will Need

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

Directions

Get the bi-color LED.

Cut the shorter lead of the LED so that it is 1/4 inches in length.

Get the 300 Ohm resistor. Cut one of the ends so that it is 3/8 inches.

Solder the short lead of the LED to the short lead of the 300 Ohm resistor.

Cut the bottom of the resistor so that its lead is 3/8 inches in length.

Cut the lead of the LED that is not connected to the resistor so that it is the same length as the lead that has the resistor soldered to it.

Insert the LED into the Arduino board. The lead with the resistor goes into pin 11. The lead that does not have the resistor gets inserted into pin 12.

Upload the following code to the Arduino board. you should see the LED flashing red and green.

/**
 * Make a bi-color LED flash red and green.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-15
 */

#define LED_RED 11
#define LED_GREEN 12

/*   
 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
 */
void setup() {
  
  // Define output pins
  pinMode(LED_RED, OUTPUT);  
  pinMode(LED_GREEN, OUTPUT);
 
  // Set output values
  digitalWrite(LED_RED, LOW); 
  digitalWrite(LED_GREEN, LOW);
}

/*   
 *  This code is run again and again to
 *  make the LED blink.
 */
void loop() {
  red_blink();
  green_blink();
}

// Method to blink the red LED
void red_blink() {
  digitalWrite(LED_RED, HIGH);
  delay(250);                          
  digitalWrite(LED_RED, LOW);     
  delay(250);                         
} 

// Method to blink the green LED
void green_blink() {
  digitalWrite(LED_GREEN, HIGH);
  delay(250);                         
  digitalWrite(LED_GREEN, LOW);     
  delay(250);                         
}

Now, upload the following code to the Arduino board. In this code, the LED will flash green when the robot is moving forward, and the LED will flash red when the robot is moving backwards.

#include <Servo.h> 

/**
 * Make a robot whose light is red when the robot 
 * is moving backwards and is green when the robot 
 * is moving forwards.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-15
 */


Servo right_servo;
Servo left_servo;

volatile int left_switch = LOW;   // Flag for left switch
volatile int right_switch = LOW;  // Flag for right switch
boolean started = false;     // True after first start

#define LED_RED 11
#define LED_GREEN 12

void setup() {
  // Set pin modes for switches
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  
  // Set internal pull up resistors for switches
  // These go LOW when pressed as connection
  // is made with Ground.
  digitalWrite(2, HIGH);     // Right switch
  digitalWrite(3, HIGH);     // Left switch
  
  digitalWrite(4, LOW);      // Pin 4 is ground

  right_servo.attach(9);      // Right servo to pin 9
  left_servo.attach(10);      // Left servo to pin 10
 
  // Set up the interrupts
  attachInterrupt(0, bump_right, FALLING);
  attachInterrupt(1, bump_left, FALLING);
  
  started = true;            // OK to start moving
  
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_GREEN, LOW);
}

void loop() {
  if (left_switch == HIGH) {   // If the left switch hit
    go_backwards();            // Go backwards for 0.5 sec
    delay(500); 
    turn_right();              // Spin for 1 second
    delay(1000);
    go_forward();              // Go forward
    left_switch = LOW;         // Reset flag shows bumped
  }
  
  if (right_switch == HIGH) {  // If right switch hit
    go_backwards();
    delay(500); 
    turn_left();
    delay(1000);
    go_forward();
    right_switch = LOW;
  }
}
  
// Interrupt handlers
void bump_left() {
  if (started)              // If robot has begun
    left_switch = HIGH;
}
void bump_right() {
  if (started)             
    right_switch = HIGH;
}

// Motion Routines: forward, backwards, turn, stop
// Continuous servo motor
void go_forward() {
  right_servo.write(0);
  left_servo.write(180);
  led_green();
}
void go_backwards() {
  right_servo.write(180);
  left_servo.write(0);
  led_red();
}
void turn_right() {
  right_servo.write(180);
  left_servo.write(180);
  led_off();
}
void turn_left() {
  right_servo.write(0);
  left_servo.write(0);
  led_off();
}
void stop_all() {
  right_servo.write(90);
  left_servo.write(90);
}

void led_green() {
  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_RED, LOW);      
} 
void led_red() {
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_RED, HIGH);
}
void led_off() {
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_RED, LOW);
}

If for some reason you get a situation where you get the opposite result of what should occur (flashes red when moving forward and green when in reverse), the LED is reversed. Turn it around.

lighted-robot-1
lighted-robot-2

Also, if you are getting a situation where your servos are not moving, it likely means that voltage is insufficient. Changing the location of the servo power wire, the wire that connects the red servo line to the red line of the 4xAA battery pack usually does the trick. If not, get new batteries for the servo.