Wikijunior:Raspberry Pi/Raspberry Pi Motor Control

A tutorial by Mark Cantrill @AstroDesignsLtd 24th June, 2017 cotswoldjam.org

In this tutorial we're going to look at what we need to do control a motor with the Raspberry PI, how a motor driver works and we're going to look at using the PiZ-Moto motor driver for the Raspberry Pi to control two motors. We'll also look at how we might program a small two-wheeled robot. This worksheet focuses on the use of the PiZ-Moto motor driver board.

Connecting the motor(s)
There are two connectors on the PiZ-Moto board, one is for Motor "A" and one is for Motor "B". The connectors are screw terminals, simply push the wires of the motor into the terminals and do up the screw in the top of the connector 'finger-tight'. The connector on the left is for Motor "A", the connector on the right is for Motor "B".

For this tutorial we’re only using one motor but the second motor connector has a couple of LEDs wired into it to show changes in direction and also changes in motor speed through the brightness of the LED. The LEDs are connector to Motor "A" connector, the motor is connected to the Motor "B" connector.

Connecting the power supply
The PiZ-Moto is designed to run off a 6V 4-cell AA battery pack. We’ve included one of these and the wires to connect it with the PiZ-Moto in this tutorial.

But for the purpose of this tutorial, to save us from buying lots of batteries, we've temporarily connected a wire from the Raspberry Pi's 5V pin to the VIN (+) connection on the PiZ-Moto which enables us to run this tutorial off of a 5V USB power supply plugged into the Raspberry Pi.

This is ok for this tutorial where we're only using one small motor like this but we highly recommend that you cut this wire off and power the PiZ-Moto from the battery pack.

Also please note that when powering the PiZ-Moto from the battery pack, the 5V regulator on the PiZ-Moto will provide the Raspberry Pi with 5V. There is no need to connect a USB power supply to your Raspberry Pi when using the PiZ-Moto, infact if you did do this then you could damage the power supply.

2. Understand the functions that you'll need to use

 * Where, speed is: -100 to +100
 * Max speed = 100 or -100
 * Use positive speeds for forwards (e.g. 100)
 * Use negative speeds for backwards (e.g. -100)
 * Use smaller numbers to slow the motor down (e.g. 50 for half speed)
 * Use 0 to make the motor stop
 * And duration is: the length of time in seconds that you want the motor to run for.
 * e.g. 20 will make the motor run for 20 seconds.
 * e.g. 0.5 will make the motor run for half a second
 * If time = 0 then the motor will run until you tell it to stop by setting the speed to 0.

3. Now let’s make the motors do something…
Motor A forwards for 10 seconds

Motor B backwards for 10 seconds

4. Task 1: Write a program to control a robot…
Pretend you have two motors on a two wheeled robot. Write a program to make it go forwards for 5 seconds then backwards for 5 seconds.

5. Task 2: Make your robot turn…
Add a routine to your last program so that your robot turns left (or right) for 1 second before it changes direction from forwards to backwards.

6. Task 3: Make your robot accelerate…
Modify your program so that instead of going forwards at full speed, it increases the speed over a period of 5 seconds.

Other useful information on the PiZ-Moto
Full instructions for the assembly of the PiZ-Moto (if you have a kit) are on GitHub, together with some examples. We've already cloned the GitHub  repository onto the SDCard supplied with the tutorial so you're ready to go! But please keep an eye out for updates.

How to clone  repository onto your Raspberry Pi if it’s not there. Or if you just want to put a fresh copy in a new folder.

To clone  into your home folder (/home/pi) run the following command. This will create a folder called piz-moto that contains the simple  package, instructions and examples.

If you simply want to check for updates, just navigate into the piz-moto folder and run :

For up-to-date PiZ Moto code, from the Terminal, do:

Please note that for the purpose of running this tutorial, the SDCard has been prepared with the piz_moto files cloned to the following folder. You can continue to use it in this folder and run the git pull function to update the files as long as you are inside this folder:

The instructions found inside the piz-moto folder are worth a read as they explain all about the PiZ-Moto. For one thing, it's not just a motor driver… The PiZ-Moto includes simple interfaces to allow you to connect an ultrasonic range sensor to and infra-red photo-sensor.

It also includes a 5V regulator that you can use to power the Raspberry Pi. Plus there are two general purpose outputs that include an on-board series 100-Ohm resistor so you can connect a couple of LEDs direct to these pins.

Have fun with your PiZ-Moto motor driver board for the Raspberry Pi!