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 Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Motor driver, L293D
- 1 × DPDT relay
- 1 × DC motor
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 5; Motor driver, L293D pin EN12
- Arduino Uno R3 pin 4; Motor driver, L293D pin IN2
- Arduino Uno R3 pin 3; Motor driver, L293D pin IN1
- Arduino Uno R3 pin 5V; Motor driver, L293D pin VS; Motor driver, L293D pin VSS; DPDT relay pin NO1; DPDT relay pin NO2
- Ground: Arduino Uno R3 pin GND; Motor driver, L293D pin GND1; DPDT relay pin COIL2; DPDT relay pin NC1; DPDT relay pin NC2; DC motor pin 2
- Motor driver, L293D pin OUT1; DPDT relay pin COIL1
- DPDT relay pin COM1; DPDT relay pin COM2; DC motor pin 1
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.