Other Arduino Codes and Videos by Robojax

Control AC bulb using 5V relay and Arduino

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

Control AC bulb using 5V relay and Arduino

This code is to control AC bulb using Arduino and 5V relay. Basic code is simple control while the Advanced code makes use of Low-level trigger relay or High-level trigger relay possible.

Resources for this sketch

Basic code to control AC bulb using 5V relay


  /*
 * Arduino code to control AC bulb with relay
 * 
 * 
 * Written by Ahmad Shamshiri for Robojax.com on Saturday August 17, 2019 
 * at 16:27 in Ajax, Ontario, Canada
 * Watch video instruction 
 * for this code: https://youtu.be/g6k8sPJyif8
 * 
 * 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/>.
 */


int relayPin = 3;
int loopDelay = 1000;// delay in loop


void setup() {

    pinMode(relayPin, OUTPUT);// set pin as output
    digitalWrite(relayPin, HIGH); // set initial state OFF for low trigger relay


  
  Serial.begin(9600);// initialize serial monitor with 9600 baud
  Serial.println("Robojax Relay Control- AC bulb");
  delay(2000);
}

void loop() {
  // Robojax AC load control with 5V relay
 // turn relay ON
  digitalWrite(relayPin, LOW);
  Serial.println("Relay is ON");  
  delay(2000);// wait for 2 seconds


 // turn relay OFF
  digitalWrite(relayPin, HIGH);
  Serial.println("Relay is OFF");   
  delay(2000);// wait for 2 seconds
 
  
 Serial.println("===============");

 delay(loopDelay);// wait for loopDelay ms
          
}// loop end   

Advanced code to control AC bulb using 5V relay

This code is to control Low-level trigger relay or High-level trigger relay easily. Please watch video for details


  
/*
 * Arduino code to control AC bulb with relay
 * This is Advanced Code which works with LOW trigger and HIGH trigger relays
 * 
 * Written by Ahmad Shamshiri for Robojax.com on Saturday August 17, 2019 
 * at 16:27 in Ajax, Ontario, Canada
 * Watch video instruction 
 * for this code: https://youtu.be/g6k8sPJyif8
 * 
 * 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/>.
 */


int relayPin = 3;
const int triggerType = LOW;// your relay type
int loopDelay = 1000;// delay in loop

int relayOFF, relayON;// relay ON and OFF values for LOW and HIGH Trigger relays


void setup() {

    pinMode(relayPin, OUTPUT);// set pin as output
    if(triggerType ==LOW){
      relayON = LOW;
      relayOFF = HIGH;
      digitalWrite(relayPin, relayOFF); // set initial state OFF for low trigger relay
    }else{
      relayON = HIGH;
      relayOFF = LOW;           
       digitalWrite(relayPin, relayOFF); // set initial state OFF for high trigger relay     
    }

  
  Serial.begin(9600);// initialize serial monitor with 9600 baud
  Serial.println("Robojax Relay Control - Advanced code");
  delay(2000);
}

void loop() {

 // turn relay ON
  digitalWrite(relayPin, relayON);
  Serial.println("Relay is ON");  
  delay(2000);// wait for 2 seconds


 // turn relay OFF
  digitalWrite(relayPin, relayOFF);
  Serial.println("Relay is OFF");   
  delay(2000);// wait for 2 seconds
 
  
 Serial.println("===============");

 delay(loopDelay);// wait for loopDelay ms
          
}// loop end   

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal