View Course Path

Interfacing 8051 with relays to drive high power peripherals

Let’s say you want to control the appliances in your house using a microcontroller. But here is the issue. When it comes to power, a microcontroller cannot output a high wattage. That might make you think that a low power device like a microcontroller can perhaps not control high power peripherals. This is where relays come in. In short, a relay is a switch that enables a microcontroller to control high power external devices. It is a gateway that takes in a control signal from a microcontroller, but sources the power signal from a high power source. It sort of makes the two signals work in tandem to allow the controller to control high powered appliances.

In this post, we will understand the basics of relays and their types. And we will interface these relays with an 8051 microcontroller.

What is a Relay?

A relay is an electrically operated switch which uses small electronic stimuli from an external device to control high power peripherals. The construction of the relay helps to provide electrical isolation between two circuits which is very important in the case of microcontrollers. Due to the electrical isolation properties and high load-carrying capacity of the relays, they are used to control high power external devices like fans, lights and motors. There are two types of relays:

  1. Electromechanical relays
  2. Solid-state relays.
Relays are not the source of the power that they provide to the appliance. They act just like wall switches but are controlled using electrical signals.

Electromechanical Relays

As the name suggests, an electromechanical relay uses an electromagnet and mechanical contacts to control devices. It uses the principle electromagnetism to move a mechanical armature. Let us look at the innards of an electromechanical relay to get an insight into how they work.

Relay working

The electromechanical relay consists of an electromagnet, which, when energized, moves the metal armature. The armature is connected to the normally closed contact when the electromagnet is not energized. Once energized the electromagnet pulls the armature to the normally open contact which completes the circuit driving an external device.

Solid-state relays

A solid-state relay, in contrast to the electromechanical relay, does not have any movable parts. Due to this reason, solid-state relays have a faster switching time and higher life cycles when compared to electromagnetic relays. Solid-state relays use optocouplers instead of electromagnets to provide electrical isolation and control. In the case of DC output devices, solid-state relays use silicon-controlled rectifiers with the optocouplers whereas, in the case of AC loads, they use a TRIAC or multiple thyristors.

Solid state relay

The figure shown above depicts the working of a Solid-state relay with an AC load using a TRIAC and a zero-crossing detector.

Solid-state relays vs Electromechanical relays

Solid-state relays Electromechanical relays
Due to the use of zero-crossing detectors, solid-state relays minimize electrical noise. Electromechanical relays use coils which act as inductors, and they can cause electrical noise like back-EMF.
Have longer life cycles as they have lesser moving parts. Due to a lot of moving parts, they have shorter life cycles.
Have a faster response time and lower power consumption. Have slower response time as mechanical switches control external devices.
Use optocouplers for the controlling mechanism. Use electromagnets for the controlling mechanism.
Are highly resistant to shocks and vibrations as they do no have a lot of moving parts. Are not resistant to vibrations as they have a lot of moving parts.
Can be used in places with high electromagnetic forces. Cannot be used in places with high values of electromagnetic forces.
Have a higher initial cost. Cheaper compared to solid-state relays.
As they use semiconductors to control output circuits, a spike in voltage can cause damage to them. External variations from output side do not damage the device as they use physical switches.

Why use Relays?

When it comes to controlling devices with high power requirements, microcontrollers are simply not cut out for the task of power sourcing. Relays act as a switching device which uses the low power control signal from the microcontroller to control external devices by using electromagnets/optocouplers. They provide a way for the microcontroller to control high power peripherals while providing electrical isolation to the microcontroller.

Connecting a microcontroller directly to a relay is not the right way to go. The main reason for that is the fact that the current from the microcontroller is too less to control the switching mechanism of the relay. Due to this, a transistor is required to amplify the signal. In addition to this, in the case of electromagnetic relays, a diode is required to prevent the back EMF from damaging the transistor and the microcontroller.

Components required for interfacing relays with 8051

  1. 8051 microcontroller – AT89C51/AT89S51/52 or similar variants.
  2. 12 v electromechanical relay
  3. Solid-state relay
  4. BC 548 NPN transistor
  5. Flywheel diode 1N4148
  6. Oscillator crystal – 12Mhz
  7. Capacitors – 22pF x 2, 10µF x 1
  8. Resistors – 10kΩ x 1, 4.7kΩ x 2
  9. 12V DC supply

 Interfacing 8051 with an electromechanical relay  

 

To interface the relay with an 8051, follow these steps. In our case, we are using Proteus as the simulation software and the AT89C51 variant of the 8051 microcontroller.

  • Connect the oscillator circuit to pins 19 and 20. This includes a crystal oscillator and two capacitors of 22uF each. Connect them to the pins, as shown in the diagram.
  • Connect one end of the capacitor to the EA’ pin and the other to the resistor. Connect this resistor to the RST pin, as shown in the diagram.
  • We are using port 2.o as the output port, to control the relay using a relay driver circuit.
  • Connect one end of port 2.0 to a 4.7k ohm resistor. Connect the other end of the resistor to the base of the transistor. This turns on the transistor when a small current flows through the same—allowing the current to flow through it.
  • Ground the collector of the transistor.
  • Connect the emitter to the positive end of the diode to block out the back EMF from the relay. Connect the other end to one of the terminals of the relay.
  • Connect the negative end of the diode to 12V DC supply and the other to the power the relay.
  • Connect one end of the terminals of the relay to an AC source of 220v and 60Hz. Connect the other end to a lamp or any other peripheral you wish to drive using the relay.
  • Ground the other terminal of the peripheral.

C code to interface a relay with 8051

#include<reg52.h>
sbit relay_pin = P2^0; gives a variable name to port 2
void Delay_ms(int); predefines the delay function
void main()
{
do // loop to contineously switch the relay 
{
relay_pin = 0; 
Delay_ms(1000);
relay_pin = 1; 
Delay_ms(1000);
}while(1);
}

void Delay_ms(int k) // function to generate delay 
{
int j;
int i;
for(i=0;i<k;i++)
{
for(j=0;j<1275;j++)
}
}

The code given above is used to switch an external lamp connected to the relay on and off every 1000 milliseconds. To do this, the microcontroller sends a pulse to port 2.0 to which the relay driving circuit is connected. The microcontroller sends a high signal to the base of the transistor, which allows the relay to get charged. The diode provides protection to the transistor from the back EMF of the inductor in the electromagnet.

Interfacing 8051 with solid-state relays

Interfacing solid state relay to the 8051 is similar to that of an electromechanical relay, but it does not require a driver circuit. The main reason for this is that it does not cause problems like back EMF and can be controlled directly by using a microcontroller.

The same code given above can be used to control the solid-state relays as well.

We hope that reading this article has helped you understand relays and how to interface them with microcontrollers in a better way. If you have any issues in understanding the same feel free to contact us through the comments section and we will get back to you in a jiffy. In the next post in this 8051 course, we will interface a Bluetooth module to the 8051 and use it to control an appliance via a relay.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top