I am a newbie. Need help on my project. I am building a project which controls a 5V relay from ESP8266 ESP-01. Since the GPIO2 gives the output of 3.3V, I am using an N channel MOSFET 2N7000 to control the 5V relay. The circuit is shown below: ESP8266 schematic

However once everything is connected and I switch on, the ESP-01 Blue LED glows continuously, and the LED after R1 continues to glow and I get garbage On serial port from ESP-01. The standalone ESP works absolutely fine. Kindly help how to solve this issue. Thanks in advance

  • 1
    \$\begingroup\$ To fix this, can you replace PNP with NPN and emitter to ground. Then Replace R3 with a reverse diode across coil such as the 1N400x types to shunt turn off spike +ve \$\endgroup\$ Feb 8, 2019 at 7:45
  • \$\begingroup\$ what is the purpose of R3 in your circuit? \$\endgroup\$
    – jsotola
    Feb 8, 2019 at 8:02
  • \$\begingroup\$ A 2N7000 will work, but my guess is that you've connected it the wrong way. Pin 1 connects to GND, Pin 2 connects to R2 and Pin 3 connects to the relay coil. Also, you should replace R3 with a 1N4001 diode. \$\endgroup\$
    – Steve G
    Feb 8, 2019 at 9:18
  • \$\begingroup\$ In addition to mixing up FETs and BJT's your problem is that the 10K pulldown resistor in your circuit is putting the GPIO2 in a mode incompatible with the way you want the ESP8266 to boot. \$\endgroup\$ Feb 9, 2019 at 5:07

2 Answers 2


Turn around transistor. To swap outer pins.

The 2N7000 is not a PNP as shown in the schematic but is the Nch FET which also has a inward arrow like a PNP but polarity so the arrow is Gnd and you have it reversed.

The gate is active high impedance with 60pF input with inverting output Drain switched to gnd.

This voltage controlled switch which will drop < 0.5V with 7 ohm switch with Vgs>4.5V This is because Relay has a 70 Ohm coil, so 9% of the voltage is lost, but still sufficient to drive the coil.

All DC coils and open switched inductance needs a low resistance reversed diode across the drain-source switch to turn off the current slower and not raise the voltage rapidly trying to sustain the voltage.

V=LdI/dt where dt is very small >10ns with 25 pF Coss and “dI” is from 70mA to 0V turning off this coil, V=L 70mA/10ns or 7kV per uH.

  • R3 =1k * 70mA limits the voltage to 70V, is no good
  • always use a diode with low resistance or rated for more DC current than being used
  • such as a reverse 1N4001~...5 diode with a few Ohms when activated turning off the FET clamps the voltage to 0.6+ 70mA * 2~3ohms=0.8V max above Vcc=5V .

    A 1N4148 might be 1.2V above Vcc and switch faster causing more EMI due to 10x higher resistance or lower current rating.

dt is increased with a lower resistance shunt diode. Thus it slows down from 10ns to about 100us Since decay time T=L/R which depends on coil inductance of several hundred uH or so or roughly 300uH/3 Ohms= 100us.

This current decay time or coil -ve rise time translates into a noise spectrum.

Where \$dt=0.35/f\$ for an impulse noise continuous spectrum up to 0.3 MHz.

This can inductively couple radiated noise into loose Rx channel wires or close tracks. It can also conduct noise share by the data ground = 0V line.

Data Garbage

  • conducted noise needs a cap between coil Vcc and FET 0V as close as possible to switch.

    • radiated noise lines should be paired or low impedance tracks or twisted pairs and same for data lines , preferably not bundled together and at right angles to coil current path.
  • a supply cap nearby will help attenuate this garbage also, along with diode and twisted pairs as long as not too big.

    • C> Ic * dt/dV=70mA * 100us /5V = 35uF. But if close to supply it may not need to be so big.
  • You can also get conducted noise garbage on the ground lines if shared by the path of coil current rather than separate gnd and Vcc to supply.

  • the Vcc loop to coil path area creates a B field as a loop antenna of this switched inductance, so keep the area between the paired power wires small.

Hopefully your LED still works after this as -5V =Vr max would be exceeded without the diode from the LED cathode rising 70V above the Vcc with Rs in series.

  • \$\begingroup\$ Thanks for your feedback. Sorry for the mistake in schematic. In reality , I am using Nch Mosfet where the source is grounded and the drain driving the relay. I had used the same schematic for another project where the motion sensor was giving output to the Mosfet and it worked fine. Sorry Didn't realise that it does not work the same for ESP. I will change the R3 to a diode and let you know. Thanks a lot! \$\endgroup\$
    – Eagle64
    Feb 9, 2019 at 11:13
  • \$\begingroup\$ If your ESP output is by default an input on power up and worse yet configured with a 10k pullup internally, then your LED will glow . \$\endgroup\$ Feb 9, 2019 at 23:54
  • \$\begingroup\$ By Pulling up the GPIO2, I am able to achieve the AP mode. However the relay control sketch does not work. What changes should i do? \$\endgroup\$
    – Eagle64
    Feb 12, 2019 at 11:43
  • \$\begingroup\$ Pulling up the input turns on the Relay. So Don’t do that \$\endgroup\$ Feb 12, 2019 at 13:02

Your schematic symbol is for a PNP transistor but the part number is that of a MOSFET.

Assuming you actually have a 2N7000, the source should be grounded and the gate connected to the 10K/120 resistors and the drain to the relay.

Also, there is a subtle issue with the LED vs R3 value- but I don’t think that’s your immediate problem. R3, as opposed to the usual fly back diode, allows the relay to switch off rapidly and is okay provided the nominal coil current of the relay is less than about 50mA, however it will cause a reverse bias spike on the LED which can eventually damage it (relay coil current x R3). You could eliminate R3 and put a 1N4148 across the LED (inverse parallel) which would switch a bit slower but would be okay to higher relay current.

  • \$\begingroup\$ Thanks for the feedback. Will change the diode and let you know. Sorry for the mistake in schematic. I have used the Nch Mosfet and it had worked for another project for driving the 5v relay. However I didn't realise that this wouldn't work with ESP. \$\endgroup\$
    – Eagle64
    Feb 9, 2019 at 11:15
  • \$\begingroup\$ Of course it works with an Nch FET, if the GPIO is defined as an output, 0=off,1=On, but needs >4.5V to turn On and <2V to turn off, for the 2N7000. \$\endgroup\$ Feb 12, 2019 at 13:06

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.