Search Code

4-Channel Solid-State Relay with Arduino

4-Channel Solid-State Relay with Arduino

This video and code will help you turn on or turn off four solid-state relays (SSRs) using Arduino.

Images

How to use a 4-channel solid-state relay with Arduino to control a 4A bulb or load
How to use 4 channel Solid State Relay with Arduino To control 4 A bulb or load
184-Arduino code to control a four-channel solid-state relay
Language: C++
++
/*
 * This is code to control 4-channel Solid State Relay (SSR) modules using Arduino.
 * Written by Ahmad Shamshiri for Robojax.com 
 * on January 11, 2019 at 21:03 in Ajax, Ontario, Canada.

  Watch instruction video for this code: https://youtu.be/Rfaz5qOLKYQ
  
 * 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/>.
 */


int ssr[]={2,3,4,5}; // Arduino pin numbers used for SSR
int triggerType = HIGH;// type LOW if low trigger and HIGH if high trigger SSR is used
int wait = 2000;// delay time

int ssrON, ssrOFF;// used for two different SSR trigger type. Do not change

void setup() {
  
    Serial.begin(9600);// prepare Serial monitor
    // set pins as output
    if(triggerType)
    {
      ssrON = HIGH;
      ssrOFF = LOW;
    }else{
      ssrON = LOW;
      ssrOFF = HIGH; 
    }
 for(int i=0; i < 4; i++)
 {    
    pinMode(ssr[i], OUTPUT);// sent i(th) pin as output
    digitalWrite(ssr[i], ssrOFF); // Turn the SSR OFF  
 }
               
    Serial.println("Robojax 4 SSR ");

    
}

void loop() {

  // this is just a demo
 for(int i=0; i < 4; i++)
 {
      contrlSSR(i+1, ssrON, 2000); // turn SSRs one by one ON
 }// for loop


 for(int i=0; i < 4; i++)
 {
      contrlSSR(i+1, ssrOFF, 1000);  // turn SSRs one by one OFF
 }// for loop
    

 
   contrlSSR(1, ssrON, 2000);  // turn SSR1 ON for 2 seconds
   contrlSSR(2, ssrON, 3000);// turn SSR2 ON for 3 seconds
   contrlSSR(4, ssrON, 2000);// turn SSR4 ON for 2 seconds
 
   contrlSSR(1, ssrOFF, 0); // turn ssr1 OFF
   contrlSSR(4, ssrOFF, 0);// turn ssr4 OFF
   contrlSSR(2, ssrOFF, 0);// turn ssr2 OFF
    
   delay(2000);
    Serial.println("====== loop done ==");

}// loop



/*

 *  contrlSSR turns OFF or ON SSR
 *  January 11, 2019
 * @param  is an integer, SSR number starts with 1
 * @param control should be ssrON or ssrOFF 
 * @param wait is a number in milliseconds. 

 * 
 */
void contrlSSR(int number, int control, int wait)
{
  if(control == HIGH)
  {
     Serial.print("SSR "); Serial.print(number);Serial.println(" ON");
      digitalWrite(ssr[number-1], ssrON); // Turn the SSR ON    
      delay(wait); 
  }else{
     Serial.print("SSR "); Serial.print(number);Serial.println(" OFF");
      digitalWrite(ssr[number-1], ssrOFF); // Turn the SSR OFF    
      delay(wait);     
  }
}

Resources & references

No resources yet.

Files📁

No files available.