Controlling DC Motors Using Python With a Raspberry Pi (40 pin)

Keith Weaver

In this tutorial I will show you how to connect two motors to your Raspberry Pi. Doing so will allow your Raspberry Pi to interact in the real world, making it possible to build a robot, turn on a fan on a hot day or even drop a treat for your cat or dog while your away.

About

The simplest way to understand how this works is by seeing. I’ve included a number of shots of me building the system at each step. I am following this Controlling DC Motors Using Python With a Raspberry Pi tutorial but had to modify it slightly to work with a 40 pin Raspberry Pi instead of a 26. What do I mean?

Pen is pointing to the pins on the bottom of the Raspberry Pi

Parts

  • A Raspberry Pi with SD card preinstalled with Raspbian
  • A Breadboard to connect everything on
  • An L293 motor driver chip
  • Jumper cables to connect everything up (Male to male and female to male)
  • One or two DC motors
  • 4x AA batteries and holder

Connecting First DC Motor One

First steps involve wiring up the power through the L293 motor driver. It doesn’t matter with the 26 pin or 40 pin. The power in and power out are the same pins. From the bottom right corner, the first one and the third one. Here is drawing of the layout:

Image from https://business.tutsplus.com/tutorials/controlling-dc-motors-using-python-with-a-raspberry-pi--cms-20051

Here are some images of current setup:

Next involves setting up the GPIO’s, this is a difference depending on the number of pins. Here is a description of each pin:

We will use GPIO 23 (Pin 16, 8 pins over), GPIO 24 (Pin 18, 9 pins over) and GPIO 25 (Pin 22, 11 pins over). Be careful about the ground pin (Pin 20). In my circuit, this is the outside row of pins. You’ll have to hook up these GPIO’s to the proper pins of the motor stepper.

I’ve coloured each wire differently. Purple is for GPIO 23 and it plugs into pin 4 on the motor stepper. Green is for GPIO 24, next to GPIO 23 and plugs into pin 2. Yellow is for GPIO 25, one away from GPIO 24 and plugs into pin 1 of the motor stepper. Images of my circuit below.

Image from https://business.tutsplus.com/tutorials/controlling-dc-motors-using-python-with-a-raspberry-pi--cms-20051

Now it’s time to add the power. Add the battery holder wires to the positive bar and negative bar. Images below.

And the final step for the first motor is to add the actual motor. This gets added to the only pins (on one side) that have not been filled. As of now, it goes green to GPIO 24, yellow to GPIO 25, empty, negative power out, negative power out, empty, purple to GPIO 23, and positive power in.

The actual motor won’t work with the easy wires. You’ll need to strip away the wire to connect to the DC motor.

Now plug in your Raspberry Pi and you should SSH into your Pi. I’m gonna follow my gist here. You will have to install a pip library called l293d that makes it super simple. Run in the terminal

pip install l293d

or maybe

sudo pip install l293d

And create a .py file with the contents

import l293d.driver as l293d# Motor 1 uses Pin 22, Pin 18, Pin 16
motor1 = l293d.motor(22,18,16)
# Run the motors so visible
for i in range(0,150):
motor1.clockwise()
l293d.cleanup()

Then you should hear the DC motor run. You can increase the range of the for loop to verify the motor is running. I changed mine to 1500 .

If your circuit is not running, verify the connections of all wires and verify you have the correct GPIO pins.

Adding a Second Motor

To add a second motor to this circuit will involve hooking up to the other side of the motor driver. This will be similar to the first side, connecting three GPIO’s to some pins.

You should have the two middle pins (13, 12) connected to the negative rail. The next step is to connect GPIO 17 (Pin 11), GPIO 27 (Pin 13), and GPIO 22 (Pin 15) to the motor stepper. GPIO 22 gets connected to pin 9 (top right) on the motor stepper. It’s one of the green wires in the photos below. GPIO 27 connects to pin 10 on the motor stepper. It’s the blue wire in the images below. The last connection, GPIO 17 to pin 15 on the motor stepper (second to the top left).

The last missing step is the motor. We need to add the DC motor to the circuit. This is similar to the first motor with stripping two wires and putting them into the open spaces. Here is a diagram

Image from https://business.tutsplus.com/tutorials/controlling-dc-motors-using-python-with-a-raspberry-pi--cms-20051

The diagram is of 26 pin. My Raspberry Pi has 40 pins so it’s not the same pins.

The next step involves coding. SSH back into your pi and create a new .py (Python) file.

import l293d.driver as l293d# Motor 2 uses Pin 15, Pin 13, Pin 11
motor2 = l293d.motor(15,13,11)
# Run the motors so visible
for i in range(0,1500):
motor2.clockwise()
l293d.cleanup()

This should run just the second motor. To run both motors, combine the scripts (you only need to import the library once).

import l293d.driver as l293dmotor1 = l293d.motor(22,18,16)
motor2 = l293d.motor(15,13,11)
for i in range(0,10):
motor1.clockwise()
motor2.clockwise()
l293d.cleanup()

More

Here is more related content

Keith Weaver

Written by

keithweaver.ca

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade