Other Arduino Codes and Videos by Robojax

Controlling 8 channel relay with 4x3 keypad

How to control 8 channel relay with 4x3 keypad with Arduino

This Arduino code is to control 8 AC or 8 DC load connected to 8 channel relay with 4x3 keypad
  1. G3MB Data Sheet (PDF)

 
 /*
 * Library taken : https://playground.arduino.cc/Code/Keypad
 * This is the Arduino code for 4x3 keypad with 8 channel relay
 * watch the video for details and demo 
 *  * 
 * written by Ahmad Shamshiri for Robojax.com 
 * on Feb 04, 2019 at 18:01 in Ajax, Ontario, Canada

 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from http://robojax.com/learn/arduino/

 * 
 */
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
// start of keypad settings 
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// end of keypad settings

// start of relay settings
const int relayPin[]={9,10,11,12, 13, 14, 15, 16};// output pins where 8 relays will be connected
String relayNames[] ={"CH1", "CH2","CH3","CH4","CH5","CH6","CH7","CH8"};// Just put name for 8 relays
// do not change lines bellow
int pushed[] ={0,0,0,0, 0,0,0,0};// status of each buttons
int relayStatus[] ={HIGH,HIGH,HIGH,HIGH, HIGH,HIGH,HIGH,HIGH};// initial status of relay

// end of relay settings
void setup(){
  // Robojax 4x3 keypad with 8ch Relay test
  Serial.begin(9600);
  for(int i=0; i<8; i++)
  {
    pinMode(relayPin[i], OUTPUT); // set relay pins as output  
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
  }
  
  Serial.println("Robojax 8 channel relay keypad");
  
}
  
void loop(){
   // Robojax 4x3 keypad with 8ch Relay test
  int val;
  int knum;
  char key = keypad.getKey();
  
    // just print the pressed key
  if(key && key !='*' && key !='#' && key !='0' && key !='9' ){
    knum = (int)key-49;// convert char to integer (one less)
    if(knum>=0 && knum<8){
     //Serial.println(knum);
         if(relayStatus[knum] == LOW){
        
            pushed[knum] = 1-pushed[knum];
            delay(50);
          }// if   
         controlRelay(knum);// turn relay ON or OFF
    }
  }else{
    val = LOW;
  }
  
  if(knum>=0 && knum<8){
        relayStatus[knum] = val;
  }

delay(50);

}// loop end


/*
 * 
 * @brief Turns the relay ON or OFF 
 * @param relayPin is integer pin of relay
 * @return no return value
 * Written by Ahmad Shamshiri for Robojax.com 
 */

 void controlRelay(int number)
 {

     if(pushed[number] == 1)
     {
      digitalWrite(relayPin[number], LOW);// Turn ON relay
      Serial.print(relayNames[number]);
      Serial.println(" ON");
     }else{
      digitalWrite(relayPin[number], HIGH); // turn OFF
      Serial.print(relayNames[number]);
      Serial.println(" OFF");
     }
 // Robojax 4x3 keypad with 8ch Relay test
}//controlRelay end


   

If you found this tutorial helpful, please support me so I can continue creating content like this. You can support me on Patreon ( Launched June 14, 2019): http://bit.ly/2ZnITLz

Or support me via PayPal