Building Robots with Python and Raspberry Pi
  • Introduction
  • Chapter1
  • Chapter2
  • Chapter3
  • Chapter4
  • Chapter5
  • Chapter6
  • Chapter7
  • Chapter8
  • Chapter9
  • Chapter10
Powered by GitBook
On this page
  • 4.1 Components
  • (a) Raspberry PI 3
  • (b) Micro SD Card for Raspberry Pi
  • (c) Battery for Raspberry Pi
  • (d) Car Chassis
  • (e) Raspberry Support Board
  • (f) Dual H-Bridge Motor Driver
  • (g) Raspberry Camera Module
  • (h) Camera Mount
  • (i) Bread Board
  • (j) Wires
  • 4.2 Assembling the Robot Car
  • Solder Wires to The Motors
  • Install Motors
  • Install Bottom Board
  • Install Front/Back Boards
  • Install Dual H-Bridge Motor Driver
  • 4.3 Mount Raspberry PI
  • Mount the battery case
  • Mount the Raspberry PI assembly
  • 4.4 Camera Module Installation
  • 4.5 Motor Driver Wiring
  • 4.6 Warning

Chapter4

PreviousChapter3NextChapter5

Last updated 7 years ago

4.1 Components

The following components are needed to create the robot car. You can get these components either from amazon or ebay.

(a) Raspberry PI 3

(b) Micro SD Card for Raspberry Pi

(c) Battery for Raspberry Pi

(d) Car Chassis

(e) Raspberry Support Board

(f) Dual H-Bridge Motor Driver

(g) Raspberry Camera Module

This module is optional and only for advanced students who wants to try computer vision, object tracking robots.

(h) Camera Mount

(i) Bread Board

(j) Wires

4.2 Assembling the Robot Car

Solder Wires to The Motors

Before assembling the car and installing the motors, each motor has to be soldered with two separate wires, as shown in the figure below.

Install Motors

The first step is to install motors to the side board of the car. The following graph shows how to install motors using two screws (that comes with the motor kit).

Install Bottom Board

Install Front/Back Boards

Install Dual H-Bridge Motor Driver

Note that the above diagram uses standoff spacer screws. If those are not available, spacers can be used instead. When you mount, make sure the tallest part (black) is away from the edge (the above diagram shows the opposite).

4.3 Mount Raspberry PI

  1. Put a micro SD card (b) inside Raspberry PI 3 (a) SD card slot.

  2. Mount the Raspberry PI 3 control board (a) to the support board (e) in a similar manner as we mounted the motor control board. Use standoff spacer screws if available, otherwise, use spacers. You may need to drill a few holes for mounting the PI board (a) to the support board(e).

Mount the battery case

Mount the battery case to the top of the support chassis using one screw and one nut.

Mount the Raspberry PI assembly

We have attached the Raspberry PI to the support board. Now, we can mount the assembly to the top of the support chassis (as shown in the picture above) using 4 standoff spacer screws included in the chassis kit.

4.4 Camera Module Installation

Camera can also be installed to raspberry PI. First mount camera module (g) to raspberry PI board like shown be low. Then the camera module can be installed to camera mount (h). Finally the camera mount (h) can be installed on raspberry PI support board (e).

Now, we need to connect the wires before we can finish off the mounting tasks.

4.5 Motor Driver Wiring

Double H driver module is the circuit which controls the motors. It has two DC motor wiring ports and two DC motor control ports.

The two DC motor wiring ports are on the left (green) and right (red) side of the board. Both ports have two connectors labeled A and B. Think A connector as the positive connector and B connector as the negative connector. Since we have four motors on the chassis, we can connect each DC motor wiring port to two motors. The left port can be connected to the left side two motors and the right port can be connected to the right side two motors.

The motors can rotate clockwise or counter-clockwise depending on the DC motor control port values. Each DC motor control port has three PINs. For example, the left DC motor control port has pin I1, I2 and EnableA. Those three pins control the outputs to the left motors. I1 and I2 are digital ports which are used to control the direction of motor, EnableA is connected with PWM port of control board to control the speed of motor. We can connect EnableA directly to a 5V input if the speed of car remains the same.

Port I1 and I2 should be connected to 3.3V (value=1) or Ground (value = 0).

If I1=1 (3.3V) and I2=0 (Ground), the motor rotates clockwise. If I1=0 (Ground) and I2=1 (3.3V), the motor rotates anticlockwise. If I1=I2,the motor stops rotating.

As shown below, the left two motors are connected to the left DC motor wiring port (green). Note that the top wire of the bottom motor (red) and the bottom wire of the top motor (orange) are connected to green connector A. The top wire of the top motor (yellow) and the bottom wire of the bottom motor (brown) are connected to green connector B. The right two motors are connected to the right DC motor wiring port (red). The top wire of the bottom motor(green) and the bottom wire of the top motor(gray) are connected to red connector B. The top wire of the top motor (purple) and the bottom wire of the bottom motor(blue) are connected to the red connector A. This is opposite to the connection for the left side. Make sure they are connected this way, otherwise, the motors will be rotating in a way different from the programming we are going to do later on.

On the bottom side, there are eight wires connected. Three wires (EA/I2/I1) on the left are for controlling the left side motors. Three wires on the right (EB/I4/I3) are for controlling the right side motors. The two wires in the middle are for power supply. Note that we are only using two of the three blue connectors. The connector labeled with "+5V" is not used. Note the bottom 8 wires will be connected to the bread board, so note down the colors since we will be closing the chassis later.

At this point, we can install the top cover for the chassis. The top cover should already have the Raspberry PI assembly attached to it. As shown in the picture below. The top cover can be mounted with 4 screws that came with the chassis kit. After the chassis cover is installed, we can connect the rest of the wires with a bread board. So, stick a bread board (i) to the top of the chassis as shown in the below diagram.

