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

Related Code and Videos

1-4 Keys Keypad
2-4x3 Kaypad
3-4x4 Keypad
4-4x4 Keypad Black (hard shell)
5-5x4 Keypad
6-4x3 Keypad: Controlling 8 Channel Relay

Resources for this code

  1. Library code for 4x4 Keypad
  2. Get Schematic diagram for this sketch from Arduino Course on Udemy
  3. Learn Arduino step by step from beginner to Advance (Coruse)
  4. Get Early Access to my videos via Patreon

 
 /*
 * 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. support me via PayPal