My prototype would use active low output from 8-10 ESP8266-12E GPIO pins to control AC appliances. I have several options to interface the relay (or SCR phase control circuit) with the MCU. But I can't decide which one is best to use from long term performance and cost perspective. What I have thought of so far, is as below:

Using a suitable PNP transistor:


simulate this circuit – Schematic created using CircuitLab

Using an optocoupler


simulate this circuit

Using darlington transistor array such as ULN2803

I have around 8 to 10 GPIO control pins. If I see from cost perspective, discrete transistors solution seems cheaper than those wired around optocoupler or ULN2803. However, I am not sure if all the hand soldering 8-10 transistors are worth the effort (I am no good at it, I admit). Also, I don't know how the discrete transistor based solution will perform in the long run. Any advice will be appreciated.

  • \$\begingroup\$ Why specifically active low? Active high would make driving the relay easier. Just an NPN as a low side switch and a base resistor - and don't forget the diode for the relay. \$\endgroup\$
    – JRE
    Dec 30, 2016 at 13:52
  • \$\begingroup\$ @JRE- I did some ad-hoc tests and found that while sourcing current from GPIO, there was a power shortage for the onboard radio transceiver. This caused some serious problems while connecting to my home Wi-Fi. On the other hand, sinking current worked like charm. \$\endgroup\$ Dec 30, 2016 at 13:59

1 Answer 1


If the microcontroller and relay run from the same supply voltage, then your top option using a PNP transistor is probably the easiest. It's just like the typical NPN low side drive, just flipped around.

The optocoupler circuit you show is not good. Most optos don't have the output current capability to drive a relay, especially relays with low voltage coils that can be run from the same supply as the microcontroller. Take a look at the CTR (current transfer ratio) of the opto.

However, these digital signals are coming from a microcontroller that can drive them with arbitrary polarity. Unless these are very special open drain outputs or the like, simply invert the logic in the firmware. Now you have active high signals, which can drive a low side NPN switch directly. That also allows for powering the relay from a higher supply voltage, which is a good idea when possible. I go into more detail on that here.

For completeness, here is a way to drive a relay from a active low digital output, but where the relay coil requires higher voltage than the digital logic:

Again, it is only worth doing this if the digital signal can't be inverted to be high when the relay is supposed to be on, or you are stuck with a open drain output. This is very rarely the case when the signal is coming straight out of a microcontroller.

Q1 is in the common base configuration. It acts as a switchable current sink, to voltages higher than the 3.3 V supply. When the digital output is asserted, the bottom end of R1 is held at ground. Figure about 700 mV for the B-E drop, so that leaves 2.6 V across R1. (2.6 V)/(2.4 kΩ) = 1.1 mA. Due to the gain of the transistor, most of that will come from the collector, not the base.

That provides about 1 mA base current for Q2. The relay I used as example draws 27 mA at 12 V, so this circuit requires Q2 to have a gain of at least 27. That particular transistor has a minimum guaranteed gain of 100 at both 10 and 100 mA, so there is a comfortable margin.

D1 is not optional. It gives the kickback current from the coil a place to go that doesn't require creating a high voltage and blowing out Q2.

  • \$\begingroup\$ Thank you so much. This is clever. I decided to go for active low control because, when I connected around 5-6 LEDs (with 220 ohm series resistor), I saw a significant performance drop in the radio connectivity. Then I read the ESP documentation and found the pins have open drain output. Programmatically, I can enable the internal pull-up resistors wired to the GPIOs and give ENABLE<bar> signal (active low) to control the appliance. However, I'll run the tests again by flipping the GPIOs in my code. Just to rule out if my router is to be blamed for intermittent wifi connectivity. \$\endgroup\$ Dec 30, 2016 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.