The motors are powered by a 7.2V battery pack (6 AA batteries). The battery pack has two wires for 7.2V positive (red) and ground (black). The 7.2V positive is connected to the motor power supply of the double H driver module (the red cable in the above figure) and the ground is connected to the GND port of the double HD driver module (the central green cable in the above figure). All connections will be done via the bread board.

Now, we will finish connecting all the wires. It's always a good practice to connect the ground wires first.

Please note that the double H drive module and raspberry PI MUST have the same GND. To do this, the following three lines have to be connected through the bread board: 1) the driver module GND 2) raspberry PI GND (PIN 6) 3) GND of the 7.2V battery package

The 7.2V ground wire (black) from the battery pack is plugged into the leftmost column of the top half of the bread board. Remember the 8 wires from the DC motor controller board? The middle green wire (refer to Figure 4.5.5) from the DC motor control board will be plugged into the same column (top half). In addition, we need to connect the ground of the Raspberry PI to the same column. Refer to the Pinout diagram in Chapter 3. We can see that pin #9 is a ground pin. So, we plug one end of the wire (black in the diagram above) to that pin (#9) and the other end to the same column (top half). So, now, the battery pack ground (black wire), the Raspberry PI ground (black wire) and the Motor Control board ground (green in the diagram) are connected together (through the bread board).

The remaining six wires from the motor control board are used for motor controlling. Three wires for controlling left side motor (EA/I2/I1) and three wires for controlling right side motor (EB/I4/I3). We selected the following six GPIO pins from raspberry PI to connect to them:

GPIO2, GPIO3, GPIO4 for left motor and GPIO 27, GPIO 17, GPIO 18 for right side motor.

The three wires for the left motor control board need to be connected to the Raspberry PI pins #3, 5, and 7 (corresponds to GPIO#2, 3, 4). You can connect them directly, or through the bread board. In Figure 4.5.6 above, they are connected through bread board. The purple/white stripe wire (that connects to I1) needs to be connected to PIN#3(GPIO#2). The blue wire (that connects to I2) needs to be connected to PIN#5(GPIO#3), the green wire (that connects to EA) needs to be connected to PIN#7(GPIO#4).

The three wires for the right motor control board need to be connected to the Raspberry PI pins #13, 11, 12 (corresponds to GPIO#27, 17, 18). The black wire (that connects to I3) needs to connect with PIN#13(GPIO#27). The white wire (that connectes to I4) needs to connect with PIN#11(GPIO#17). The gray wire (that connects with EB) needs to conenct with PIN#12(GPIO#18). You can connect them directly or through the bread board. In Figure 4.5.6 shown above, they are done through the bread board.

Below is the connection table between the motor driver module pins and raspberry GPIO PINs.

Driver Module

Raspberry PIN

left_I1

GPIO2

left_I2

GPIO3

left_EA

GPIO4

right_I1

GPIO27

right_I2

GPIO17

right_EA

GPIO18

NOTE:

The connection between I1/I2 and GPIOs has to match with the python code in Chapter 5. If the initial connection is wrong (the motor is rotating in a wrong way), you can either fix the connection by swapping between GPIOs or just update the python code to adjust the port mapping.

Optionally, you can connect a LED. LED has to be used together with a resistor to reduce current. Plug the two legs of the resistor in two different columns (as shown in the diagram). Plug the left leg of the LED in the same column as the right leg of the resistor. Plug the right leg of the LED in a different column. Now, connect one wire (brown in the diagram) from PIN#6 (ground) to the same column as the left leg of the resistor. Connect another wire (red in the diagram) from PIN#8(GPIO#14) to the right leg of the LED.

Now, connect the battery pack powers to the motor control board. You can connect them directly, but it's done through the bread board in Figure 4.5.6 shown above. Plug in the red wire from the 7.2V (6 X AA) battery pack to the leftmost column (bottom half, the top half is used for ground. Don't ever connect power and ground together). Plug in the red wire from the DC motor control board (middle wire as shown in Figure 4.5.5) in the same column (bottom half).

After all the wires are connected, we can conenct a USB keyboard, USB mouse, and a HDMI monitor to the Raspberry PI board. Now, connect the power bank to the Raspberry PI board. It will start booting. Once it starts, it behaves just like a PC. Set up Wifi connection from the Wifi connection menu. Once done, go to a terminal window and type "ip addr show wlan0" and write down the ip address -- the numbers after inet and it's in the format of n.n.n.n. Once we get the ip address, we can connect to it via ssh or use a program such as putty. So, we can detach the keyboard and mouse and the robot will be on its own.

Always remember to shutdown the robot once you are done with it. Type "sudo shutdown -P 0" will shut it down.

4.6 Warning

Always shutdown the robot by typing "sudo shutdown -P 0" before adding or removing hardare components. Otherwise, you risk damaging the robot.

After the four motors have been installed to two side bars. Install the bottom board of the car with four screws.

Figure 4.5.1 Motor Control board

Figure 4.5.2. Motor control board connecting to motors.

Figure 4.5.3 Wiring for left motors.

Figure 4.5.4. Wiring for the right motors.

Figure 4.5.5 Wiring for the motor control board. 8 wires will be connected to the breadboard.

Figure 4.5.6 Wiring diagram for the entire assembly.

The left and right EA/EB pins have to be connected to GPIO4 and GPIO18 as these two GPIO pins can provide hardware PWM (power width modulation) to control the motor powers. More detailed information for PWM can be found on arduino's website .

https://www.arduino.cc/en/Tutorial/PWM