Learn Robotics with Raspberry Pi — Part 2: Chassis and Motors

Neel Kumar
8 min readAug 2, 2021

--

Summary

In the part 1, you prepared the Raspberry PI to program it using VNC, now build a chassis and connect first external board to the Pi, connect 2 motors, and tune them. By the end on this blog you will have a moving robot!

Materials(Not Finished)

MotorShield: This is attached to the Raspberry Pi to control the motors and sensors.

DC Motors, wheels, and cables: The specific model of these don’t matter, but make sure they are DC motors.

Caster Wheels: These will support the robot, but won’t get in the way of the motors.

Batteries: These will power the Raspberry Pi and MotorShield. They can supply the required amount of current for longer time to drive the robot. Standard sized AA/AAA batteries don’t have enough juice to power for longer time.

Battery Pack: To hold the 2 batteries

Battery Charger: To charge the batteries

DC-DC Buck Converter: 2 batteries connected in series will be 7.4 V, which is more that 5 V required to power Raspberry Pi, and the motors. The Buck converter will change the voltage so that we can safely power the motors and Pi.

Multi-Meter: We will need this to tune the voltage converter.

USB-C cable: You will need a USB-C cable that you are willing to cut. This will power the pi

Screws: You will need these to assemble the chassis.

STL files:

3D printing service: You can use any 3D printing service (search “3D Printing Service”) to print the STL files.

My E-Mail(theneelkumar@gmail.com): E-Mail me with the caption Medium Robot 3D Prints, and your address. The first 15 requests will get free of charge, and after that I only charge $7.50(the chassis, motor, and caster wheel mounts) or $5(the motor and caster wheel mounts).

Chassis

Your chassis can be whatever shape or size you want just make sure that it only has 2 motors for movement. I have used only 2 motors (instead of 4), to get full range of motion, to save power, and save ports to connect additional sensors, motors, or servos the robot.

The robot should have supports to keep from falling over. Caster wheels don’t disturb the motors and can be installed with a mount and screws (recommended) or a very strong adhesive. Caster wheels are sometimes to short to support the motors so we need to use 3D printed mounts, I will talk more about 3D printing later.

To create the most basic chassis, you can by a thin piece of wood from Homedepot, or someplace like that, and drill holes in it to accommodate the motors, sensors, and electronics. If you don’t have a drill, download the STL files on my GitHub, and 3D print a base for the robot, with holes to attach the parts.

3D Printing

3D printing was all of the craze in 2017. The stock prices shot up rapidly and it was talked about on every social media platform. This technology has been around since the 1980s, but it wasn’t until a few years ago that 3D printers hit the magical price of a $1000. Even though this is still a lot, it was low enough that private buyers could afford it. I myself was fortunate enough to get my hands on one of these pretty early on. So how do they work? There are a few different 3D printing technologies, but I will only talk about FDM, or Fused Deposition Modeling, 3D printing because it is the most widespread. In an FDM 3D printer there are four main parts, filament, extruder assembly, print bed, and motors.

Source: https://all3dp.com/prusa-i3-mk2s-upgrade/

The most important part in a 3D printer is the extruder assembly. This is the part that is doing the real work. A FDM 3D printer melts plastic in the extruder and puts it on the print bed layer by layer until it forms a 3D object. The extruder takes in the filament, which is a tube of plastic, melts it, and then the motors move the extruder to the right place to lay out the filament.

Now that we have a rough idea of how the printer works, lets print something. First, we have to create a 3D model of whatever you want to print. We will need CAD to do this. CAD, or computer aided design, allows you to create 3D models for many uses. The CAD software I used is Fusion 360, and if you would like to get into 3D printing yourself, you can download it here for free! Once the 3D model is made, you can export it in the STL file format. The 3D printer can then use that to print a 3D model.

