Relay Introduction
A relay is an electromagnetic switch operated by a relatively small electric current that can turn on or off a much larger electric current. The heart of a relay is an electromagnet (a coil of wire that becomes a temporary magnet when electricity flows through it). You can think of a relay as a kind of electric lever: switch it on with a tiny current and it switches on (“leverages”) another appliance using a much bigger current. Why is that useful? As the name suggests, many sensors are incredibly sensitive pieces of electronic equipment and produce only small electric currents. But often we need them to drive bigger pieces of apparatus that use bigger currents. Relays bridge the gap, making it possible for small currents to activate larger ones. That means relays can work either as switches (turning things on and off) or as amplifiers (converting small currents into larger ones).
There different types of relay like 2 relay , 4 relay. The number says, number devices a relay can control
Default state of relay
When there is no power given, the default state of relay is from common input it will pass the input to the normally closed.
This program LED switch on /off is performed by Bluetooth through 2 relay
Connections
Code
#include <SoftwareSerial.h>
SoftwareSerial bluetooth (4,5);
char val; // variable to receive data from the serial port
int ledpin = 2; // LED connected to pin 2 (on-board LED)
void setup()
{
pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 115200bps
bluetooth.begin(9600);
int relayled=7;
pinMode(7,OUTPUT);
}
void loop()
{
digitalWrite(7,HIGH);
if( bluetooth.available() ) // if data is available to read
{
val = bluetooth.read(); // read it and store it in 'val'
Serial.println(val);
}
if( val == 'a' ) // if 'a' was received led 2 is switched off
{
digitalWrite(ledpin = 2, HIGH); // turn Off pin 2
}
if( val == 'A' ) // if 'A' was received led 2 on
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
}
if( val == 'b' ) // if 'b' was received led 3 is switched off
{
digitalWrite(ledpin = 3, HIGH); // turn Off pin 3
}
if( val == 'B' ) // if 'B' was received led 3 on
{
digitalWrite(ledpin = 3, LOW); // turn ON pin 3
} //else (ledpin = 3, LOW) //set led pin 3 to low state
if( val == 'C' ) // if 'C' was received led 2 on for 1 second
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
delay(1000); // wait 1 second
digitalWrite(ledpin, HIGH); // turn Off pin 2
}
if( val == 'D' ) // if 'D' was received led 3 on for 1 second
{
digitalWrite(ledpin = 3, LOW); // turn ON pin 3
delay(1000); // wait 1 second
digitalWrite(ledpin, HIGH); // turn Off pin 3
}
if( val == 'E' ) // if 'E' was received led 2 on for 5 seconds
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
delay(5000); // wait 500 milli seconds
digitalWrite(ledpin, HIGH); // turn Off pin 2
}
if( val == 'F' ) // if 'F' was received led 3 on for 5 seconds
{
digitalWrite(ledpin = 3, LOW); // turn ON pin 3
delay(5000); // wait 500 milli seconds
digitalWrite(ledpin, HIGH); // turn Off pin 3
}
if( val == 'G' ) // if 'G' was received turn led pin 2 on for 500ms then switch off and turn on pin 3 for 500 mili seconds then off
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
delay(500); // wait 500mili second
digitalWrite(ledpin, HIGH); // turn Off pin 2
digitalWrite(ledpin = 3, LOW); // turn ON pin 2
delay(500); // wait 500 mili second
digitalWrite(ledpin, HIGH); // turn Off pin 2
}
if( val == 'h' ) // if 'h' was received switch off all pins
{
digitalWrite(ledpin = 13, LOW); // turn Off pin 13
digitalWrite(ledpin = 2, HIGH); // turn Off pin 2
digitalWrite(ledpin = 3, HIGH); // turn Off pin 3
}
if( val == 'H' ) // if 'H' was received switch pin 2 on and off 1000 times
for(int i = 0; i < 1000; i++)
{
digitalWrite(ledpin = 2, HIGH); // turn ON pin 2
delay (1000); //wait 1000 mili seconds
digitalWrite(ledpin = 2, LOW); // turn Off pin 2
delay (1000); //wait 1000 mili seconds
}
}

