Back to Step by Step Course by Robojax

Lesson 65: Controlling 2 channel Solid State relay with push buttons with Arduino

Part 6: Using Relay with Arduino

In this lesson we learn how to use two channel Solid State Relay and turn them ON and OFF using two push button where the state is toggled. Push ON and push off. 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 65: Using Solid State Relay  with Arduino and two push buttons
 *  Arduino code to push-ON and push-OFF 2 channel Solid State Relay
 *  when button pushed, relay ON and stay ON
 *  push again to make it OFF and stay OFF
 * this can be done with 2 push button to control 2 solid state relay
 * written by Ahmad Shamshiri for 
 * on Feb 25, 2019 at 21:38 in Ajax, Ontario, Canada

  Watch instruction video for this code:
  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 <>.
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() {
  // 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;
      ssrON = LOW;
      ssrOFF = HIGH; 
    digitalWrite(ssr1Pin, ssrOFF);// initial relay status to be OFF
    digitalWrite(ssr2Pin, ssrOFF);// initial relay status to be OFF 

 // 2 channel SSR 20190225

void loop() {
// 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;
    }// if  for pushbutton 1 
    if(val2 == HIGH && ssr2Status == LOW){ 
      pushed2 = 1-pushed2;
    }// 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     
        Serial.println("SSR1 OFF");
        digitalWrite(ssr1Pin, ssrOFF);  
      }// else   

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

// 2 channel SSR 20190225 
}// loop 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