Interfacing a Relay with PIC Microcontroller – MikroCBitahwa Bindu
Watch the video tutorial:
A relay is an electromagnetic switch which is used to switch High Voltage/Current using Low power circuits. Relays isolate as well low power circuits from high power circuits, this is a good feature especially for safety reasons a section of the circuit with high dangerous voltage/current could be isolated from the user. When a low voltage is applied to the relay (coil wounded on a soft iron core), this coil becomes a magnet which in turns energizes the soft iron core which closes or open the high voltage/current contacts of the relay. Electromagnetic relays uses an electromagnet to operate a switching mechanism mechanically. A relay can be used to switch higher power devices such as motors, light bulbs and solenoids.
Figure 2: Relay Energized Figure 3: Relay De-energized
Generally a normal Single Pole – Double Throw (SPDT) relay has 5 terminals. Two terminals are used to energize the coil of the relay and the other three are the controlled switch to which a controlled device is connected to. The three terminals are: COM (common), NO (Normally Open) and NC (Normally Closed). When the coil of the relay is not energized, the armature will be connected to NC contact. Thus COM and NC will be connected, anything connected through this NC terminal will be switched on. When the coil is energized, its becomes an electromagnet which will attract the iron armature, this will open the NC contact and close the NO contact. Figures 2 and 3 above show light bulbs connected to NO and COM contacts of relays, on figure 2, Switch 1 is closed, 5V passes through the coil of Relay 1 which energizes it, the magnet attracts the iron armature which closes the NO contact of the relay which in turn closes the 220V circuit of the light bulb L1. The light bulb 1 is on. On figure 3, Switch 2 is open, Relay 2 is de-energized and light bulb L2 is off.
Specifications of a relay
There are a few parameters one has to consider before selecting a relay to ensure safety and proper operation:
- Coil Ratings: This is the low voltage required to energize the relay. Some relays operate with DC voltage of 5V to 12V drawing a current of about 50mA to 100mA.
- Contact Ratings: These ratings are for the high voltage/current terminals of the relay. The commonly used contact ratings are the maximum voltage and current it can handle continuously. No device with higher voltage and/or current rating than the relay should be connected to the relay, otherwise it could be damaged.
- Enclosure and Mounting: The type of an enclosure used depending on the operating environment and the required mounting methods.
A relay interfacing circuit
Figure 4: Interfacing a relay to a PIC microcontroller
If possible, the relay should be powered by a separate power supply to enable a connection of relays requiring a different voltage like for example a 12V relay which is different form the 5V supplied to the Microcontroller. In figure 3, the microcontroller will switch on the transistor Q1 when pin RB0 is high which in turn will switch on the relay, anything connected to the contacts of the relay can thus be switched on or off. Some relays operate with DC voltage of 5V to 12V drawing a current of about 50mA to 100mA. As this current is obviously higher then the maximum 25mA a PIC Microcontroller pin can supply. That is why a relay should never be connected directly to a PIC microcontroller. A transistor circuit like the one above on figure 4 could be used instead.
Note the use of a back emf suppression diode D1 across the relay contacts, this diode is called freewheeling diode. The role of this diode is to prevent damage to the transistor from a back EMF generated from the relay coil when the relay switches. Diode of type 1N4001 or an equivalent is suitable as a freewheeling diode.
In this example, we are going to write a simple program to switch on and off a device connected to a relay an interval of 5 second:
TRISB.F0 = 0; // set RB0 as output
while (1) // endless loop
PORTB.F0 = 1; //Turns ON relay
Delay_ms(5000); // 5S delay
PORTB.F0 = 0; //Turns OFF relay
Delay_ms(5000); //5S delay
You can download the full project files (MikroC source code and Proteus Schematic design) below here.
All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).
MikroC Code: Interfacing a Relay
Proteus Schematic: Interfacing Relay Proteus