Please select an option to see content specific to your location and shop online.


Using Raspberry Pi To Control A Motor Via GPIO

Scroll down

We just love our Raspberry Pi. It’s such a flexible friend. There are literally hundreds of fun things you can do with the little critter. And here’s just one: controlling a motor.

I mean, it’s something everyone wants to do at some time, right? So we’ve put together a quick session on how to use your Pi together with an L293D chip to control the direction of rotation of a DC motor – and how you can vary the speed of a DC motor through Pulse Width Modulation (PWM).

As if that wasn’t enough, we’ll also show you how you can program a simple human machine interface to control the system in different ways.

Please, don’t thank us – it’s just what we do…


We’ve split the exercise into three steps of increasing complexity.

Step 1

Let’s start by checking out the L293D motor control chip and seeing basically how we can use it to control our DC motor. On our way, we’ll learn a bit about the IDLE integrated development environment and the Python programming language by entering commands directly at the Python prompt. These commands are used to turn a DC motor “On” and “Off” via the L293D chip.

Step 2

Next, we’ll look at creating a file containing a series of Python commands, saving this file as a “computer program”, and “running” the program. We’ll also explore how Pulse Width Modulation (PWM) controls the speed of our motor – not to mention how the Duty Cycle relates to the power delivered to the motor. It‘s at this stage that we’ll check out the definition and use of functions in Python as a way of simplifying code and reducing repetition.

Step 3

Finally, we’ll look at ideas around human-machine interaction via a simple user interface and show you how can re-programme this interface by tweaking the Python Code to control the system in different ways. Of course, it’s pretty important to be able to write a description of how a system should behave – and draw a system flowchart to help you with the design and writing of the Python code. So we’ll give you the low-down on these tricks too.

We’ve created these exercises using a Raspberry Pi Model B with a 26 Way GPIO Pin header.  Newer models have a

40 Way GPIO header, but the first 26 pins ae the same.

So what’s stopping you? Just check out these files for all our exercises and Raspberry Pi Setup guides. And before long, controlling the direction and speed of your DC motor will be easy as Pi.


Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.