Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:IC L293
Breadbord and jumper cables or
Soldering equipment, vector board, cables and connectors
2 DC motors

Simple Motor Board for the Raspberry Pi with IC L293d and software pulse-width modulation

In this tutorial, the direction and speed of two motors are controlled independently with the Raspberry Pi using the motor driver IC L293d and software-based Pulse-Width-Modulation(PWM). Pulse-Width-Modulation is a method for controlling the frequency and duty cycle of a signal. A PWM duty cycle of 10 for example means that the signal is there for 10% of the interval time.

The Raspberry Pi 2 (and B+) supports two hardware PWM pins, but in this tutorial the more simple software-based PWM is shown. Check out Raspi.tv for a great introduction on software-PWM and Adafruit and their nice tutorial on using hardware PWM.


All pin numbers are counted per board numbering.


L293d motor driver breadboard

L293d motor driver breadboard schematic

Circuit scheme
MOTSUP is a connector for the power source of the motors. The IC L293 (data sheet) can manage an input voltage between 4.5 and 36 Volt; select a power source after checking the motor specification. For the L293d, the motor must draw maximally 650mA.

Motorboard Schematic

Motorboard Schematic

Run the motors
Make sure all connections are right, then download and execute the motor controller script with

Code explained
In the Motor Class of motor_control_l293_pwm.py, a motor is setup in the __init__ function:

First, the pins are setup. Then, two software PWM instances with a frequency of 100 hertz are created for the forward and backward pins.

The duty cycle is initialized with 0 in start for having the motors at rest. The control pin which turns a motor on or off is set to on, because the motor speed is regulated over the software PWM duty cycle.
The function forward then simply sets the backward duty cycle to zero and the forward duty cycle to the given speed:

Now, a motor can be created and controlled with

This will run motor1 directed forward with a speed of 90% of the maximum.

The complete script
Click on Full Script to show all of the code.

Permanent board
The permanent board is easily assembled on a stripe board:

Assembled Motorboard

Assembled Motorboard

Gerber files
For printing or etching the circuit board, check out these Gerber files:

RasPi.tv introduction on software-PWM: http://raspi.tv/2013/rpi-gpio-0-5-2a-now-has-software-pwm-how-to-use-it
Adafruit tutorial on using hardware PWM: https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-dc-motor/lm293d
L293 data sheet: http://www.ti.com/lit/ds/symlink/l293.pdf

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


Time limit is exhausted. Please reload CAPTCHA.