With the high-level overview covered, let’s move on to the actual L293D wiring. We’ll break down what each pin is used for based on the pinout diagram below:
(Source: Last Minute Engineers)
Power (pins 4, 5, 8, 12, 13, 16)
The L293D needs two independent power sources to work correctly – 5V for logic (pin 16), and 4.5V to 36V for motor power (pin 8). The 5V logic input can be pulled from either of the Raspberry Pi’s 5V pins without issue, but the motor power should never be taken from the Pi’s GPIO. It’s not designed to handle this much current and will likely break if this is attempted. Instead, I recommend using an external power source, like a 9V battery or a handful of AA batteries run in series. Use a higher voltage power source than you think you need for your motors, as the L293D induces a pretty heavy voltage drop (assume ~25%) and any excess voltage can be reduced by using PWM. For reference, I’m comfortably running two small 6V motors with a 9V battery. Also, make sure your motors draw 600 mA of current or less at peak – this information should be available on the motor manufacturer’s website or sales page.
Only one ground pin on each side (12 or 13 and 4 or 5) needs to be connected to the Raspberry Pi’s ground – it doesn’t matter which. Also, since they all go to the same ground, it’s okay to connect both sides to the same ground pin on the Pi.
Motor Connections (pins 3, 6, 11, 14)
These are the pins that you’ll connect to the positive and negative terminals of your DC motors. Motor 1 should be connected to pins 3 and 6, and motor 2 should be connected to pins 11 and 14. Positive/negative orientation doesn’t matter, but it’s easiest to program if they’re both in the same direction – positives on pins 3 and 14 and negatives on pins 6 and 11 (or vice versa).
Motor Direction Inputs (pins 2, 7, 10, 15)
These pins need to be connected to numbered GPIO pins on the Raspberry Pi. I tend to connect mine to pins 23, 24, 25, and 26 just because they’re numbered sequentially, but any 4 pins should work as long as you remember which ones you choose.
These pins control the H-bridge functionality of the L293D. Let’s assume, for example, that you used the same pins that I did for your motors, and motor 1 (pins 2 and 7 on the chip) is connected to pins 23 and 24 on the Pi’s GPIO. Sending a HIGH or LOW signal to both pins at the same time will result in no motor movement, as the H-bridge circuit will be set such that no current will flow across the motor. Sending a HIGH signal to one pin and a LOW signal to the other will make the motor spin in one direction, and flipping which pin you send each signal to will reverse the motor’s direction.
Input vs. spin direction chart.
(Source: Last Minute Engineers)
Motor Speed Inputs (pins 1, 9)
Since these pins utilize PWM, they must be connected to PWM-enabled pins on the Raspberry Pi, which include pins 12, 13, 18, and 19.
If we want the motors to be run with all available power, they can be sent a HIGH signal through this pin and it will work. However, to use PWM, we must enable PWM on the pins and ‘start’ it at a chosen duty cycle, between 0 (off) and 100. The process for doing this is shown in greater detail in the L293 driver program, namely within the ‘halfSpeedBack’ method. Many of the PWM controls shown in the LED wave programs will work here, too.
With this information, you should have everything you need to use your L293D motor driver! I tend to imagine them being used for RC cars, but they can be used to control fan motors, pumps, and any other DC devices that work with PWM signals and reversed polarity– just make sure to double check your wiring before turning anything on. Good luck and have fun!