The L293D motor driver is a small integrated circuit that is capable of powering and controlling two small DC motors (<600 mA each) independently. It can control each motor’s spinning direction using H-bridge circuits and their speed using pulse-width modulation (PWM) through the use of a controller, like a Raspberry Pi.
The L293D chip has 16 pins and a small indent on one side of the top of the chip, which is used to indicate chip orientation. Each of the 16 pins on the chip performs a particular function: six are used to power the chip and the motors, six are used to control the motors’ speed and direction, and four are used to connect the motors. The specific pinouts are detailed in the L293D writeup.
H-bridge circuits are simple circuits that are used to control the direction in which electrical current flows across a motor by using a set of four electronically controlled switches. If switches A & B are closed and switches C & D are open, current will flow across the motor in one direction (making it spin, for example, clockwise). Alternatively, if switches A & B are instead opened and switches C & D are closed, current will flow across the motor in the opposite direction, making it spin counterclockwise. The L293D driver utilizes two H-bridge circuits, using one per attached motor to control its spin direction.
Pulse width modulation is a technique used to control the average amount of power that an electronic device receives. It’s a reasonably complex topic, so instead of covering it in detail here I opted to dedicate an entire module to it instead. I would strongly encourage you to read the writeup in that module for more information, though deep knowledge on how PWM works is not explicitly necessary to use the L293D motor driver.
Here’s the short of it: PWM works in percentages. Lower percentages = less power to the electronics = slower motors. For example, let’s say the power source in a motor circuit produces 5V. Should we control this voltage with PWM, a PWM signal of 35% will make a motor act as though it’s only receiving 35% of the 5V power input, or 1.75V, so it will spin slowly. Further, a 0% signal will turn the motor off, and a 100% signal will make the motor spin at full, 5V speed. Dynamically varying the PWM signal will make a motor change speed, which is fundamental to the usage of DC motors in many applications.
L293D motor drivers serve as a great introduction to integrated circuits and datasheets, which must be referenced in order to wire the chip correctly and ensure that the manufacturer’s recommended limitations are not exceeded. As with stepper motors, the module can be as complex or as simple as desired. An understanding of how H-bridges and PWM work is not explicitly necessary, but it can greatly aid in understanding how the chip works and why sending particular signals result in different motor functions.
Conceptual Questions:
What is an integrated circuit?
What is the L293D motor driver? Why is it useful?
What aspects of motor movement does it allow us to control?
Can we power a motor directly from a Raspberry Pi?
What is a datasheet? Why is it useful?
Why can we only use small motors with the L293D motor driver?
Why is chip orientation important? What could happen if we accidentally flipped the chip around?
Advanced Questions:
What is an H-bridge circuit? How does it work?
What is pulse-width modulation? How does it work?
In what situations is pulse-width modulation particularly useful?
To control a DC motor, we must have a way to control both its speed and direction. Using an H-bridge motor driver like the L293D (pictured below), we can independently control both aspects of motor movement for 2 motors at the same time. Motor spin direction is controlled using something called an H-bridge, while speed is controlled with a PWM signal. I would really recommend looking at the PWM writeup before jumping into this one to make sure you have a good background on how it works, but it’s not absolutely necessary as I’ll explain the essentials below.
The L293D may feel daunting to use at first, but breaking it down into smaller pieces will hopefully make things feel more manageable. We’ll start by discussing how H-bridges control spin direction and PWM controls speed at a high level, then we’ll discuss how these functions are controlled on the L293D itself.
An L293D chip. Note the cutouts on the surface: the left side has a U-shaped cutout, and the same cutout should be present on all L293D chips (including yours). That side will be oriented to the top of pinout diagrams in this document – make sure you don’t get it flipped!
(Source: Adafruit)
To start, let’s discuss how H-bridge circuits work. In short, by opening and closing certain switches in a simple, H-shaped circuit, H-bridges can change the direction of current flow across a device (a motor, in our case). This is useful because the direction in which DC motors spin is dependent on the direction of current flowing through them.
In the diagram below, a current flows from the top (labeled VCC) to the bottom (the 3 lines, representing ground). Closing switch 1 (S1) and switch 4 (S4) would complete the circuit across the motor in a positive direction, making it spin forwards. Alternatively, if S1 and S4 were opened and S2 and S3 were closed instead, the circuit would run across the DC motor in a negative direction, making it spin backwards. If all of the switches are open or closed at the same time, no current will be sent across the motor, and it will not spin in either direction.
The H-bridge circuitry is all well and good, but it only covers half of the story – how do we control the motor’s speed? We could make them spin at full speed all the time, but that would lead to jerky movements that may not be desirable, especially if motor movement needs to be precise. In order to fine-tune motor speed control, L293D chips use PWM, or pulse-width modulation.
Pulse-width modulation is a method by which we can reduce the average voltage that’s sent across a circuit. It does this by sending pulses of power (instead of a sustained flow) through the circuit at repeated intervals, often tens or hundreds of times per second. Because the power is being turned on and off so quickly, it can be hard to discern that it’s flickering at all and will likely appear as though the motor is simply running slower at a lessened voltage – the desired effect.
Each on/off pulse is called a duty cycle, and we can change the ratio between how long the power is on vs. how long it’s off within each duty cycle. As mentioned above, duty cycles are run very quickly, often too fast to see – this speed is called the frequency and is measured in Hz. Let’s say, for example, that a circuit normally runs at 5V. If power is only sent 75% of the time within each duty cycle (and is off the other 25% of the time), then a device within the circuit will act as though it’s only receiving 75% of the 5V, or ~3.75V. It’s possible to change the percent values of the duty cycles dynamically, which will be explored in more detail in the links at the end of this document.
Duty cycle voltage vs. time graphs. Note how the average output voltage increases as duty cycle percentage increases.
(Source: Circuit Digest)
Remember: for the motors to spin, they must be given both a direction (H-bridge) and power (PWM) using the various outputs on the L293D. Having only one or the other will not work, as the circuit won’t be complete. The process for assigning direction and power will be covered in the next section and in the L293 Driver program.