You are on page 1of 1

1

2
3 /*
4 * Arduino code to control AC bulb with relay
5 * This is Advanced Code which works with LOW trigger and HIGH trigger relays
6 *
7 * This code is "AS IS" without warranty or liability. Free to be used as long as you
keep this note intact.*
8
9 */
10
11
12 int relayPin = 3;
13 const int triggerType = LOW;// your relay type
14 int loopDelay = 1000;// delay in loop
15
16 int relayOFF, relayON;// relay ON and OFF values for LOW and HIGH Trigger relays
17
18
19 void setup() {
20
21 pinMode(relayPin, OUTPUT);// set pin as output
22 if(triggerType ==LOW){
23 relayON = LOW;
24 relayOFF = HIGH;
25 digitalWrite(relayPin, relayOFF); // set initial state OFF for low trigger relay
26 }else{
27 relayON = HIGH;
28 relayOFF = LOW;
29 digitalWrite(relayPin, relayOFF); // set initial state OFF for high trigger
relay
30 }
31
32
33 Serial.begin(9600);// initialize serial monitor with 9600 baud
34 Serial.println("Robojax Relay Control - Advanced code");
35 delay(2000);
36 }
37
38 void loop() {
39
40 // turn relay ON
41 digitalWrite(relayPin, relayON);
42 Serial.println("Relay is ON");
43 delay(2000);// wait for 2 seconds
44
45
46 // turn relay OFF
47 digitalWrite(relayPin, relayOFF);
48 Serial.println("Relay is OFF");
49 delay(2000);// wait for 2 seconds
50
51
52 Serial.println("===============");
53
54 delay(loopDelay);// wait for loopDelay ms
55
56 }// loop end

You might also like