Skip to content

Controlling a Motor with the L293D Chip

Welcome to the L293D Lab

Monty waving welcome 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

Monty thinking 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:

  1. Place the L293D chip in the middle of your breadboard so the two rows of pins straddle the center gap.
  2. Connect Vcc1 (pin 16) to the Pico's VBUS pin (pin 40) — this gives the chip 5 V logic power.
  3. Connect Vcc2 (pin 8) to your battery pack's positive wire — this powers the motors.
  4. Connect all four GND pins (4, 5, 12, 13) to the ground rail on your breadboard.
  5. Connect the battery pack's negative wire to the same ground rail.
  6. Connect the Pico's GND to the ground rail.
  7. Connect Enable 1,2 (pin 1) to Pico GP20.
  8. Connect Input 1 (pin 2) to Pico GP18.
  9. Connect Input 2 (pin 7) to Pico GP19.
  10. Connect one motor wire to Output 1 (pin 3).
  11. Connect the other motor wire to Output 2 (pin 6).

Watch Out!

Monty warning 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
from machine import Pin, PWM
import time

# Enable pin — set HIGH to allow the motor to run
enable = Pin(20, Pin.OUT)
enable.value(1)        # turn on the motor driver

# Direction pins for Motor A
motor_in1 = Pin(18, Pin.OUT)  # Input 1 controls direction
motor_in2 = Pin(19, Pin.OUT)  # Input 2 controls direction

def motor_forward():
    """Spin the motor forward."""
    motor_in1.value(1)  # IN1 HIGH and IN2 LOW = forward direction
    motor_in2.value(0)

def motor_backward():
    """Spin the motor backward."""
    motor_in1.value(0)  # IN1 LOW and IN2 HIGH = backward direction
    motor_in2.value(1)

def motor_stop():
    """Stop the motor."""
    motor_in1.value(0)  # Both LOW cuts power to the motor
    motor_in2.value(0)

# Run a sequence: forward for 2 seconds, then stop, then backward
motor_forward()
time.sleep(2)   # wait 2 seconds

motor_stop()
time.sleep(0.5) # brief pause

motor_backward()
time.sleep(2)   # wait 2 seconds

motor_stop()

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
from machine import Pin, PWM
import time

# Use PWM on the enable pin so we can control speed
enable_pwm = PWM(Pin(20))
enable_pwm.freq(1000)  # switch 1000 times per second (1 kHz)

motor_in1 = Pin(18, Pin.OUT)  # direction pin 1
motor_in2 = Pin(19, Pin.OUT)  # direction pin 2

# Go forward at 50% speed
motor_in1.value(1)  # set direction to forward
motor_in2.value(0)
enable_pwm.duty_u16(32768)  # 32768 is exactly 50% of 65535 (max speed)
time.sleep(2)

# Ramp up from 50% to 100% speed over about 1 second
for motor_speed in range(32768, 65536, 655):
    enable_pwm.duty_u16(motor_speed)  # increase speed one step at a time
    time.sleep(0.01)                  # wait 10 ms between each step

# Stop the motor
enable_pwm.duty_u16(0)  # duty cycle 0 = motor off

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

Monty giving a 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

  1. L293D Datasheet (Texas Instruments)
  2. Last Minute Engineer — L293D DC Motor Tutorial
  3. MicroPython PWM Documentation

Great Work!

Monty celebrating 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!