Back to Arduino Step By Step Course

Lesson 66: Controlling 4 channel relay with push button switches

دروس آردوینو به فارسی

Lesson 66: Controlling 4 channel relay with push button switches

Please select other codes for this lecture from the links below.

Part 6: Using Relay with Arduino

In this lesson we learn how to control 4 channel relay with 4 push buttons. There are two codes. This code (lesson 66-2) is to push and hold to turn the light on and keep it pressed. The code lesson 66-1 is to toggle the relay like push-on, push-off.

  * Lesson 66-2: Controlling 4 channel relay with push button switches
 *  Arduino code to use 4 push buttons to turn ON of OFF 4 channel relay
 *  push and hold to turn ON
 *  release to turn  OFF
 * this can be done with 4 push button to control 4 relays.
 See lesson 66-1 for the code that toggles the light push-on, push-off
  Watch instruction video for this code:

 * written by Ahmad Shamshiri for 
 * on December 11, 2018 at 19:39 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. Make a donation using PayPal by credit card

Get this code and other Arduono codes from
  * 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 <>.
const int pushButton[] ={2,3,4,5};// define push button inputs
const int relayPin[]={11,10,9,8};// output pins where 4 relays will be connected
String relayNames[] ={"CH1", "CH2","CH3","CH4"};// Just put name for 4 relays
int pushed[] ={0,0,0,0};// status of each buttons
int relayStatus[] ={HIGH,HIGH,HIGH,HIGH};// initial status of relay

void setup() {
  // 4-Relay-4-Push button 20181211
  Serial.begin(9600);// initialize serial monitor 
  for(int i=0; i<4; i++)
    pinMode(pushButton[i], INPUT_PULLUP); 
    pinMode(relayPin[i], OUTPUT);   
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF

// 4-Relay-4-Push button 20181211

void loop() {
// 4-Relay-4-Push button 20181211

  for(int i=0; i<4; i++)
     int  val = digitalRead(pushButton[i]);   

      if(val == HIGH){
        Serial.println(" ON");
        digitalWrite(relayPin[i], LOW); 
        Serial.println(" OFF");
        digitalWrite(relayPin[i], HIGH);
      }// else   
  }// for 
// 4-Relay-4-Push button 20181211  
}// loop 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 and affiliated sites.