Built-in project · Arduino Uno R3

Relay switches a motor on an Arduino Uno

Pin 7 drives a transistor that pulls in a 5 V relay, with a diode across its coil, and the relay’s contacts run a DC motor. The whole build, an Arduino Uno and 9 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 $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Driving a relay with a transistor
  • A flyback diode across the coil
  • Why contacts are electrically separate from the coil
  • Normally open and normally closed contacts
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 11

How it works

Pin 7 on the Uno cannot supply a relay coil, which draws about 40 mA at 5 V. So the pin drives a transistor's base through a 1k resistor, and the transistor carries the coil current to ground.

A diode across the coil gives its collapsing field a safe path when the transistor lets go. The relay's contacts are a separate circuit: one pole switches the motor, and the second pole lights a green or red LED, showing which way the relay sits.

The sketch turns pin 7 on for two seconds and off for two seconds.

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
Transistor (NPN or PNP) pin EArduino Uno R3 pin GND
DPDT relay pin NC1Arduino Uno R3 pin GND
DC motor pin 2Arduino Uno R3 pin GND
LED, green pin CArduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
Resistor, 1k Ω pin 1Arduino Uno R3 pin 7
Diode pin CArduino Uno R3 pin 5V
DPDT relay pin COIL1Arduino Uno R3 pin 5V
DPDT relay pin NO1Arduino Uno R3 pin 5V
DPDT relay pin COM2Arduino Uno R3 pin 5V
Transistor (NPN or PNP) pin BResistor, 1k Ω pin 2
Diode pin ATransistor (NPN or PNP) pin C
DPDT relay pin COIL2Transistor (NPN or PNP) pin C
DC motor pin 1DPDT relay pin COM1
Resistor, 220 Ω (1) pin 1DPDT relay pin NO2
Resistor, 220 Ω (2) pin 1DPDT relay pin NC2
LED, green pin AResistor, 220 Ω (1) pin 2
LED, red pin AResistor, 220 Ω (2) pin 2

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
// Relay: a pin switches a transistor, the transistor switches the relay coil,
// and the relay's contacts switch a motor.
//
//   pin 7      -> 1k resistor -> the transistor's base (PN2222 / 2N2222)
//   emitter    -> GND
//   relay coil -> between 5V and the collector
//   1N4007     -> across the coil, its stripe to 5V
//   relay COM  -> the motor, the motor's other lead to GND
//   relay NO   -> 5V, NC -> GND (released, the motor's lead is grounded)
//   2nd pole   COM -> 5V, NO -> green LED, NC -> red LED (each via 220 ohm)
//
// A relay coil wants far more current than a pin should give (this one draws
// about 40 mA at 5 V), so the pin only drives the transistor's base and the
// transistor carries the coil. When the transistor lets go, the coil's
// collapsing field kicks back, and the diode gives that kick a safe path.
//
// The contacts are a separate circuit. Nothing from the motor ever reaches
// the Uno's pin; it only hears the relay click.

const int RELAY = 7;

void setup() {
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: relay switches a motor");
}

void loop() {
  digitalWrite(RELAY, HIGH);
  Serial.println("relay on: motor running");
  delay(2000);
  digitalWrite(RELAY, LOW);
  Serial.println("relay off: motor stopped");
  delay(2000);
}

Code walkthrough

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

setup sketch.ino

Makes RELAY (pin 7) an output and opens serial at 9600 baud with a title line.

void setup() {
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: relay switches a motor");
}

loop sketch.ino

Sets RELAY HIGH, which turns the transistor on and pulls the relay in, and prints that the motor is running. After 2 seconds it sets the pin LOW, releasing the relay, and prints that it stopped. Another 2 seconds, then it repeats.

void loop() {
  digitalWrite(RELAY, HIGH);
  Serial.println("relay on: motor running");
  delay(2000);
  digitalWrite(RELAY, LOW);
  Serial.println("relay off: motor stopped");
  delay(2000);
}

Troubleshooting

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

  • The relay does not click This project
    Check the transistor's emitter goes to GND, and its pins are the right way around.
  • Sparks or resets when the relay drops out This project
    Fit the diode across the coil with its stripe toward 5 V.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
    Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong.
  • The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
    Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
  • The diode is in backwards, so nothing flows, or a flyback diode conducts all the time. Current flows from the anode to the cathode, the end with the stripe. Diode
    Put the striped end toward the more positive side for a series diode. For flyback across a coil, the stripe goes to the positive supply side of the coil.
  • A diode in series drops about 0.7 V (silicon) and 0.3 V (Schottky), so the output is lower than expected. A 1N4148 is also too small for big currents. Diode
    Count the drop in your voltage budget. Use a 1N4007 or similar for amps, and a Schottky when you need a small drop.

Change it and keep it

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