Pi ROBOT
Pi robots: motors and control
TUTORIALS Build a robot
In part two of his series, Les Pounder shows us how to safely connect and control motors with a Raspberry Pi.
Part Two!
Don’t miss next issue, subscribe on page 16!
At the end of part one, we had built our robot from a kit and soldered wires from the motor A terminals. In this part, we move on to writing code that will control the motors and move our robot. As our robot has four motors, we need to use two motor controllers. One for the rear motors, the other for the front. But which are the best choice for this project, and how do we interface the Raspberry Pi to them? We will create our own Python module (a library of prewritten code) to control the motors and create a test script to ensure it all works.
OUR EXPERT
Les Pounder is associate editor at Tom’s Hardware and a freelance maker. He blogs about hacks and makes at http://bigl.es.
This part can get quite complex – we are working with four motors. Take your time, tackle just one motor at once, and double-check your connections before moving on. Sure, we want to get to the bit where our robot runs around under our command, but we need to put some work in before we reach those giddy delights.
YOU NEED
› Robot kit
› Pi 3B+ or 4
› 4GB
› 2x L298D motor controllers
› 12x female-to-female jumper wires
› USB battery for testing
Code: https:// github.com/ lesp/Linux-Format-Robot/ archive/refs/ heads/main. zip
Motor controllers
We can’t just connect the GPIO of the Raspberry Pi to the motors. The amount of current consumed by a motor is many times what the Pi GPIO can produce. Instead we need something that sits between the Pi and the motors: a motor controller. The controller receives the signal from the Raspberry Pi’s GPIO and triggers its output pins to send power to the motor. In reality, the motor controller is an H bridge, where changing the state of two pins can send the power to alternate motor pins. This forces the motor to spin in either direction, giving us directional control. The most common motor controllers for makers are the L9110S, L298N and DRV8833. But which one do we need?
To determine this we need to look at the datasheet for our yellow DC motors. It seems they have a max current of 800mA. This is the stall current, which is the current consumed by the motor when it is forced to stop – awire may catch in the axle, or the robot becomes trapped. Looking at the datasheet for the L9110S, we see that it can handle 800mA, but only for one motor; two motors could see 1.6A go through and destroy the controller. We tested the stall current for one motor and noted that it reached 900mA, which is a definite strike against the L9110S.