wiring set up of relay

I am having a slight problem with the above circuit. I am driving the "set" side of a latching relay with either the input of pin D5 from an ESP8266-12E or from a data pin from a HT12D attached to a RF receiver.
When I activate the D5 pin using Blynk the Transistor (2N222) "sets" the relay turning off that ESP and turning on another ESP which does its thing and then activates its D5 pin "resetting" the relay turning it off and turning back on the original ESP. This works great every time. However when I "set" the relay using the HT12D sometimes the ESP "resets" the relay and sometimes it doesn't and gets stuck continuously looping through its code. The strange thing is that usually it works fine when the transmitter and receiver are in close range of each other (5 yards) but at longer distances (15 yards) the ESP does rarely "resets" the relay. I have tried replacing R2 with a 100K resistor and a 4.7K resistor. Neither helped, in fact with the 100K it never "reset" even with the Blynk ESP.
Should I try using a different R1 resistor for each input before D1 and D2 or is there a value for R2 that would work as it is set up now.
I don't think it is the code as it does resetting. Does anyone have any suggestions? I am not an electrical engineer, just putting thing together from what I can learn from places like this. So any and all help would be appreciated as well as an explanation (keep it simple for me please) Thanks. circuit with second 2n2222 would this be how I would wire in the second 2n2222 for the ESP 8266-12E. This shouldn't cause any problems?

  • \$\begingroup\$ Ah, I think it is because when you switch off the latch relay A, there is created a "back EMF" which causes a "fly back current" which is absorbed by the flyback diode D1. But often not all the flyback current is absorbed by the flyback diode D1, and some flyback current might leak through Q1 (2N2222) or the ground path, causing spikes triggering things again. One workaround is to use "optical isolation": Use a "optocoupler" placed before R1. Now the original signal input first goes to the optocoupler, then goes to drive 2N2222. / to continue, ... \$\endgroup\$
    – tlfong01
    Aug 31, 2020 at 1:23
  • \$\begingroup\$ Now because of the optical isolation, the leakage current cannot go back to the original signal source (ESP8266/HD12D) so easily causing unexpected triggering. I know what I am saying is a bit abstract, so to make things not so abstract, you might like to see some example schematics of use of optocouplers used in many popular cheap relays. My answer to the following question has those schematics: electronics.stackexchange.com/questions/505318/…. Cheers. \$\endgroup\$
    – tlfong01
    Aug 31, 2020 at 1:31
  • 1
    \$\begingroup\$ I confess I don't really understand the big picture of your design but could it be the HT12D's output itself is suspect when things are far apart? Can you confirm you're getting a consistent trigger at D3 in that case? The transistor circuit looks fine to me - I don't think your problem is with its resistor values. \$\endgroup\$
    – td127
    Aug 31, 2020 at 3:13
  • \$\begingroup\$ I''ll admit I'm a little daunted by the use of an optocoupler. When the transmitter and receiver are far apart the signal still get received because the relay is activated as well as another relay. What I don't understand is why would the output of the HT12D be less when they are far apart and why that would effect the second ESP's ability to trip the relay when it does it when the Blynk pin activates the relay the first time as well as when the RF is close together. This makes no sense to me but I'm not that knowledgeable about this. \$\endgroup\$ Sep 1, 2020 at 20:50
  • \$\begingroup\$ but why would the HT12D's output be different when things are far apart. I thought when it received a signal it just put out a pulse that it had done so. What optocoupler would you suggest and how is it hooked up? \$\endgroup\$ Sep 2, 2020 at 4:38

2 Answers 2


R1 is a bit on the high side, you're only getting about 1.5mA base drive with the full 3.3V in and the coil draws almost 50mA. I would reduce it to 680 ohms, but I don't think that's your problem.

It looks like you're trying to drive the reset input directly with the ESP output pin, which is bad in a couple of ways. Suggest you duplicate the BJT drive circuit sans diodes but with the flyback diode, transistor and base resistor.

  • \$\begingroup\$ I have posted a circuit with this in mind. Is this what you mean. Is there any downsides to this? \$\endgroup\$ Sep 1, 2020 at 13:49
  • \$\begingroup\$ Looks good. As I said I would reduce R1. There are no downsides to this configuration. \$\endgroup\$ Sep 1, 2020 at 14:44
  • \$\begingroup\$ So I reduced the R1 to 680 and tried this configuration. I did not however include the 10k resistor between emitter and base on the second 2N2222. It didn't work as the circuit became locked in a loop with the first (Blynk) ESP constantly running and the second ESP never running. Could the elimination of the 10K resistor cause this? \$\endgroup\$ Sep 1, 2020 at 20:11
  • \$\begingroup\$ I don’t think so. \$\endgroup\$ Sep 1, 2020 at 23:54
  • \$\begingroup\$ Should the diode D1 and its companion be an 1N4148 or 1N4007? Have 1N4148 now \$\endgroup\$ Sep 2, 2020 at 2:28

So first off I would like to thank everyone for their great suggestions. I did not try them all but the ones I did try should have worked but had other glitches. I decided to change the pin coming off the HT12D. I was using a data pin D9 (D8-D11) which apparently stays HIGH until the HT12D is activated again. For some reason when in close proximity it stops pulls the pin LOW but far apart it remains HIGH so it keeps tripping the relay again and again. By using the VT (valid transmission) pin on the HT12D it goes HIGH on a valid transmission but goes LOW when the transmission stops, which it does after about a second. So the pin goes HIGH tripping the relay but then returns to LOW allowing the relay to be reset. Now it works all the time correctly. Thanks again for all everyones help.


Your Answer

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

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