logo
NodeMCU 16 Channel Relay Box
NodeMCU 16 Channel Relay Box

This project with the NodeMCU mini can controll 16 or more outputs to drive relays,
it can be controlled from a Web Browser address bar or from an Android App that I am developing
Called NodeControllerTab or Nodecontroller in conjunction with NodecontrollerEx

You could send the command from a Web Browser address bar,
format: ip-address/COMMAND ,eg: 10.220.1.22/RELAY1ON
would turn Relay 1 ON.

format: ip-address/COMMAND ,eg: 10.220.1.22/RELAY1OFF
would turn Relay 1 OFF.

Or you could use the Apps that I have been developing
NodeControllerTab for Android, this App is designed for Tablets but will work with Phones in landscape mode,
alternatively you could use two Apps NodeController and NodeControllerEx, they each controll 8 channel relay boards

How it works
We send commands to the NodeMCU throw it's wifi conection and then in turn it communicates with the Arduino Mega to turn outputs On or Off,
we use an Arduino Mega to drive the outputs because of the large number of Digital pins this board provides, althow in this project we are only using
16 of the Digital pins, we could configure it to use many more, the communication between the NodeMCU and Arduino Mega is
i2c, the NodeMCU is the Master and the Arduino Mega as the Slave, we could add many more Slaves to the i2c Bus.

PARTS

1 x NodeMcu ESP8266 D1 Mini
ATmega2560 Arduino Mega
5v to 3.3v 4 Channel IIc I2c logic level converter Bi-Directional module
2 x 8 Channel relay module with optocoupler and High Level Trigger
or if you use the Transistor use Low Level Trigger relays
4 x TM1637 7 Segment 4 Bit Displays 0.36" or 0.56"
1 x LED and 470 ohm resistor
1 x small switch
5v power supplys, I use phone chargers they are around 2 Amps,
the 8 channel relay board with all relays On consume around .7 Amps
If you want to add then Transistors
16 x 2N2222 (2N2222A) Transistors
16 x 1k resistors
Power supply to suit the relays

Some other components depending how you would like to connect the wires to the PCB, eg pin headers, terminals.











8 channel relay 

8 channel relay       8 channel relay

8 channel relay       8 channel relay

16_channel_relay_box_front       16_channel_relay_box_inside1

16_channel_relay_box_inside2       16_channel_relay_box_back

NodeControllerTab


NodeControllerTab on Google Play Store

 NodeController on Google Play Store
NodeControllerEX on Google Play Store



Below I have added a link to download a Ziped file with the Arduino Sketch, Schematic, Readme
Download  Sketch and info

 

       

16 Channel relay box
click picture to enlarge

Below are the Sketches for This Project, Master and Slave, you can modify it to suit your own designs, I have not optimized the code, that way you can see how it works.

/*

 MerrittSoft
 NodeMCU and Arduino Mega 16 channel relay box
 NodeMCU Master unit
 07/09/2020
 Language: C++/Arduino
 Thank you to those people that made the Libarays in this project posable.
*/

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Wire.h>

ESP8266WebServer server;

char* ssid = "SsidName";
char* password = "PassWord";

int outputStateRead[16] = {12, 22, 32, 42, 52, 62, 72, 82, 112, 122, 132, 142, 152, 162, 172, 182};

int pinState[16];

int command;

const int wificonnected = D7; // wifi connection led.

