Built-in project · Arduino Uno R3

Relay on pin 8 through a transistor, on an Arduino Uno

Pin 8 drives an NPN transistor through 1k, the transistor pulls in a 5 V relay with a diode across its coil, and the relay’s contact lights an LED once a second. The whole build, an Arduino Uno and 6 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.

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

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

The circuit runs here once the page has loaded.

What you'll learn

  • Switching a coil with a transistor
  • Relays as electrically isolated switches
  • Flyback diodes that absorb a voltage spike
  • Common, normally open and normally closed contacts
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

A relay coil needs more current than a pin can give. So pin 8 goes through a 1k resistor to the base of a transistor, and the transistor carries the coil's current to ground. The coil's other end is on 5V.

When pin 8 goes high, the transistor turns on and the coil pulls the relay in with a click. The relay's contact connects 5V to a 220 ohm resistor and a green LED, so the LED lights.

A diode sits across the coil. When the transistor lets go, the coil sends back a voltage spike, and the diode gives it a safe path. The sketch holds each state for one second.

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
LED, green pin CArduino Uno R3 pin GND
Resistor, 1k Ω pin 1Arduino Uno R3 pin 8
Diode pin CArduino Uno R3 pin 5V
DPDT relay pin COIL1Arduino Uno R3 pin 5V
DPDT relay pin COM1Arduino 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
DPDT relay pin NO2DPDT relay pin NO1
Resistor, 220 Ω pin 1DPDT relay pin NO1
DPDT relay pin NC2DPDT relay pin NC1
LED, green pin AResistor, 220 Ω 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
// A relay: pin 8 switches a transistor, the transistor switches the coil.
//
//   pin 8 -> 1k resistor -> the transistor's base; emitter -> GND
//   collector -> one end of the relay coil; the coil's other end -> 5V
//   a diode across the coil, its stripe toward 5V
//   relay COM -> 5V, relay NO -> 220 ohm -> LED -> GND
//
// The coil needs more current than a pin can give, so the pin only switches
// the transistor and the transistor carries the coil's current. When the
// transistor lets go, the coil's magnetic field collapses and kicks back a
// voltage spike; the diode gives it somewhere harmless to go.
//
// The relay clicks in for a second, and the LED on its contact lights, then
// clicks out for a second.

const int RELAY = 8;

void setup() {
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: relay on pin 8");
}

void loop() {
  digitalWrite(RELAY, HIGH);
  Serial.println("relay in: LED on");
  delay(1000);
  digitalWrite(RELAY, LOW);
  Serial.println("relay out: LED off");
  delay(1000);
}

Code walkthrough

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

setup sketch.ino

It sets pin 8 as an output and starts the serial port at 9600. It prints a startup line.

void setup() {
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: relay on pin 8");
}

loop sketch.ino

It sets pin 8 high, prints "relay in: LED on" and waits 1000 ms. Then it sets the pin low, prints "relay out: LED off" and waits another 1000 ms.

void loop() {
  digitalWrite(RELAY, HIGH);
  Serial.println("relay in: LED on");
  delay(1000);
  digitalWrite(RELAY, LOW);
  Serial.println("relay out: LED off");
  delay(1000);
}

Troubleshooting

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

  • The relay never clicks. This project
    Check pin 8 goes through the 1k resistor to the transistor base, the emitter goes to GND, and the collector goes to the coil.
  • The LED does not light when the relay clicks. This project
    The LED must be wired from the relay's normally open contact (NO), not the NC contact.
  • The diode seems to do nothing, or shorts the coil. This project
    Its stripe side goes toward 5V. A reversed diode conducts all the time.
  • 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.