Controlling a Motor with the L293D Chip
Welcome to the L293D Lab
The L293D is a classic motor driver chip that contains two full H-bridges in one package. It is cheap, easy to wire, and perfect for learning motor control. Let's build something amazing!
What Is the L293D?
The L293D is a dual H-bridge motor driver chip. "Dual" means it can control two DC motors independently — or one stepper motor. A stepper motor is a special motor that moves in tiny, exact steps.
The "D" at the end of the name means it has built-in flyback diodes. These protect the chip from voltage spikes that motors create when they switch off.
Key specs:
- Motor voltage: 4.5 V to 36 V (the Vcc2 pin)
- Logic voltage: 5 V (the Vcc1 pin)
- Max current per channel: 600 mA continuous, 1.2 A peak
- Cost: about $0.50–$1.00 per chip
The L293D Pinout
The L293D has 16 pins arranged in a Dual In-line Package (DIP) — two rows of pins, like legs on a bug. Here is what each important pin does:
| Pin | Name | Function |
|---|---|---|
| 1 | Enable 1,2 | Set HIGH to allow Motor A to run |
| 2 | Input 1 | Controls Motor A direction |
| 3 | Output 1 | Motor A terminal 1 |
| 6 | Output 2 | Motor A terminal 2 |
| 7 | Input 2 | Controls Motor A direction |
| 8 | Vcc2 | Motor power supply (4.5–36 V) |
| 9 | Enable 3,4 | Set HIGH to allow Motor B to run |
| 10 | Input 3 | Controls Motor B direction |
| 11 | Output 3 | Motor B terminal 1 |
| 14 | Output 4 | Motor B terminal 2 |
| 15 | Input 4 | Controls Motor B direction |
| 16 | Vcc1 | Logic power supply (5 V) |
| 4, 5, 12, 13 | GND | Ground (these pins also act as heat sinks) |
Key Idea
The L293D needs two separate power supplies: Vcc1 (5 V) powers the logic inside the chip, and Vcc2 powers the motors. This separation protects your Pico from noisy motor currents.
Wiring the L293D to the Pico
Follow these steps to connect one motor to the L293D and the Pico:
- Place the L293D chip in the middle of your breadboard so the two rows of pins straddle the center gap.
- Connect Vcc1 (pin 16) to the Pico's VBUS pin (pin 40) — this gives the chip 5 V logic power.
- Connect Vcc2 (pin 8) to your battery pack's positive wire — this powers the motors.
- Connect all four GND pins (4, 5, 12, 13) to the ground rail on your breadboard.
- Connect the battery pack's negative wire to the same ground rail.
- Connect the Pico's GND to the ground rail.
- Connect Enable 1,2 (pin 1) to Pico GP20.
- Connect Input 1 (pin 2) to Pico GP18.
- Connect Input 2 (pin 7) to Pico GP19.
- Connect one motor wire to Output 1 (pin 3).
- Connect the other motor wire to Output 2 (pin 6).
Watch Out!
The Pico's GPIO pins run at 3.3 V, but Vcc1 needs 5 V. Connect Vcc1 to the Pico's VBUS pin (5 V from USB), not the 3V3 pin. The direction inputs (IN1, IN2) still work fine at 3.3 V.
Sample Program: Forward, Backward, Stop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
What Each Line Does
| Line | Purpose |
|---|---|
enable.value(1) |
Turns on the H-bridge so current can flow to the motor |
motor_in1.value(1), motor_in2.value(0) |
Forward: current flows through the motor in one direction |
motor_in1.value(0), motor_in2.value(1) |
Backward: current flows through the motor the other direction |
motor_in1.value(0), motor_in2.value(0) |
Stop: both inputs LOW cuts power to the motor |
time.sleep(2) |
Waits 2 seconds before running the next line |
Controlling Speed with PWM
Connect the Enable pin to a PWM-capable GPIO pin to control motor speed. Pulse-Width Modulation (PWM) turns the pin on and off many times per second. The longer it stays on, the faster the motor spins.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
What Each Line Does
| Line | Purpose |
|---|---|
PWM(Pin(20)) |
Creates a PWM signal on the enable pin |
enable_pwm.freq(1000) |
Sets the PWM to switch 1000 times per second |
duty_u16(32768) |
Sets speed to 50% (32768 out of 65535 max) |
range(32768, 65536, 655) |
Counts from 50% to 100% in small steps |
duty_u16(0) |
Sets speed to 0% — motor stops |
Monty's Tip
The L293D loses about 1.5 V across its output transistors. If you use a 5 V battery, your motor only gets about 3.5 V. For stronger motors, use a higher battery voltage or switch to the DRV8833, which has much lower losses.
References
- L293D Datasheet (Texas Instruments)
- Last Minute Engineer — L293D DC Motor Tutorial
- MicroPython PWM Documentation
Great Work!
You can now drive a motor forward and backward with speed control — those are the core skills for building any wheeled robot. Next, try connecting a second motor to the B channel and making a two-motor robot drive straight!