Back to Step by Step Course by Robojax

Lesson 67: Controlling 8 channel realy with keypad using Arduino

Part 6: Using Relay with Arduino

In this lesson we learn about relay and how it functions. Then give example of electromagnet and how relay function internally. The wiring of a relay module is shown and code is explained.

  • 00:00 Introduction
  • 08:32 Relay power rating
  • 09:54 Wiring Explained
  • 11:39 Code Explained
  • 13:31 Demonstration

 Lesson 67: Arduino code for 4x3 keypad with 8 channel relay
 * Library taken :
 * This is the Arduino code for 4x3 keypad with 8 channel relay
  Watch instruction video for this code:
 *  * 
 * written by Ahmad Shamshiri for 
 * on Feb 04, 2019 at 18:01 in Ajax, Ontario, Canada

  This video is part of Arduino Step by Step Course which starts here:


If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon

or make donation using PayPal

 *  * 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
    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
    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 <>.

/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
// start of keypad settings 

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {

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
  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){
         if(relayStatus[knum] == LOW){
            pushed[knum] = 1-pushed[knum];
          }// if   
         controlRelay(knum);// turn relay ON or OFF
    val = LOW;
  if(knum>=0 && knum<8){
        relayStatus[knum] = val;


}// 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 

 void controlRelay(int number)

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


The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side