Back to Arduino Step By Step Course

Lesson 66: Controlling 4 channel relay with push button switches

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 66: Controlling 4 channel relay with push button switches
 *  
 *  when button pushed, relay ON and stay ON
 *  push again to make it OFF and stay OFF
 * 
 *  
 * written by Ahmad Shamshiri for Robojax.com 
 * on Feb 25, 2019 at 21:38 in Ajax, Ontario, Canada


  Watch instruction video for this code: https://youtu.be/0PW22PD89u0
  
  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/>.
 
 */
const int pushButton1 = 2;// define push button inputs
const int pushButton2 = 3;// define push button inputs

const int ssr1Pin =8;// output pins where solid state  will be connected
const int ssr2Pin =9;// output pins where solid state  will be connected

int triggerType = HIGH;// type LOW if low trigger and HIGH if high trigger SSR is used

int pushed1 =0;// status of button 1
int pushed2 =0;// status of button 2
int ssr1Status =HIGH;// initial status of ssr 1 (HIGH or LOw)
int ssr2Status =HIGH;// initial status of ssr 2 (HIGH or LOw)
int ssrON, ssrOFF;// used for two different SSR trigger type. Do not change

void setup() {
  // Robojax.com 2 channel SSR 20190225
  Serial.begin(9600);// initialize serial monitor 

    pinMode(pushButton1, INPUT_PULLUP); // define pin for pushButton1
    pinMode(pushButton2, INPUT_PULLUP); // define pin for pushButton1
        
    pinMode(ssr1Pin, OUTPUT);// define pin for SSR1   
    pinMode(ssr2Pin, OUTPUT); // define pin for SSR2  
    if(triggerType ==HIGH)
    {
      ssrON = HIGH;
      ssrOFF = LOW;
    }else{
      ssrON = LOW;
      ssrOFF = HIGH; 
    }       
    digitalWrite(ssr1Pin, ssrOFF);// initial relay status to be OFF
    digitalWrite(ssr2Pin, ssrOFF);// initial relay status to be OFF 

 // Robojax.com 2 channel SSR 20190225
}

void loop() {
// Robojax.com 2 channel SSR 20190225


     int  val1 = digitalRead(pushButton1); // read pushButton1 vlaue
     int  val2 = digitalRead(pushButton2); // read pushButton2 vlaue 
            
    if(val1 == HIGH && ssr1Status == LOW){ 
      pushed1 = 1-pushed1;
      delay(100);
    }// if  for pushbutton 1 
    if(val2 == HIGH && ssr2Status == LOW){ 
      pushed2 = 1-pushed2;
      delay(100);
    }// if  for pushbutton 2 

  ssr1Status = val1;// save the pushbutton 1 status
  ssr2Status = val2;// save the pushbutton 2 status
      if(pushed1 == HIGH){
        Serial.println("SSR1 ON");
        digitalWrite(ssr1Pin, ssrON); // Turn SSR1 ON     
      }else{
        Serial.println("SSR1 OFF");
        digitalWrite(ssr1Pin, ssrOFF);  
      }// else   

       if(pushed2 == HIGH){
        Serial.println("SSR2 ON");
        digitalWrite(ssr2Pin, ssrON); // Turn SSR2 ON     
      }else{
        Serial.println("SSR2 OFF");
        digitalWrite(ssr2Pin, ssrOFF);  
      }// else  

    Serial.println("=="); 
  delay(100);
// Robojax.com 2 channel SSR 20190225 
}// 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