Built-in project · Arduino Uno R3

Relay switched by an L293D (pins 5, 3 and 4), on an Arduino Uno

An L293D drives the coil of a 5 V relay from pins 5, 3 and 4, and the relay’s contacts switch a DC motor on and off every two seconds. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $36. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Driving a relay coil with a driver chip
  • How a relay's contacts work apart from its coil
  • Enable and input pins on an L293D
  • Keeping a switched circuit apart from the Uno
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A relay coil needs more current than an Uno pin can give. So the Uno drives an L293D motor driver, and the L293D drives the relay coil. The relay is a switch worked by a magnet: current in the coil pulls the contact across.

Pin 5 holds the enable pin high, and pins 3 and 4 are the two inputs. When pin 3 is HIGH and pin 4 is LOW, OUT1 goes up near 5 V and current flows through the coil to ground. The relay clicks in and the motor on its contacts spins.

The sketch switches it on for two seconds and off for two seconds, forever.

Parts list

5 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.

Kits that can build it

These 5 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Motor driver, L293D pin EN12Arduino Uno R3 pin 5
Motor driver, L293D pin IN2Arduino Uno R3 pin 4
Motor driver, L293D pin IN1Arduino Uno R3 pin 3
Motor driver, L293D pin VSArduino Uno R3 pin 5V
Motor driver, L293D pin VSSArduino Uno R3 pin 5V
DPDT relay pin NO1Arduino Uno R3 pin 5V
DPDT relay pin NO2Arduino Uno R3 pin 5V
Motor driver, L293D pin GND1Arduino Uno R3 pin GND
DPDT relay pin COIL2Arduino Uno R3 pin GND
DPDT relay pin NC1Arduino Uno R3 pin GND
DPDT relay pin NC2Arduino Uno R3 pin GND
DC motor pin 2Arduino Uno R3 pin GND
DPDT relay pin COIL1Motor driver, L293D pin OUT1
DPDT relay pin COM2DPDT relay pin COM1
DC motor pin 1DPDT relay pin COM1

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A relay switching a motor, its coil driven by an L293D.
//
//   L293D EN1,2 -> Uno pin 5    IN1 -> Uno pin 3    IN2 -> Uno pin 4
//   L293D OUT1 -> one end of the relay coil; the coil's other end -> GND
//   relay COM -> 5V (motor supply)    relay NO -> the motor -> GND
//   L293D VSS and VS -> 5V      L293D GND -> GND
//
// A relay is a switch worked by a magnet. Current through the coil pulls the
// contact over, and the contact can switch a circuit that never touches the
// Uno. The coil needs more current than an Uno pin can give, so the L293D
// drives it, the same chip that drives the motor in the motor lesson.
//
// IN1 HIGH drives OUT1 up to the supply (less the chip's own drop of about a
// volt), current flows through the coil, and the relay clicks in: the motor
// spins. IN1 LOW takes OUT1 back to ground, the coil lets go, and the motor
// stops. IN2 stays LOW; the second half of the chip is free for a second relay.

const int ENABLE = 5;
const int DIRA = 3;
const int DIRB = 4;

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  digitalWrite(ENABLE, HIGH);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: relay on an L293D");
}

void loop() {
  digitalWrite(DIRA, HIGH);
  digitalWrite(DIRB, LOW);
  Serial.println("relay in: motor spins");
  delay(2000);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
  Serial.println("relay out: motor stops");
  delay(2000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Makes pins 5, 3 and 4 outputs, then drives ENABLE HIGH once so the L293D is always on. It opens serial at 9600 baud and prints a title.

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  digitalWrite(ENABLE, HIGH);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: relay on an L293D");
}

loop sketch.ino

Sets DIRA HIGH and DIRB LOW to power the coil, prints that the relay is in, and waits 2 seconds. Then it sets both inputs LOW, so the coil lets go and the motor stops, and waits another 2 seconds.

void loop() {
  digitalWrite(DIRA, HIGH);
  digitalWrite(DIRB, LOW);
  Serial.println("relay in: motor spins");
  delay(2000);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
  Serial.println("relay out: motor stops");
  delay(2000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The relay never clicks This project
    Check that EN12 is wired to pin 5 and that the coil connects from OUT1 to ground.
  • The motor does not run when the relay clicks This project
    The motor path is separate: relay COM and NO must connect the 5 V supply to the motor.
  • The motor is weak or does not turn, and the chip gets hot. Motor driver
    The L293D gives about 600 mA per channel and drops about 1.4 V per side. Use a motor that fits, and for bigger motors pick another driver.
  • Nothing moves even though the inputs are right. Motor driver
    Power both supply pins: VCC1 (pin 16) at 5 V for logic and VCC2 (pin 8) for the motor. Tie the enable pin high, and join all grounds.
  • The relay coil does not click, or the Arduino resets when it switches. DPDT relay
    A pin cannot drive a coil directly. Use a transistor or MOSFET with a base resistor, and power the coil from its rated voltage (5 V or 12 V).
  • The transistor dies or the circuit glitches at turn off. DPDT relay
    Put a flyback diode across the coil, cathode to the positive side. The coil kicks back a high voltage spike when it is switched off.
  • The motor does not spin, or the pin gets hot or the board resets when it is wired straight to a GPIO pin. DC motor
    A pin gives about 20 mA and a motor needs far more. Drive it through a transistor or motor driver, with its own supply and a shared ground.
  • The transistor or pin is damaged by spikes when the motor stops. DC motor
    Put a flyback diode across the motor, cathode to the positive side. A motor is a coil and kicks back voltage when switched off. Add a 0.1 uF capacitor across the motor terminals to cut noise.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.