In this tutorial we will learn Relay interfacing with PIC16F877A. Relay is very important component to interface the heavy appliances with the help of microcontroller.
Post Contents
Relay
Relays are devices which allow low power circuits to switch a relatively high Current/Voltage ON/OFF. A relay circuit is typically a smaller switch or device which drives (opens/closes) an electric switch that is capable of carrying much larger current amounts.
Principle
Current flowing through the coil of the relay creates a magnetic field which attracts a lever and changes the switch contacts. The coil current can be on or off so relays have two switch positions and most have double throw (changeover) switch contacts.
Construction and working
Relays are made up of electromagnet and a set of contacts generally based on Single Pole Double Throw (SPDT) or Double Pole Double Throw (DPDT) switching method. It has 3 pins to perform function –
- COM = Common, always connect to NC; it is the moving part of the switch.
- NC = Normally Closed, COM is connected to this when the relay coil is off.
- NO = Normally Open, COM is connected to this when the relay coil is on.
You can easily understand the concept of Relay by looking this below image.
Prerequisites
Before Interfacing, every one should know about the Relay Driver which is used to interface the relay to microcontroller.
Relay Interfacing with PIC16F877A
Circuit Diagram
In this tutorial I’m connecting One Relay in Port B.0. And switch is connected into Port B.1.
Code
If you want to make Relay ON, Just give the High Signal (1) to that pin which is connected to ULN2803. The concept of this code is just turn On the Relay when the button is pressed.
#include<htc.h> #define relay RB0 #define sw RB1 void main() { TRISB0=0; TRISB1=1; while(1) { if(sw==1){ relay=1; } else { relay=0; } } }
Output
[ Please find the output image Here ]
So Programming wise its very simple. It is like a LED program. But Hardware wise we should be careful. Because we might connect AC power supply devices here. So Be careful guys. You may connect Fan, AC Motor, Lamp etc to relay.