A Relay

Figure 1: A Relay

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 4, 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 off. A 1N4001 diode or an equivalent is suitable as a freewheeling diode.

MPLAB XC8 Code

To switch ON and OFF a device connected to a relay is very simple, it’s like sending a command to switch ON or OFF an LED. Once the output pin is high with a logic ‘1’, the transistor will be switched ON as a switch which in turn will energize the relay. A logic ‘0’ will switch OFF the transistor and the relay will become de-energized. All we need is two steps: Set the PORT direction to Output with the TRIS Register and send a logic ‘1’ or ‘0’ to the output with the LAT Register.

1. Set The PORT bit Direction with the TRIS Register. 1 will make it an input and 0 an output.

Example: TRISBbits.RB0 = 0;                 //Set PORTB.0 only as Output

2. Output a LOW (to switch off) or a HIGH (switch on) to the PORT with the LAT Register.

Example: LATBbits.RB0 = 1;        //Switch ON PORTB.0

LATBbits.RB0 = 0;        //Switch OFF PORTB.0

Figure 4 shows a relay interfaced to a Microcontroller, the below code will energize the relay for 2 second then de-energize it for 1 second and so on. Anything connected to the relay will switch ON and OFF. 2 Seconds ON and 1 Second OFF.

You can download the full project files (MPLAB XC8 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).

MPLAB XC8 Code: Interfacing Relay XC8 Code

Proteus Schematic: Relay XC8 Proteus