Controlling 16 channel relay module using Arduino
Controlling 16 channel relay module using Arduino
In this page you will get the code to control 16 channel relay module using Arduino.
The two capacitors are C1 100μF 25V and C2 470μF 16V.

This module has problem with voltage to relay. See my video Showing you how to solve the problem.
How to use this relay with 4 wires
Chapters of this video
- 00:00 Introduction
- 01:22 Hardware explained
- 05:40 load calculation
- 06:47 Design flaw in the module (must see)
- 08:46 Code is explained
- 12:54 Wiring Explained (watch this if you don't know how to connect your AC load https://youtu.be/58XWVDnB7Ss)
- 14:20 Demonstration
Where I can buy this 16 channel relay?
-Search eBay (no affiliation) Here-Search Amazon (no affiliation) Here
-Search AliExpress (no affiliation) Here
-Search Banggood (no affiliation) Here
Components used in this module
- DIP 4pin Photocoupler
- ULN2803A Darlington Transistor Arrays
- LM2596 Step-Down Voltage Regulator
- 16 Channel Relay Module Schematic Diagram
- Robojax Crash Course on Arduino: Learn Arduino in 30 Minutes (code and video)
- Learn Arduino step by step from beginner to Advance (Coruse)
- Get Early Access to my videos via Patreon
Code to control 16 Channel Relay module using Arduino
/*
* Arduino code to control 16 channel relay with Arduino UNO
*
* Written by Ahmad Shamshiri for Robojax.com on Sunday Oct 08, 2018
* at 10:35 in Ajax, Ontario, Canada
* Watch video instruction for this code: https://youtu.be/Q9aBI4ELKC4
*
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const int controlPin[16] = {2,3,4,5,6,7,8,9,10,11,12,A0,A1,A2,A3,A4}; // define pins
const int triggerType = LOW;// your relay type
int loopDelay = 1000;// delay in loop
int tmpStat =1;
void setup() {
for(int i=0; i<16; i++)
{
pinMode(controlPin[i], OUTPUT);// set pin as output
if(triggerType ==LOW){
digitalWrite(controlPin[i], HIGH); // set initial state OFF for low trigger relay
}else{
digitalWrite(controlPin[i], LOW); // set initial state OFF for high trigger relay
}
}
Serial.begin(9600);// initialize serial monitor with 9600 baud
}
void loop() {
for(int i=0; i<16; i++)
{
channelControl(i,tmpStat,200);// turn each relay ON for 200ms
}
if(tmpStat)
{
tmpStat=0;
}else{
tmpStat=1;
}
Serial.println("===============");
//channelControl(6,1, 2000); // turn relay 7 ON for 2 seconds
//channelControl(6,0, 5000); // turn relay 7 OFF for 5 seconds
//channelControl(9,1, 3000); // turn relay 10 OFF for ever
delay(loopDelay);// wait for loopDelay ms
}
/*
* funciton: channelControl
* turns ON or OFF specific relay channel
* @param relayChannel is integer value channel from 0 to 15
* @param action is 1 for ON or 0 for OFF
* @param t is time in melisecond
*/
void channelControl(int relayChannel, int action, int t)
{
int state =LOW;
String statTXT =" ON";
if(triggerType == LOW)
{
if (action ==0)// if OFF requested
{
state = HIGH;
statTXT = " OFF";
}
digitalWrite(controlPin[relayChannel], state);
if(t >0 )
{
delay(t);
}
Serial.print ("Channel: ");
Serial.print(relayChannel);
Serial.print(statTXT);
Serial.print(" - ");
Serial.println(t);
}else{
if (action ==1)// if ON requested
{
state = HIGH;
}else{
statTXT = " OFF";
}
digitalWrite(controlPin[relayChannel], state);
if(t >0 )
{
delay(t);
}
Serial.print ("Channel: ");
Serial.print(relayChannel);
Serial.print(statTXT);
Serial.print(" - ");
Serial.println(t);
}
}
Control more than 1 relay at a time in the 16 Channel relay module
/*
* Arduino code to control 16 channel relay with Arduino UNO
* Control more than 1 relay
* Written by Ahmad Shamshiri for Robojax.com on Sunday Oct 08, 2018
* at 10:35 in Ajax, Ontario, Canada
* Watch video instruction for this code: https://youtu.be/Q9aBI4ELKC4
*
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const int controlPin[16] = {2,3,4,5,6,7,8,9,10,11,12,A0,A1,A2,A3,A4}; // define pins
const int triggerType = LOW;// your relay type
int loopDelay = 1000;// delay in loop
int tmpStat =1;
void setup() {
for(int i=0; i<16; i++)
{
pinMode(controlPin[i], OUTPUT);// set pin as output
if(triggerType ==LOW){
digitalWrite(controlPin[i], HIGH); // set initial state OFF for low trigger relay
}else{
digitalWrite(controlPin[i], LOW); // set initial state OFF for high trigger relay
}
}
Serial.begin(9600);// initialize serial monitor with 9600 baud
}
void loop() {
digitalWrite(controlPin[5], LOW); // relay 6 ON
digitalWrite(controlPin[2], LOW); // relay 3 ON
digitalWrite(controlPin[12], LOW); // relay 13 ON
digitalWrite(controlPin[5], HIGH); // relay 6 OFF
}