Microcontroller Relay Driver and Interface

Microcontroller Relay Interface and Driver

Many microcontroller designs typically mix multiple interfacing methods. A microcontroller (µC) system can be viewed as a system that reads from inputs, performs processing and writes to outputs. Microcontrollers are useful to the extent that they communicate with other devices, such as sensors, motors, switches, keypads, displays, memory and even other micro-controllers. Often a need arise to interface output of the microcontroller with an electromagnetic relay (EMR).

Relays are devices which allow low power circuits to switch a relatively high Current and/or Voltage on/off. Here is a simple microcontroller-relay interface circuit with perfect “galvanic isolation”. “Galvanic isolation”means an isolation between two circuits, i.e. no metal conduction between those circuits. Transfer will then take place for instance optical or by induction. Galvanic means “related to DC”. Galvanic Isolation says that the driver circuit is separated from the signal source in such a way that DC current cannot bridge the connection. The widely accepted method for galvanic isolation is the use of optical isolator (optocoupler/photocoupler).

Relay with Microcontroller Schematic

Microcontroller Relay Driver and Interface

CNY17-1 from Vishay Semiconductors is an optically coupled pair consisting of a gallium arsenide infrared emitting diode optically coupled to a silicon NPN phototransitor. Signal information can be transmitted by the device while maintaining a high degree of electrical isolation between input and output.

Interfacing this circuit with a µC is very simple and straight forward. Input of the circuit can be connected to the selected output port of the µC through the input terminals. However keep an eye on the supply polarity. Logic 1 (H = 5V) at the input of the optocoupler PC1 (CNY17-1) will switch on the electromagnetic relay, and logic 0 (L=0V) will turn it off. The whole circuit can be powered from any DC source capable of delivering about 50 mA at 12V DC.


  • Prototype was tested with 1K resistor in place of R1. If you are using a different optocoupler, try to alter this value as per the requirement
  • Typical current transfer ratio (CTR) of CNY70-1 is 40 % to 80 %
  • The 12V/320Ω SPDT relay in the prototype draws about 38mA
  • The power diode D1 (1N4007, 1N4001 or similar) connected across the relay coil, protects the transistor T1 from damage due to the B-EMF pulse generated in the relay coil’s inductance when T1 (BC547) turns off
  • C1 (100µF) is a bypass capacitor to absorb the current transients when the relay turns on and off. This will ensure more reliable operation, and help prevent interference with the operation of the control circuitry


Join the conversation!

Error! Please fill all fields.
  • Sankalp

    Sir i need the necessary coding for AT89C51 micro controller for implementing for the project” synchronization of grids on sensing voltage or frequency beyond range”. Here is the circuit for the same
    The frequency range 48-52 Hz above or below it depicts bad frequency.
    Same is the case for voltage as well.

  • Praktis S

    i have looking for this for few weeks ago. found that my circuit not works as i expect -_-
    then i found your article, i try it then its work very well….
    thanks for this…
    sorry for bad english, coz i’m indonesian

  • Gregg W

    I think the capacitor across the coil + to ground is a nice touch; I didn’t consider it until I saw this post.

  • Gregg W

    There are a number of nice little circuit boards with optocouples and relays already mounted available on eBay. I chose one with dual input LEDs that will operate on either a positive or grounded input signal depending on how you connect them. If you want to isolate the input LED from the relay coil. use the center pin of the jumper connection and the signal input. Unfortunately they are not available in two different voltages for signal and coil, but a clever person could change the surface mount resistor for the opto isolator.

  • thirumoorthy

    hello sir i am control by 3 phase motor(DOL) forward and reveres, microcontroller 89s52 to interface 5v relay(1&2). microcontroller p3.0(forward relay(1)) and p3.1(reverse relay(2)) output to take and using optocoupler pc817 to connect 5v relay. 5v relay to control 230v in 3 phase contact coil(1&2). it works good . i have many time to press forward and reveres condition .microcontroller to stop and it have not working.after press reset . it will work . i have power diode (1N4007) connected across the relay coil.i have remove the 230v in 5v relay it’s work good .5v relay on/off good but to connect 230v contact coil and press for/rev microcontroller to stop. forward/reveres contact on after some times press fastly forward and reveres microcontroller to stop.

    • Jim Keith

      Regarding your schematic, there are a few improvements to make.
      Microcontrollers are poor at sourcing LED current –they do much better sinking LED current –so tie the LED anode to +5V –this requires the P3 outputs to be complemented.

      On the relay driver, you do show the clamp diode across the phototransistor –it should go across the relay coil.

      The opto coupler may not have sufficient CTR (current transfer ratio to drive the relay coil). Instead, use it to turn on a driver transistor that has lots of gain like the 2N4401.

      The relay contacts need to be rated for 230VAC pilot duty –this could be a problem. You may need a larger pilot relay for the job. AC contactor coils tend to be nasty because of the inductance –place a RC quencharc across the coil.

      Your assembly code may have an error that causes it to get lost in space –make sure that you use the proper return instruction from interrupts and sub routines to prevent stack overflow. Also, set your stack pointer much higher than the default –that is a common issue with the 8051 series.

      Here is a screenshot of initialization code that may be helpful:

  • Tariq996

    Mr. T.K. Haree, can I use 4N25 opto-coupler instead of CNY17-1

  • T.K.Hareendran

    Interested readers can download the pdf datasheet of CNY17-1 from here

Looking for the latest from TI?