Search Code

Lesson 65: Controlling Two-Channel Solid-State Relays with Push Buttons Using an Arduino

Lesson 65: Controlling Two-Channel Solid-State Relays with Push Buttons Using an Arduino

461-Lesson 65: Controlling a 2-channel solid-state relay with push buttons using an Arduino
Language: C++
++
/*
  * Lesson 65: Using a Solid State Relay with Arduino and two push buttons
 *  Arduino code to push-ON and push-OFF 2-channel Solid State Relays
 *  when button pushed, relay ON and stay ON
 *  push again to make it OFF and stay OFF
 * this can be done with 2 push buttons to control 2 solid state relays
 *  
 * written by Ahmad Shamshiri for Robojax.com 
 * on February 25, 2019 at 21:38 in Ajax, Ontario, Canada


  Watch instruction video for this code: https://youtu.be/jGncm9kPt3Y
  
  This video is part of the 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. 

or make a 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 downloaded 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 value
     int  val2 = digitalRead(pushButton2); // read pushButton2 value 
            
    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

Resources & references

No resources yet.

Files📁

No files available.