void setup() {

 pinMode(wificonnected, OUTPUT);
 digitalWrite(wificonnected, LOW);

 WiFi.begin(ssid, password);
 WiFi.hostname("NodeMCU16Ch");
 WiFi.mode(WIFI_STA);
 Serial.begin(9600);
 while (WiFi.status() != WL_CONNECTED)
 {
   Serial.print(".");
   delay(500);
 }
 digitalWrite(wificonnected, HIGH);
 Serial.println("");
 Serial.print("IP Address: ");
 Serial.println(WiFi.localIP());
 Serial.println();

 server.on("/", []() {
   server.send(200, "text/plain", "NodeMCU-16-Channel");
 });

 // below are the commands that activate the Relays from the mobile device.
 // eg. the /RELAY1ON is the command you add to the end of the ip address to turn Relay 1 ON eg. 10.220.1.20/RELAY1ON ,
 // This can be done in a Web Browser address bar on the same network or using NodeControllerTab app.

 server.on("/RELAY1ON", relay1on);
 server.on("/RELAY1OFF", relay1off);
 server.on("/RELAY2ON", relay2on);
 server.on("/RELAY2OFF", relay2off);
 server.on("/RELAY3ON", relay3on);
 server.on("/RELAY3OFF", relay3off);
 server.on("/RELAY4ON", relay4on);
 server.on("/RELAY4OFF", relay4off);
 server.on("/RELAY5ON", relay5on);
 server.on("/RELAY5OFF", relay5off);
 server.on("/RELAY6ON", relay6on);
 server.on("/RELAY6OFF", relay6off);
 server.on("/RELAY7ON", relay7on);
 server.on("/RELAY7OFF", relay7off);
 server.on("/RELAY8ON", relay8on);
 server.on("/RELAY8OFF", relay8off);

 server.on("/RELAY9ON", relay9on);
 server.on("/RELAY9OFF", relay9off);
 server.on("/RELAY10ON", relay10on);
 server.on("/RELAY10OFF", relay10off);
 server.on("/RELAY11ON", relay11on);
 server.on("/RELAY11OFF", relay11off);
 server.on("/RELAY12ON", relay12on);
 server.on("/RELAY12OFF", relay12off);
 server.on("/RELAY13ON", relay13on);
 server.on("/RELAY13OFF", relay13off);
 server.on("/RELAY14ON", relay14on);
 server.on("/RELAY14OFF", relay14off);
 server.on("/RELAY15ON", relay15on);
 server.on("/RELAY15OFF", relay15off);
 server.on("/RELAY16ON", relay16on);
 server.on("/RELAY16OFF", relay16off);

 server.begin();
 Wire.begin(D1, D2); //join i2c bus with SDA=D1 and SCL=D2 of NodeMCU.

 outputstate(); // Call outputstate void to receive all the states of the Slave pin outputs,
 //  future development and debug.

}

void loop() {

 

 server.handleClient();

}


//************ voids that send the commands to the Slave.
void relay1on()
{
 Serial.print("relay1 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");       // send 204 code as a return acknowledgment to the mobile device
 Wire.beginTransmission(8);  // the address of the Slave.
 Wire.write(11);             // command sent to Slave
 Wire.endTransmission();     // stop transmitting
 Wire.requestFrom(8, 1);     // request a read data from slave.
 while (Wire.available()) {
   command = Wire.read();   // Read response from Slave
   Serial.print(command);
   outputstate(); // void outputstate
 }
 Serial.println();
 return;
}

