16-channel relay board chaser on an Arduino Uno
Sixteen relays on one board from sixteen Uno pins: they click on one after another and off again, each lamp on the board following its relay. The whole build, an Arduino Uno and 2 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $43. On Mokxi they're free.
What you'll learn
- Active-low relay inputs
- Driving many outputs from an array of pins
- Why relay coils need their own supply
- Setting the safe state before enabling an output
- Using analog pins as digital outputs
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A 16-channel relay board has one input per relay. The Uno drives all sixteen: IN1 to IN12 on pins 2 to 13, and IN13 to IN16 on A0 to A3. The relay coils run from a separate 12 V supply, and the grounds are joined.
The inputs are active LOW. A relay pulls in while its pin is LOW and drops out when the pin is HIGH.
The sketch clicks the relays on one after another with a 120 ms gap, then switches them off one after another in the same order. Then it repeats.
Parts list
3 parts, plus the jumper wires. They retail for about $43; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × 16-channel relay board
- 1 × DC voltage source
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 pin | Connects to |
|---|---|
| 16-channel relay board pin GND | Arduino Uno R3 pin GND |
| 16-channel relay board pin IN12 | Arduino Uno R3 pin 13 |
| 16-channel relay board pin IN11 | Arduino Uno R3 pin 12 |
| 16-channel relay board pin IN10 | Arduino Uno R3 pin 11 |
| 16-channel relay board pin IN9 | Arduino Uno R3 pin 10 |
| 16-channel relay board pin IN8 | Arduino Uno R3 pin 9 |
| 16-channel relay board pin IN7 | Arduino Uno R3 pin 8 |
| 16-channel relay board pin IN6 | Arduino Uno R3 pin 7 |
| 16-channel relay board pin IN5 | Arduino Uno R3 pin 6 |
| 16-channel relay board pin IN4 | Arduino Uno R3 pin 5 |
| 16-channel relay board pin IN3 | Arduino Uno R3 pin 4 |
| 16-channel relay board pin IN2 | Arduino Uno R3 pin 3 |
| 16-channel relay board pin IN1 | Arduino Uno R3 pin 2 |
| DC voltage source pin V- | Arduino Uno R3 pin GND |
| 16-channel relay board pin IN13 | Arduino Uno R3 pin A0 |
| 16-channel relay board pin IN14 | Arduino Uno R3 pin A1 |
| 16-channel relay board pin IN15 | Arduino Uno R3 pin A2 |
| 16-channel relay board pin IN16 | Arduino Uno R3 pin A3 |
| DC voltage source pin V+ | 16-channel relay board pin VCC |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A 16-channel relay board, chased from sixteen Uno pins.
//
// IN1..IN12 -> pins 2 to 13 IN13..IN16 -> A0 to A3
// VCC -> the 12 V supply (the board's relays have 12 V coils), GND -> GND
//
// The board's inputs are active LOW: a relay pulls in while its pin is LOW.
const uint8_t IN[16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3};
void setup() {
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(IN[i], HIGH); // off before the pin becomes an output
pinMode(IN[i], OUTPUT);
}
}
void loop() {
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(IN[i], LOW);
delay(120);
}
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(IN[i], HIGH);
delay(120);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
For each of the 16 pins in IN, it writes HIGH first and only then makes the pin an output. HIGH means off for this board, so no relay clicks on at start-up.
void setup() {
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(IN[i], HIGH); // off before the pin becomes an output
pinMode(IN[i], OUTPUT);
}
}loop sketch.ino
The first loop sets each pin LOW in turn, with 120 ms between, so the relays pull in one by one. The second loop sets each pin HIGH in turn, so they release one by one in the same order.
void loop() {
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(IN[i], LOW);
delay(120);
}
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(IN[i], HIGH);
delay(120);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Relays click on when you power up, or the pattern is backward This project
The board is active LOW. LOW turns a relay on and HIGH turns it off. - No relays click This project
Check that the relay board VCC is on the 12 V supply and its GND is joined to the Uno's GND. - Relays do not switch or the controller resets. 16-channel relay board
The coils need their own 12 V supply with enough current for many relays at once. Join the grounds of the controller and the board, and do not power coils from a microcontroller pin. - The logic is backward or relays click at start. 16-channel relay board
Check whether your board is active low or active high, and set pins to the off state before making them outputs. Switch relays on one at a time to limit the surge. - The circuit sees no voltage or the wrong amount. DC voltage source
Make sure V+ and V- go to the right nodes. The source floats, so tie one side to your circuit ground. Check the set voltage matches what your parts allow. - A part gets too much voltage. DC voltage source
Check the rating of every part before raising the supply. Use series resistors with LEDs, and do not connect the source straight across a short. - 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.