Built-in project · Arduino Uno R3

Relay board on an Arduino Uno

A four-channel relay board chasing four lamps from pins 2 to 5. Active low, so LOW is on, and the pins are written HIGH before they become outputs. 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.

IntermediateRuns in your browser. Free, and no account needed.

Relay board · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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 board: three ways to switch things a pin cannot.
//
//   pins 2 to 5  IN1 to IN4 of the four-channel board: four lamps
//   pins 6, 7    IN1 and IN2 of the two-channel board: motor A both ways
//   pin 8        the DPDT relay's transistor, through 1 k: motor B both ways
//
// Two circuits run this sketch: "Relay board" wires the lamps, and "Relays
// reverse a motor" wires the two motors. Whatever a circuit leaves unwired
// is simply a pin toggling with nothing on it.
//
// # Active low
//
// The relay boards are active low: an optocoupler's LED sits between VCC and
// each IN pin, so a channel is ON when its pin is LOW. relayOn() hides that,
// and setup() writes every pin HIGH *before* making it an output, because the
// other order leaves each pin low for an instant and every relay clicks on at
// power-up.
//
// # Two relays make an H bridge
//
// Motor A sits between the two channels' COMs. Each NC is on ground and each
// NO on 5 V. Both released: both ends on ground, which is a brake. Channel 1
// in: that end goes to 5 V and the motor turns. Channel 2 in instead: the
// other way. Both in: both ends on 5 V, a brake again, and never a short,
// because each COM is only ever on one contact.
//
// # One coil, two poles
//
// The DPDT relay does the same with one coil, because its two poles move
// together: the motor between the COMs, the supply crossed over between the
// NOs and the NCs. It is a bare relay, so its 40 mA coil hangs off 5 V
// through a 2N2222, with a 1N4007 across it for the kick.
//
// Relays are slow: 10 ms to pull in, 5 ms to let go. Nothing here asks more of
// them than a few clicks a second.

const int LAMPS[] = {2, 3, 4, 5};
const int MOTOR_A[] = {6, 7};
const int DPDT = 8;

void relayOn(int pin, bool on) {
  digitalWrite(pin, on ? LOW : HIGH);
}

void setup() {
  for (int pin = 2; pin <= 7; pin++) {
    digitalWrite(pin, HIGH);   // off, before it is even an output
    pinMode(pin, OUTPUT);
  }
  pinMode(DPDT, OUTPUT);
  digitalWrite(DPDT, LOW);
  Serial.begin(115200);
  Serial.println("Mokxi Uno: relays three ways");
}

void chase() {
  for (int i = 0; i < 4; i++) {
    relayOn(LAMPS[i], true);
    delay(250);
    relayOn(LAMPS[i], false);
  }
}

void loop() {
  Serial.println("motor A forward, motor B forward");
  relayOn(MOTOR_A[0], true);
  digitalWrite(DPDT, LOW);
  chase();

  Serial.println("both motors braked");
  relayOn(MOTOR_A[0], false);
  delay(600);

  Serial.println("motor A reverse, motor B reverse");
  relayOn(MOTOR_A[1], true);
  digitalWrite(DPDT, HIGH);
  chase();

  relayOn(MOTOR_A[1], false);
  delay(600);
}

Parts list

13 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

16 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; Relay board, four channels pin IN4
  • Arduino Uno R3 pin 4; Relay board, four channels pin IN3
  • Arduino Uno R3 pin 3; Relay board, four channels pin IN2
  • Arduino Uno R3 pin 2; Relay board, four channels pin IN1
  • Ground: Arduino Uno R3 pin GND; LED, yellow (1) pin C; LED, yellow (2) pin C; LED, yellow (3) pin C; LED, yellow (4) pin C
  • Ground: Relay board, four channels pin GND
  • Supply: Relay board, four channels pin VCC
  • Relay board, four channels pin NO1; Resistor, 220 Ω (4) pin 1
  • Supply: Relay board, four channels pin COM1; Relay board, four channels pin COM2; Relay board, four channels pin COM3; Relay board, four channels pin COM4
  • Relay board, four channels pin NO2; Resistor, 220 Ω (3) pin 1
  • Relay board, four channels pin NO3; Resistor, 220 Ω (2) pin 1
  • Relay board, four channels pin NO4; Resistor, 220 Ω (1) pin 1
  • Resistor, 220 Ω (1) pin 2; LED, yellow (1) pin A
  • Resistor, 220 Ω (2) pin 2; LED, yellow (2) pin A
  • Resistor, 220 Ω (3) pin 2; LED, yellow (3) pin A
  • Resistor, 220 Ω (4) pin 2; LED, yellow (4) pin A

Change it and keep it

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