Back to Arduino Step By Step Course

Lesson 67: Controlling 8 channel realy with keypad using Arduino

If you don't like to see ads while video is being played you can purchase this course for $200 from Udemy or purchase YouTube premium Here

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.


  /*
 Lesson 67: Arduino code for 4x3 keypad with 8 channel relay
 * Library taken : https://playground.arduino.cc/Code/Keypad
 * This is the Arduino code for 4x3 keypad with 8 channel relay
  Watch instruction video for this code: https://youtu.be/dkESWpdDBYk
 *  * 
 * written by Ahmad Shamshiri for Robojax.com 
 * on Feb 04, 2019 at 18:01 in Ajax, Ontario, Canada



  
  This video is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8

 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

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

 * 
 */
/* @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 

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

   

The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps 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 Amazon.com and affiliated sites.