DC motor speed and direction control

Use the L293D driver chip to set a DC motor's speed with a knob and flip its direction with a button.
  • Level:Intermediate
  • Duration: 1 h
  • Ages 12+
  • Platform: Arduino
Community project EEMENTOR team
DC motor speed and direction control – EEMENTOR STEM™ project

Motors need far more current than an Arduino pin can give, so they always go through a driver. In this project the L293D chip powers the small DC motor from the kit: turn the potentiometer to change the speed, and press the button to reverse the direction.

What you need

  • UNO R3 board and USB cable
  • L293D motor driver chip
  • 3–6 V DC motor
  • 10 kΩ potentiometer
  • Push button
  • Breadboard and jumper wires (all from the UNO R3 Starter Kit)

Placing the chip

Place the L293D across the middle gap of the breadboard, with the notch facing left. Pin 1 is then the bottom-left pin, and the pins count anticlockwise to pin 16 at the top-left. Never connect a motor straight to an Arduino pin: it needs more current than the pin can safely give.

Wiring

Breadboard wiring diagram: DC motor speed and direction control
Breadboard view. Check each connection against the wiring table below.
L293D pin Connect to
1 (EN1) UNO pin 9 (speed)
2 (IN1) UNO pin 8
3 (OUT1) Motor terminal 1
4 and 5 (GND) GND
6 (OUT2) Motor terminal 2
7 (IN2) UNO pin 7
8 (VCC2, motor power) 5V
12 and 13 (GND) GND (top rail)
16 (VCC1, logic power) 5V (top rail)
9, 10, 11, 14, 15 Not used
Other parts Connect to
Potentiometer outer pins 5V and GND
Potentiometer middle pin UNO A0
Button, one side UNO pin 2
Button, other side GND

Join the breadboard's top and bottom rails with two short wires, + to + and − to −, so both sides of the chip get 5V and GND.

Build it step by step

  1. Place the L293D across the centre gap, notch to the left.
  2. Connect 5V and GND to the bottom rails, then bridge them to the top rails.
  3. Wire the chip's power and ground pins, then pins 1, 2 and 7 to the UNO.
  4. Connect the motor to pins 3 and 6.
  5. Add the potentiometer (A0) and the button (pin 2).
  6. Upload the code, turn the knob to speed the motor up, and press the button to reverse it.

The code

const int EN_PIN = 9;     // L293D pin 1: speed (PWM)
const int IN1_PIN = 8;    // L293D pin 2
const int IN2_PIN = 7;    // L293D pin 7
const int POT_PIN = A0;
const int BUTTON_PIN = 2;

bool forward = true;
bool lastButton = HIGH;

void setup() {
  pinMode(EN_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP); // pressed = LOW
}

void loop() {
  // Reverse on each button press (stop briefly first to protect the motor)
  bool button = digitalRead(BUTTON_PIN);
  if (lastButton == HIGH && button == LOW) {
    analogWrite(EN_PIN, 0);
    delay(300);
    forward = !forward;
  }
  lastButton = button;

  // Direction: one input HIGH and the other LOW
  digitalWrite(IN1_PIN, forward ? HIGH : LOW);
  digitalWrite(IN2_PIN, forward ? LOW : HIGH);

  // Speed from the knob, 0 to 255
  int speed = map(analogRead(POT_PIN), 0, 1023, 0, 255);
  analogWrite(EN_PIN, speed);
  delay(20);
}

How it works

The L293D contains an H-bridge: four electronic switches arranged so that current can flow through the motor in either direction. IN1 and IN2 choose the direction (HIGH and LOW, or LOW and HIGH). EN1 switches the output on and off, and because it's connected to a PWM pin, analogWrite() switches it very fast, so the motor gets an average power that sets its speed. The chip also contains protection diodes for the voltage spikes motors create.

Make it your own

  • Print the speed to the Serial Monitor as a percentage.
  • Add a slow start: increase the speed step by step instead of jumping to the knob value.
  • Use the second half of the chip (pins 9 to 15) to drive another motor, as in a two-wheeled robot.

Troubleshooting

  • The motor hums but doesn't turn at low settings: small motors need a minimum power to start. Turn the knob further.
  • The UNO resets when the motor starts: power pin 8 (VCC2) from a separate battery pack for the motor, with its GND joined to the UNO's GND.
  • Nothing moves: check that the notch faces left, and that pins 8 and 16 get 5V.

Learn the basics first

New to these parts? These lessons explain them step by step: Mix colours with the RGB LED module (PWM) and Make a tilt alarm with a buzzer (pull-up inputs).

0 comments

Leave a comment

Please note, comments need to be approved before they are published.

Build it yourself

Get the parts used in this project, tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the parts

More projects

  • Pulse visualiser

    • Level:Intermediate
    • Duration: 45 min
    • Platform: Arduino

    by EEMENTOR team

  • Flame alarm

    • Level:Beginner
    • Duration: 40 min
    • Platform: Arduino

    by EEMENTOR team

Community projects

Built something awesome? Share it.

Send us your project and we'll feature it in the hub, credited to you, so other makers can learn from it.

  1. Sign in and tell us about your build
  2. Our team reviews your submission
  3. We publish it in the hub, credited to you