Some of you might not want to spend $300-$1000 on a personal 3D printer. In that case, In that case, you can either use a 3D printing service, or you can e-mail me at theneelkumar@gmail.com with the caption Medium Robot 3D Prints. The first 15 requests I get will be free of charge, and after that I only charge $7.50(the base, motor mounts, and caster wheel mounts) or $5(the motor mounts and caster wheel mounts.

Setup(Not Finished)

First, we need to solder wires to the motor and the voltage converter assembly.

Solder the wires from the battery pack to the IN ports of the Buck converter. Solder the red wire to the + and the black wire to the — .

Now prepare the cable to power the Raspberry Pi. For that, cut a USB-C cable in half and cut some of the outer plastic off the black and red wires . Solder the red to + OUT and the black to - OUT on the voltage converter. Then cut two wires and connect them to the other + OUT and — OUT holes on the voltage converter.

Let’s prepare the motors to connect to MotorShield. Each motor has two pins to connect to power supply; cut red and black wires and solder them to the metal pins on the motors. Put the metal parts of the wire in the holes and wrap them around it. Then solder them to the motors.

Now that we are done with soldering, let’s get the robot ready. First, attach the MotorShield on the Raspberry Pi with the included standoffs. Screw in stand offs so that we can attach the Pi to the chassis without the bottom circuitry getting damaged.

To connect the Pi to the chassis insert the standoffs in the holes with the charging port facing in. You may need to push the standoffs in-place because the fit is quite snug. Then, secure the standoffs from the bottom with bolts.

Now attach the motors to the chassis. Insert the screws into the top part of the motors assembly so that they are in position when you go to put the assemble on the main body. Put the motor at the bottom of the assembly and screw it in using the longest screws in the set. Make sure both of the screw heads are on the same side.

After, screwing the assembly to the body, secure them at the top with bolts.

Now we have to support the motors so that the robot doesn’t fall over. Screw the caster wheels to the bottom to the part. You only have to screw the caster wheels from two screws on opposite corners. Secure them using bolts.

Then attach them to the body using the two holes at the top of the caster wheel assembly.

Connect the wires of left motor to MOTOR 1, and right motor to MOTOR 2 of the MotorShield. One pair of red and black wires from the output of the buck converter is connected to the MotorShield, and the USB C cable is connected to the Raspberry Pi for power.

Top of the chassis showing all the components wired together

Tuning

DC-DC Buck Converter

Two batteries connected in series have 7.4 V, but the input voltage of Raspberry PI is 5 V. Below is the input voltage to the Buck converter.

Use a tiny screwdriver to carefully adjust the potentiometer untill the output voltage is little more than 5 V. Notice two pairs of wires connected to the outputs, one pair to power Raspberry PI, and the other for MotorShield.

Output voltage is 5.08 V after adjusting the Buck converter.

Motors

Not all motors are created equal so we have to compensate for that with tuning. To tune the motors run $ python3 MotorCalibration.py . What we are doing here is adding an offset to the left motor. Follow the instructions the program prints out and it will guide you through the tuning process. Also, if you connect the ground terminal of a DC motor to the power pin then the motor will turn in the opposite direction than what is expected. To account for this run $ python3 PolarityCalibration.py . Follow the instructions the program gives you.

The Code

Lets take a look at the code that controls the motors.
Import libraries and set GPIO mode:

import PiMotor
import time
import RPi.GPIO as GPIO
import config
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Define Motors:

m1 = PiMotor.Motor("MOTOR1",2) # Left Motor
m2 = PiMotor.Motor("MOTOR2",1) # Right Motor

Read tuned value from config file(I made another script that takes care of reading and writing to the config file):

cal = config.read("motor")

The code is a bit long because it allows you to choose going forward, backward, left, or right. So let me just show you how to move the motors, and I’ll let you figure out the rest on your own.

Moving robot forward:

m1.forward(40 + cal)
m2.forward(40)

Moving robot backward:

m1.reverse(40 + cal)
m2.reverse(40)

Turning robot right:

m1.forward(40 + cal)
m2.reverse(40)

Turning robot left:

m1.reverse(40 + cal)
m2.forward(40)

What’s Next!

In the next blog I will show you how to tune and use the sensors!

Here are the links to the rest of the blogs:

Part 1: Overview

Part 2: (This Article) Motors and Chassis

Part 3: Sensors

Part 4: Putting everything together

--

--