To control our high voltage device like home appliance by pic microcontroller, we have to have clear idea on interfacing relay with pic microcontroller. Here I try to answer your very important question with simulation that how to interface relay with pic microcontroller?
To discuss it elaborately I have to discuss what is relay? How it work? And how we can use it in our project?
Relay is an electrically operated switch work on electro-magnetism principal here you see the block diagram of
Now working principle of relay is very simple when P1 and p2 get a certain amount of voltage then coil between P1 and P2 get energized and create a magnetic field around it. For this magnetic field the metal switch liver which is attach to the common pin is attracted towards the NO pin and connection made between NO and common pin.
So by using relay switch we easily turn on or off any high voltage device by microcontroller. See the block diagram below to how we turn on or off a 230 volt AC bulb by micro controller.
Interfacing Relay with PIC Microcontroller :
To interface relay with microcontroller we need a driver circuit to drive the power to relay. See the block diagram
Now question is how the driver circuit works?
In my project I used 6V relay but supply given 12 V and diode D1 isolate the voltage between P1 and P2 pin. When microcontroller send out put voltage through R1 register at base of transistor T1, transistor T1 pull the some voltage through coil between P1 and P2 pin and grounded. Finally 6 volt is reached between pin P1 and P2 and relay is activated and it connect NO pin from NC pin of relay. When NO pin connected then bulb get 230 V ac and it glowing after some time delay it again off and repeating the step.
In bellow see the embedded C code for interfacing relay with pic microcontroller
// Author : Subham Dutta
// Date : 06-02-14
// Website : www.nbcafe.in
trisb = 0;
portb = 0;
while (1)
{
portb = 0b00000001;
delay_ms(1000);
portb = 0b00000000;
delay_ms(1000);
}
}
See the simulation video how it simulate in Proteus