Built-in project · Arduino Uno R3

Relay module on pin 8, on an Arduino Uno

A relay module on pin 8 clicks on and off every two seconds; its contact switches an LED, and the LED on pin 13 stays lit to show the board is running. 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.

Beginner About 15 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

  • How a relay works as an isolated switch
  • COM and NO contacts
  • Controlling a module with one pin
  • Switching a separate circuit
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

The relay module has a switch inside that is moved by a coil. The contact has no electrical link to the Uno, so it could switch another supply. Here the signal pin IN goes to pin 8, and COM connects to 5V. When the relay is on, the NO contact joins COM to a 220 Ω resistor and the green LED.

The sketch turns pin 8 on for two seconds and off for two seconds, printing "relay on" and "relay off". You hear a click each time. The LED on pin 13 is lit at the start and left on, to show the board is running.

Parts list

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

Kits that can build it

These 3 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
LED, green pin CArduino Uno R3 pin GND
Relay module pin GNDArduino Uno R3 pin GND
Relay module pin INArduino Uno R3 pin 8
Relay module pin VCCArduino Uno R3 pin 5V
Relay module pin COMArduino Uno R3 pin 5V
Relay module pin NOResistor, 220 Ω pin 1
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 module on pin 8: on for two seconds, off for two.
//
//   module S -> pin 8   + -> 5V   - -> GND
//   relay COM -> 5V; NO -> 220 ohm -> LED -> GND
//
// The relay's contact is a switch with no electrical link to the board, so it
// can switch something on another supply. Here it switches an LED.

const int RELAY = 8;

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

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

Code walkthrough

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

setup sketch.ino

Sets pin 13 as an output and writes it HIGH, so that LED stays lit. Then it makes RELAY (pin 8) an output and starts serial at 9600 baud.

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

loop sketch.ino

Sets RELAY HIGH and prints "relay on", waits 2000 ms, then sets it LOW and prints "relay off" and waits another 2000 ms. The relay's contact follows the pin, so the LED turns on and off with it.

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

Troubleshooting

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

  • The LED never lights This project
    Wire the LED to the NO contact. COM goes to 5V, and the LED's cathode (C) goes to ground.
  • The LED is on when the relay is off This project
    NO means normally open. Make sure it, and not NC, is the contact in use.
  • 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 LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • The relay does not click, or the board resets when it switches. Relay module
    A relay coil needs more current than a pin can give. Use a module with a transistor driver, and power VCC from 5 V. Share ground between the module and the Arduino.
  • The relay works backwards: on when you expect off. Relay module
    Many modules are active LOW: the input must go LOW to turn the relay on. Set the pin HIGH at startup so the load does not switch on during boot.

Change it and keep it

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