Relay Module interfacing with pic16f877a microcontroller, In this pic microcontroller tutorial you will learn how to interface relay module with any pic microcontroller or any microcontroller? How to control ac load with relay? How to turn on and turn off relay with pic microcontroller by using digital output ports of pic16f877a microcontroller. you will see it is very easy to interface relay with pic16f877a microcontroller. Relay has many applications in pic microcontroller based projects and embedded systems projects. you may find its applications in home automation projects. Check the following articles on relay interfacing:
Now lets start with introduction of relay and how relayworks and at the end of article, we will see how to interface relay module with pic microcontroller.
What is Relay?
Relay is an electromagnetic switch which is controlled with a small current and is used for turned on or off high electric circuits or appliances. Means its magnetic coil is turned on or off with a small current but its magnetic contacts are used for controlling high currents. Two types of rely modules are commonly used, first one is ac relay module whose magnetic coil is operated at ac voltages. Similarly, second one is dc relay module whose magnetic coil is operated at dc voltages. Both relay modules are easily available in market or online shop. But in this article, we will only talk about dc voltages relay module. A simple relay which is operated at dc voltages is shown in figure 1
Figure 1 A Simple Dc Operated Relay Module
Pin Configuration of Relay Module
Relay modules are available in different channels such as single channel and multi channel etc. Single channel consists of only single relay and multi channel consists of multi relays which are connected in parallel with each other. Every relay normally consists three inputs pins such as VCC, GND and signal etc. Similarly, it also consists of three output pins such as NC (normally close), NO (normally open) and COM (common). Supply voltages are applied at VCC and GND pin which are normally 5V dc and signal voltages are applied at signal pin. Every relay module also consists of two LEDs which tell the relay is power on or off.
Working Principle of Relay Module
Relay works on the principle of electromagnetic switch. When voltages are applied at its input pins such as at VCC and GND then this relay is power on, means it comes into active position. Because its consists of normally open (NO) and normally close (NC) contacts which are used for controlling high current and this current could be ac or dc. Both positions such as when voltages are applied at input pins and when voltages are not applied at input pins are shown in figure 2
Figure 2 Relay Module Activated and Deactivated Positions
According to the figure 2 when dc voltages are not applied at input pins of relay module then this relay does not come into in active position and common point remain connected with NC point means with normally close point. During this position if any current is available at common point then this current is passed through normally close point. Then the circuit or thing which is connected with normally close point, it would be come into turned on position otherwise it would be remained in turned off position. Similarly, when the voltages are applied at input pins of relay then this relay come into in active position means when voltages are applied at coil terminals then electromagnetic field is produced.
This electromagnetic field attracts the armature which is the liver connected through spring. During this position the common point is connected with normally open point then current is passed through common point to normally open point shown in figure 2. Then the circuit or thing which is connected with normally open point, it would be come into turned on position otherwise it would be remained in turned off position. Similarly, the high currents are controlled through this relay module. These are available in different current ratings. Circuit diagram of relay module is shown below:
How to Interface Relay Module with PIC16f877a Microcontroller
To interface pic microcontroller with relay, we need to use digital output port of pic 16F877A microcontroller. When we apply digital high signal to relay, relay becomes on and when we apply digital low signal to relay ,relay turns off. As we discussed in above paragraph, voltages are required to turned on the relay module which are normally 5V dc and this voltages could be taken with any controller such as microcontroller or Arduino etc. Here we shell take this voltages from any port of pic microcontroller. It is programmed in c language with the help of mikro/c software and it is also powered up with 5V dc.
This microcontroller controlled the relay with so efficiently and intelligently. Circuit diagram for relay module interfacing with pic16f877a microcontroller is shown below. I have connected a push button with pic microcontroller which will be used to turn on and turn off relay module.
Suppose we want to turn on the relay module for any specific time then we shell tell the microcontroller through programming to turn on the relay module for some time . Then this microcontroller automatically gives 5V dc voltages for that specific time to turn on the relay module. So, we can easily control the relay module with pic 16f877a microcontroller. Check the video for complete demonstration of how to relay module interfacing with pic microcontroller works.
Code for relay interfacing with pic16f877a microcontroller
Code for relay interfacing is written using Mikor c for pic compiler. One pin of microcontroller is used as a digital input pin and second pin is used as a digital output pin.
void main() { ADCON1=0X0F; TRISB.B1=0; TRISE.B0=1; while(1) { if (PORTE.B0==1) { PORTB.B1=1; } else { PORTB.B1=0; } } }
Hi i’m trying to control the light using arduino uno with two channel relay board for automatic light intensity system using LDR and but the relay was operating reverse functioning but i don’t know how to solve the problem.
if you are using a low active relay ,reverse the logic on the code.
or use a high active relay.
Can you provide example on solinoid valve control wit pic16 or 18
How can I use mplab not mikro c software
please check this getting started gudie:
http://microcontrollerslab.com/mplab-xc8-compiler-getting-started/