Relay Interfacing with Arduino

What is Relay?

A Relay is an electrical switch that uses an electromagnet to move the switch from the off position to the on position instead of a person moving the switch.

Image of Relay :-


Task 1: To blink the bulb using relay?

Code:-

int relay =32;

void setup()

{

  pinMode(relay,OUTPUT);

}

void loop ()

{

   digitalWrite(relay,LOW);

  delay(2000);

   digitalWrite(relay,HIGH);

  delay(2000);

}

Output:-






Task 2: To turn on/off the bulb using relay?

Code:-

int relay =3;

int sw1=4,sw2=5;

void setup()

{

  pinMode(relay,OUTPUT);

  pinMode(sw1,INPUT);

  pinMode(sw2,INPUT);

  digitalWrite(relay,HIGH);

}

void loop ()

{

  

  if (digitalRead(sw1)==LOW)

  {

   digitalWrite(relay,LOW);

  

  }

  if (digitalRead(sw2)==LOW)

  {

  digitalWrite(relay,HIGH);

  }

}

Output:-