void relay1off()
{
 Serial.print("relay1 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(10);
 Wire.endTransmission();
 Wire.requestFrom(8, 1);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay2on()
{
 Serial.print("relay2 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(21);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay2off()
{
 Serial.print("relay2 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(20);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay3on()
{
 Serial.print("relay3 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(31);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay3off()
{
 Serial.print("relay3 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(30);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay4on()
{
 Serial.print("relay4 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(41);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay4off()
{
 Serial.print("relay4 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(40);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay5on()
{
 Serial.print("relay5 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(51);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay5off()
{
 Serial.print("relay5 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(50);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay6on()
{
 Serial.print("relay6 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(61);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay6off()
{
 Serial.print("relay6 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(60);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay7on()
{
 Serial.print("relay7 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(71);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay7off()
{
 Serial.print("relay7 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(70);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay8on()
{
 Serial.print("relay8 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(81);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay8off()
{
 Serial.print("relay8 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(80);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

//*********** viods for 9 to 16.
void relay9on()
{
 Serial.print("relay9 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(111);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay9off()
{
 Serial.print("relay9 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(110);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay10on()
{
 Serial.print("relay10 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(121);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay10off()
{
 Serial.print("relay10 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(120);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay11on()
{
 Serial.print("relay11 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(131);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay11off()
{
 Serial.print("relay11 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(130);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay12on()
{
 Serial.print("relay12 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(141);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay12off()
{
 Serial.print("relay12 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(140);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay13on()
{
 Serial.print("relay13 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(151);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay13off()
{
 Serial.print("relay13 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(150);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay14on()
{
 Serial.print("relay14 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(161);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay14off()
{
 Serial.print("relay14 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(160);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay15on()
{
 Serial.print("relay15 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(171);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 // delay(200);
 return;
}

void relay15off()
{
 Serial.print("relay15 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(170);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay16on()
{
 Serial.print("relay16 = 1");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(181);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

void relay16off()
{
 Serial.print("relay16 = 0");
 Serial.println();
 delay(200);
 server.send(204, "");
 Wire.beginTransmission(8);
 Wire.write(180);
 Wire.endTransmission();
 Wire.requestFrom(8, 16);
 while (Wire.available()) {
   command = Wire.read();
   Serial.print(command);
   outputstate();
 }
 Serial.println();
 return;
}

//************ Asking Slave what is all of the output states.
//************ This is for further advancement in the software development, but is working for debug.
void outputstate() {
 Serial.println();
 for (int i = 0; i < 16; i++) {
   Wire.beginTransmission(8);     // the address of the Slave.
   Wire.write(outputStateRead[i]);  // send command to Slave asking for pin states on each ouput.
   Wire.endTransmission();          // stop transmitting
   Wire.requestFrom(8, 1);           // request a read data from slave.
   while (Wire.available()) {
     command = Wire.read();           // read the pin states.
     pinState[i] = command;
     Serial.print(command);       // print pin states

   }
 }
 Serial.println();
 return;
}



//*****************************************************************************************************
//****************************************** Code End *************************************************


/*
 MerrittSoft
 NodeMCU and Arduino Mega 16 channel relay box
 Arduino Mega Salve unit
 07/09/2020
 Language: C++/Arduino
 Thank you to those people that made the Libarays in this project posable.
*/

#include <TM1637Display.h>
#include <Wire.h>
int pinStateoutput1; int pinStateoutput2; int pinStateoutput3; int pinStateoutput4;
int pinStateoutput5; int pinStateoutput6; int pinStateoutput7; int pinStateoutput8;
int pinStateoutput9; int pinStateoutput10; int pinStateoutput11; int pinStateoutput12;
int pinStateoutput13; int pinStateoutput14; int pinStateoutput15; int pinStateoutput16;


int command;

int pinState;

#define CLK1 4
#define DIO1 3

#define CLK2 5
#define DIO2 3

#define CLK3 6
#define DIO3 3

#define CLK4 7
#define DIO4 3

#define TEST_DELAY   2000

TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);
TM1637Display display3(CLK3, DIO3);
TM1637Display display4(CLK4, DIO4);
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };  // all segments clear

const uint8_t SEG_NONE[] = {
 SEG_DP,           // .
};

const int output1 = 22;
const int output2 = 23;
const int output3 = 24;
const int output4 = 25;
const int output5 = 26;
const int output6 = 27;
const int output7 = 28;
const int output8 = 29;
const int output9 = 30;
const int output10 = 31;
const int output11 = 32;
const int output12 = 33;
const int output13 = 34;
const int output14 = 35;
const int output15 = 36;
const int output16 = 37;






void setup() {

 // 4 TM1637 Display 20181029
 display1.setBrightness(1);// set brightness of dispaly 1
 display2.setBrightness(1);// set brightness of dispaly 2
 display3.setBrightness(1);// set brightness of dispaly 3
 display4.setBrightness(1);// set brightness of dispaly 4


 display1.setSegments(data);// display 1 with whatever data[] array has
 display2.setSegments(data);// display 2 with whatever data[] array has
 display3.setSegments(data);// display 3 with whatever data[] array has
 display4.setSegments(data);// display 4 with whatever data[] array has


 display1.setSegments(data);

 pinMode(output1, OUTPUT);
 pinMode(output2, OUTPUT);
 pinMode(output3, OUTPUT);
 pinMode(output4, OUTPUT);
 pinMode(output5, OUTPUT);
 pinMode(output6, OUTPUT);
 pinMode(output7, OUTPUT);
 pinMode(output8, OUTPUT);
 pinMode(output9, OUTPUT);
 pinMode(output10, OUTPUT);
 pinMode(output11, OUTPUT);
 pinMode(output12, OUTPUT);
 pinMode(output13, OUTPUT);
 pinMode(output14, OUTPUT);
 pinMode(output15, OUTPUT);
 pinMode(output16, OUTPUT);

 // digitalWrite(transformer, LOW);

 digitalWrite(output1, LOW);
 digitalWrite(output2, LOW);
 digitalWrite(output3, LOW);
 digitalWrite(output4, LOW);
 digitalWrite(output5, LOW);
 digitalWrite(output6, LOW);
 digitalWrite(output7, LOW);
 digitalWrite(output8, LOW);
 digitalWrite(output9, LOW);
 digitalWrite(output10, LOW);
 digitalWrite(output11, LOW);
 digitalWrite(output12, LOW);
 digitalWrite(output13, LOW);
 digitalWrite(output14, LOW);
 digitalWrite(output15, LOW);
 digitalWrite(output16, LOW);

 Wire.begin(8);                //Slave i2c bus is address 8
 Wire.onReceive(receiveEvent); //register receive event
 Wire.onRequest(requestEvent); //register request event
 Serial.begin(9600);           //start serial for debug
}

void loop() {
 delay(500);

 //*************** Read the pin output state and show the Number on the Display No1.
 if (digitalRead(output1) == 0) {
   display1.setSegments(SEG_NONE, 1, 0);
 } else {
   display1.showNumberDecEx(1, 0b11100000, false, 1, 0);
 }
 if (digitalRead(output2) == 0) {
   display1.setSegments(SEG_NONE, 1, 1);
 } else {
   display1.showNumberDecEx(2, 0b11100000, false, 1, 1);
 }
 if (digitalRead(output3) == 0) {
   display1.setSegments(SEG_NONE, 1, 2);
 } else {
   display1.showNumberDecEx(3, 0b11100000, false, 1, 2);
 }
 if (digitalRead(output4) == 0) {
   display1.setSegments(SEG_NONE, 1, 3);
 } else {
   display1.showNumberDecEx(4, 0b11100000, false, 1, 3);
 }


 //*************** Read the pin output state and show the Number on the Display No2.
 if (digitalRead(output5) == 0) {
   display2.setSegments(SEG_NONE, 1, 0);
 } else {
   display2.showNumberDecEx(5, 0b11100000, false, 1, 0);
 }
 if (digitalRead(output6) == 0) {
   display2.setSegments(SEG_NONE, 1, 1);
 } else {
   display2.showNumberDecEx(6, 0b11100000, false, 1, 1);
 }
 if (digitalRead(output7) == 0) {
   display2.setSegments(SEG_NONE, 1, 2);
 } else {
   display2.showNumberDecEx(7, 0b11100000, false, 1, 2);
 }
 if (digitalRead(output8) == 0) {
   display2.setSegments(SEG_NONE, 1, 3);
 } else {
   display2.showNumberDecEx(8, 0b11100000, false, 1, 3);
 }


 //*************** Read the pin output state and show the Number on the Display No3.
 if (digitalRead(output9) == 0) {
   display3.setSegments(SEG_NONE, 1, 0);
 } else {
   display3.showNumberDecEx(1, 0b11100000, false, 1, 0);
 }
 if (digitalRead(output10) == 0) {
   display3.setSegments(SEG_NONE, 1, 1);
 } else {
   display3.showNumberDecEx(2, 0b11100000, false, 1, 1);
 }
 if (digitalRead(output11) == 0) {
   display3.setSegments(SEG_NONE, 1, 2);
 } else {
   display3.showNumberDecEx(3, 0b11100000, false, 1, 2);
 }
 if (digitalRead(output12) == 0) {
   display3.setSegments(SEG_NONE, 1, 3);
 } else {
   display3.showNumberDecEx(4, 0b11100000, false, 1, 3);
 }

 //*************** Read the pin output state and show the Number on the Display No4.
 if (digitalRead(output13) == 0) {
   display4.setSegments(SEG_NONE, 1, 0);
 } else {
   display4.showNumberDecEx(5, 0b11100000, false, 1, 0);
 }
 if (digitalRead(output14) == 0) {
   display4.setSegments(SEG_NONE, 1, 1);
 } else {
   display4.showNumberDecEx(6, 0b11100000, false, 1, 1);
 }
 if (digitalRead(output15) == 0) {
   display4.setSegments(SEG_NONE, 1, 2);
 } else {
   display4.showNumberDecEx(7, 0b11100000, false, 1, 2);
 }
 if (digitalRead(output16) == 0) {
   display4.setSegments(SEG_NONE, 1, 3);
 } else {
   display4.showNumberDecEx(8, 0b11100000, false, 1, 3);
 }





}
//*********** function that executes whenever data is received from master
void receiveEvent(int howMany) {
 while (0 < Wire.available()) {

   command = Wire.read();      // Read command from master.

   //*********** This next section when asked from Master, sends all the pin output states,
   //*********** This is for further advancement in the software development, But is working for debug.
   if (command == 12) {
     pinState = digitalRead(output1);
   }
   else if (command == 22) {
     pinState = digitalRead(output2);
   }
   else if (command == 32) {
     pinState = digitalRead(output3);
   }
   else if (command == 42) {
     pinState = digitalRead(output4);
   }
   else if (command == 52) {
     pinState = digitalRead(output5);
   }
   else if (command == 62) {
     pinState = digitalRead(output6);
   }
   else if (command == 72) {
     pinState = digitalRead(output7);
   }
   else if (command == 82) {
     pinState = digitalRead(output8);
   }
   else if (command == 112) {
     pinState = digitalRead(output9);
   }
   else if (command == 122) {
     pinState = digitalRead(output10);
   }
   else if (command == 132) {
     pinState = digitalRead(output11);
   }
   else if (command == 142) {
     pinState = digitalRead(output12);
   }
   else if (command == 152) {
     pinState = digitalRead(output13);
   }
   else if (command == 162) {
     pinState = digitalRead(output14);
   }
   else if (command == 172) {
     pinState = digitalRead(output15);
   }
   else if (command == 182) {
     pinState = digitalRead(output16);
   }


   //************** If command from the master is lower then 82 junp to void tens,
   //************** If command from the Master is higher then 82 but less the 182 jump to void hundreds.
   //************** My thought here was to try and seed it up by jumping the the code needed.

   if (command < 82) {
     tens();
   }
   else if (command > 82 && command < 182) {
     hundreds();
   }
 }
}

// ************ function that executes whenever data is requested from master
void requestEvent() {
 Wire.write(pinState); // this will send the Pin States High = 1 and Low = 0 to the Master.
}

//************* Print out the Relay number 1 to 8 and to turn it ON or OFF then call void.
void tens()
{
 if ( command == 11) {
   Serial.println();
   Serial.println("Relay 1 ON");
   pinState = 11;
   relay1();
 }
 else if ( command == 10) {
   Serial.println();
   Serial.println("Relay 1 OFF");
   pinState = 10;
   relay1();
 }
 else if ( command == 21) {
   Serial.println("Relay 2 ON");
   pinState = 21;
   relay2();
 }
 else if ( command == 20) {
   Serial.println("Relay 2 OFF");
   pinState = 20;
   relay2();
 }
 else if ( command == 31) {
   Serial.println("Relay 3 ON");
   pinState = 31;
   relay3();
 }
 else if ( command == 30) {
   Serial.println("Relay 3 OFF");
   pinState = 30;
   relay3();
 }
 else if ( command == 41) {
   Serial.println("Relay 4 ON");
   pinState = 41;
   relay4();
 }
 else if ( command == 40) {
   Serial.println("Relay 4 OFF");
   pinState = 40;
   relay4();
 }
 else if ( command == 51) {
   Serial.println("Relay 5 ON");
   pinState = 51;
   relay5();
 }
 else if ( command == 50) {
   Serial.println("Relay 5 OFF");
   pinState = 50;
   relay5();
 }
 else if ( command == 61) {
   Serial.println("Relay 6 ON");
   pinState = 61;
   relay6();
 }
 else if ( command == 60) {
   Serial.println("Relay 6 OFF");
   pinState = 60;
   relay6();
 }
 else if ( command == 71) {
   Serial.println("Relay 7 ON");
   pinState = 71;
   relay7();
 }
 else if ( command == 70) {
   Serial.println("Relay 7 OFF");
   relay7();
 }
 else if ( command == 81) {
   Serial.print("Relay 8 ON");
   pinState = 81;
   relay8();
 }
 else if ( command == 80) {
   Serial.println("Relay 8 OFF");
   pinState = 80;
   relay8();
 }

 return;
}

//************* Print out the Relay number 9 to 16 and to turn it ON or OFF then call void.
void hundreds() {
 if ( command == 111) {
   Serial.print("Relay 9 ON");
   pinState = 111;
   relay9();
 }
 else if ( command == 110) {
   Serial.print("Relay 9 OFF");
   pinState = 110;
   relay9();
 }
 else if ( command == 121) {
   Serial.print("Relay 10 ON");
   pinState = 121;
   relay10();
 }
 else if ( command == 120) {
   Serial.print("Relay 10 OFF");
   pinState = 120;
   relay10();
 }
 else if ( command == 131) {
   Serial.print("Relay 11 ON");
   pinState = 131;
   relay11();
 }
 else if ( command == 130) {
   Serial.print("Relay 11 OFF");
   pinState = 130;
   relay11();
 }
 else if ( command == 141) {
   Serial.print("Relay 12 ON");
   pinState = 141;
   relay12();
 }
 else if ( command == 140) {
   Serial.print("Relay 12 OFF");
   pinState = 140;
   relay12();
 }
 else if ( command == 151) {
   Serial.print("Relay 13 ON");
   pinState = 151;
   relay13();
 }
 else if ( command == 150) {
   Serial.print("Relay 13 OFF");
   pinState = 150;
   relay13();
 }
 else if ( command == 161) {
   Serial.print("Relay 14 ON");
   pinState = 161;
   relay14();
 }
 else if ( command == 160) {
   Serial.print("Relay 14 OFF");
   pinState = 160;
   relay14();
 }
 else if ( command == 171) {
   Serial.print("Relay 15 ON");
   pinState = 171;
   relay15();
 }
 else if ( command == 170) {
   Serial.print("Relay 15 OFF");
   pinState = 170;
   relay15();
 }
 else if ( command == 181) {
   Serial.print("Relay 16 ON");
   pinState = 181;
   relay16();
 }
 else if ( command == 180) {
   Serial.print("Relay 16 OFF");
   pinState = 180;
   relay16();
 }

 return;
}

//************* voids to turn the outputs High or low 1 to 8.
void relay1()
{

 if (command == 10) {
   digitalWrite(output1, LOW);
 } else {
   digitalWrite(output1, HIGH);
 }

 return;
}

void relay2()
{
 if (command == 20) {
   digitalWrite(output2, LOW);
 } else {
   digitalWrite(output2, HIGH);
 }

 return;
}

void relay3()
{
 if (command == 30) {
   digitalWrite(output3, LOW);
 } else {
   digitalWrite(output3, HIGH);
 }

 return;
}

void relay4()
{
 if (command == 40) {
   digitalWrite(output4, LOW);
 } else {
   digitalWrite(output4, HIGH);
 }

 return;
}

void relay5()
{
 if (command == 50) {
   digitalWrite(output5, LOW);
 } else {
   digitalWrite(output5, HIGH);
 }

 return;
}

void relay6()
{
 if (command == 60) {
   digitalWrite(output6, LOW);
 } else {
   digitalWrite(output6, HIGH);
 }

 return;
}

void relay7()
{
 if (command == 70) {
   digitalWrite(output7, LOW);
 } else {
   digitalWrite(output7, HIGH);
 }

 return;
}

void relay8()
{
 if (command == 80) {
   digitalWrite(output8, LOW);
 } else {
   digitalWrite(output8, HIGH);
 }

 return;
}

//************* voids to turn the outputs High or low 9 to 16.

void relay9()
{
 if (command == 110) {
   digitalWrite(output9, LOW);
 } else {
   digitalWrite(output9, HIGH);
 }

 return;
}

void relay10()
{
 if (command == 120) {
   digitalWrite(output10, LOW);
 } else {
   digitalWrite(output10, HIGH);
 }

 return;
}

void relay11()
{
 if (command == 130) {
   digitalWrite(output11, LOW);
 } else {
   digitalWrite(output11, HIGH);
 }

 return;
}

void relay12()
{
 if (command == 140) {
   digitalWrite(output12, LOW);
 } else {
   digitalWrite(output12, HIGH);
 }

 return;
}

void relay13()
{
 if (command == 150) {
   digitalWrite(output13, LOW);
 } else {
   digitalWrite(output13, HIGH);
 }

 return;
}

void relay14()
{
 if (command == 160) {
   digitalWrite(output14, LOW);
 } else {
   digitalWrite(output14, HIGH);
 }

 return;
}

void relay15()
{
 if (command == 170) {
   digitalWrite(output15, LOW);
 } else {
   digitalWrite(output15, HIGH);
 }

 return;
}

void relay16()
{
 if (command == 180) {
   digitalWrite(output16, LOW);
 } else {
   digitalWrite(output16, HIGH);
 }

 return;
}

//*****************************************************************************************************
//****************************************** Code End *************************************************