Controlling DC Motors Using Python With a Raspberry Pi

What we plan to do is safely connect one or two motors to the Raspberry Pi with as few components as possible. Once we have the electronics put together on the breadboard, I will show you how to control them easily using Python to first make the motor spin, and then add some control to change the motor direction so we can go backwards.

This guide will require a careful eye to catch any mistakes, and a bit of courage, especially if you are new to the GPIO connectors. I would like to stress I am not responsible for any damage caused to your Raspberry Pi and/or components.

IMPORTANT: Do not connect a motor, no matter how small directly to the Raspberry Pi, it will damage your Raspberry Pi.

The main processor can only supply enough power to light a LED, roughly 20mA. A motor will want at least 400mA of current to start turning.

Controlling DC Motors Using Python With a Raspberry Pi

To get a motor running, you will need:

  • A Raspberry Pi with SD card preinstalled with Raspbian
  • A Breadboard to connect everything on
  • An L293 or SN755410 motor driver chip (I will refer both as L293D in this tutorial)
  • Jumper cables to connect everything up (Male to male and female to male)
  • One or two DC motors rated for 6v
  • 4x AA batteries and holder

If you haven’t looked closely at your Raspberry Pi before, now might be the best time to have a good look. There are 26 pins grouped in two rows of 13, and these collectively are called the General Purpose Input Output header or GPIO for short. These are a mix of four power pins, five ground pins and 17 data pins.

Figure 1. The Layout of the GPIOs with the pin numbering. Pin 1 is the top left labelled 3V3

Some of these data pins have extra functions such as an i2c bus, SPI bus and UART serial connectors, all of which can connect to other hardware to allow the Raspberry Pi to talk to items such as an Arduino, an Analogue to Digital Convertor (ADC) or add-on boards such as a PiGlow or PiFace.

TIP: When working with the GPIO pins, always do this while the Pi is unplugged, as any accident by connecting (or shorting) 2 pins together can cause damage to the Raspberry Pi.

It is important to do this while the power to the Raspberry Pi is off, or disconnected, as you  want to avoid shorting any connectors by mistake.

The first thing you need to do is connect up the power and ground wires. As with most electronics projects, everything that connects together will require a common ground. This is shown with the black wires.

The ground on the Raspberry Pi is physical pin 6. Referring to Figure one this is worked out by starting at the top left with pin 3V3, counting left to right so 5V is pin 2, GPIO 2 (labelled 2) is pin 3 and so on.

Reading pin numbers on Integrated Circuit (IC) chips is easily done by having the notch or dot to the left then starting from bottom left gives us pin 1.

Now add three wires from the GPIO pins to the L293D.

  • GPIO 25–Pin 22 > L293D–Pin 1
  • GPIO 24–Pin 18 > L293D–Pin 2
  • GPIO 23–Pin 16 > L293D–Pin 7

Add the motor:

  • Motor–wire 1 > L293D–pin 3
  • Motor–wire 2 > L293D–pin 6

It is extremely important that you double-check every connection before adding the batteries. Only when you are happy that everything is in place, connect the battery wires to the power rails of the breadboard.

One of the great features of the L293D is it that it can handle two motors independently and each motor can run at different speeds or directions. Using this one IC makes it possible to create a two-wheeled robot capable of turning, going forwards and going backwards easily.

Adding a second motor involves just three additional wires and another motor.

  • GPIO 11–Pin 23 > L293D–Pin 9
  • GPIO 9–Pin 21 > L293D–Pin 10
  • GPIO 10–Pin 19 > L293D–Pin 15

It is important to check and double-check any wiring before adding any power source to your project as some of the wiring can get a bit fiddly it is easy to miss a connection and send 5V in to the 3.3V of the Raspberry Pi.

Controlling DC Motors Using Python With a Raspberry Pi schematic board

Always check your wiring and then check it again!

If you haven’t set up an SD card for your Pi before, it will be worth understanding how to create one by reading the How to Flash an SD Card for Raspberry Pi tutorial, first.

With a freshly created Raspbian SD card in place connect the Raspberry Pi as usual and boot up.

The first two lines tell Python what is needed in the program.

The first line will want to access a module called RPi.GPIO. This module handles all the hard work involved around turning the GPIO pins on and off on the Raspberry Pi.


For more detail: Controlling DC Motors Using Python With a Raspberry Pi

Scroll to Top
Read previous post:
The “Raspberry Pi” Pie

For the "PI/E Day Contest" on instructables, I wanted to make a pie which I have not made before. I...