Built-in project · Arduino Uno R3

8-channel relay board on an Arduino Uno

An eight-channel relay board on pins 2 to 9 clicks through its channels one at a time. Active low, and every pin is written HIGH first. The whole build, an Arduino Uno and 1 more part, 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 $35. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Active low relay inputs
  • Looping over pins with a for loop
  • Setting a pin HIGH before making it an output
  • Current limits of a USB powered board
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

An eight channel relay board lets the Uno switch eight separate loads. Its inputs IN1 to IN8 connect to pins 2 to 9, and it takes 5 V and ground. The sketch clicks each channel on for 300 ms, one after another.

This board is active low: a channel pulls in while its pin is LOW. In setup, each pin is set HIGH before it becomes an output, so nothing clicks on at power up.

Eight relays draw around 70 mA each, which is more than a USB powered Uno likes. Running one at a time is fine.

Parts list

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

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
8-channel relay board pin GNDArduino Uno R3 pin GND
8-channel relay board pin IN8Arduino Uno R3 pin 9
8-channel relay board pin IN7Arduino Uno R3 pin 8
8-channel relay board pin IN6Arduino Uno R3 pin 7
8-channel relay board pin IN5Arduino Uno R3 pin 6
8-channel relay board pin IN4Arduino Uno R3 pin 5
8-channel relay board pin IN3Arduino Uno R3 pin 4
8-channel relay board pin IN2Arduino Uno R3 pin 3
8-channel relay board pin IN1Arduino Uno R3 pin 2
8-channel relay board pin VCCArduino Uno R3 pin 5V

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
// An eight-channel relay board clicking through its channels, from an Uno.
//
//   relay board IN1 to IN8 -> pins 2 to 9
//   relay board VCC -> 5V    GND -> GND
//
// The board is active low: a channel pulls in while its pin is LOW. Each pin
// is written HIGH before it is made an output, so nothing clicks at power-up.
// Eight channels at about 70 mA each is more than a USB-powered board likes;
// one at a time is fine.

const int FIRST = 2;
const int CHANNELS = 8;

void setup() {
  for (int i = 0; i < CHANNELS; i++) {
    digitalWrite(FIRST + i, HIGH);
    pinMode(FIRST + i, OUTPUT);
  }
  Serial.begin(9600);
  Serial.println("Mokxi Uno: eight relays, one at a time");
}

void loop() {
  for (int i = 0; i < CHANNELS; i++) {
    digitalWrite(FIRST + i, LOW);
    Serial.print("channel ");
    Serial.println(i + 1);
    delay(300);
    digitalWrite(FIRST + i, HIGH);
  }
}

Code walkthrough

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

setup sketch.ino

Loops through pins 2 to 9. For each it writes HIGH and then sets it as an output, which keeps all relays off from the start. It also starts serial and prints a message.

void setup() {
  for (int i = 0; i < CHANNELS; i++) {
    digitalWrite(FIRST + i, HIGH);
    pinMode(FIRST + i, OUTPUT);
  }
  Serial.begin(9600);
  Serial.println("Mokxi Uno: eight relays, one at a time");
}

loop sketch.ino

Goes through the eight channels. It pulls one pin LOW, prints "channel" and its number, waits 300 ms, then sets it HIGH again before moving on.

void loop() {
  for (int i = 0; i < CHANNELS; i++) {
    digitalWrite(FIRST + i, LOW);
    Serial.print("channel ");
    Serial.println(i + 1);
    delay(300);
    digitalWrite(FIRST + i, HIGH);
  }
}

Troubleshooting

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

  • The relays click all at once at power up This project
    Keep the order in setup: digitalWrite HIGH first, then pinMode.
  • Channels seem backward This project
    IN1 goes to pin 2 and IN8 goes to pin 9. Check the order.
  • The relays do not click or work backwards. 8-channel relay board
    The inputs are active low, so write LOW to turn a relay on. Power JD-VCC from a supply that matches the coil (5, 12 or 24 V) and join grounds.
  • The board resets when a relay switches. 8-channel relay board
    Eight coils draw a lot. Use a separate supply for the relay side and remove the VCC-JD-VCC jumper to isolate it. Add a bulk capacitor.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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