8051 Micro-controller

Interfacing of Relay with microcontroller


Relay is an electromagnetic switch, which generally use in industries and houses to control the different machine and appliances like lamp, water pump , road light AC compressor..etc.

Relay is a combination of mechanical switch and an electromagnet, it prefer when we want to control a device or machine using the low strength signal.
In this tutorial we will learn about the interfacing of relay with microcontroller and learn how to control a device using the relay.

Working of relay

Relay works on the basis of electromagnetic force. Internally relay consist of a coil of wire wrapped soft iron core. When we have applied the voltage between the ends of wrapped wire, then there is a current flow in wrapped wire. So due to this current a magnetic field would be generated and attract the switch from NC (normal close) to NO (normal open).


Simple connection of relay

In below image I have describe the simple connection of a lamp with a SPDT (single pole double throw) relay.


working of relay

Sample program to Interface relay with 8051 microcontroller.

In this program, we will control the bulb using the serial communication to sends a specific command. When user sends 1 (command) from the virtual window to  the microcontroller, then bulb will be turn on and  if user sends 2 (command) from the virtual window then bulb will be turn off.

Note: here I have assumed, you have the knowledge of the serial communication.




Your opinion matters

Here I have tried to explain the about the relay and tried to explain the connection of relay with 8051 micro-controller through a npn transistor.I would like to know your thought about the above discuss topic, so please don’t forget to write comment in a comment box.


1 Comment

  1. Jeff Verive

    The first schematic showing the operation of the circuit does not have a resistor in series with the transistor’s base, though it does show up in the second schematic. Calculating the appropriate value for this resistor is not a trivial task (at least for the hobbyist who has little experience in this calculation). Can you provide your calculations?

Leave a Reply

Theme by Anders Norén