I designed a simple circuit to control a relay with a NodeMCU micro controller. The NodeMCU polls a remote web service via a simple API and pulls high or low the D1 GPIO according to what it's posted online. The relay is triggered accordingly.

I used a 2N7000 MOSFET and a pull-down resistor (78K) to keep the GPIO low if floating. I'm not sure if I also need the other 4.7K resistor to limit the current through the base-source path of the MOSFET, I couldn't risk frying the NodeMCU so I added it.

Here my initial schematic:

enter image description here

The system works as expected. Unfortunately the NodeMCU drives the GPIOs high for a very short period of time during boot (I measured 50-80ms or so). I don't have control of this behavior because it is before my firmware starts running. The short pulse is enough to trigger the relay and I'd want to avoid that.

I tried adding a small capacitor in parallel with the pull-down resistor. This way I solved the issue as the capacitor filters out the short spike and then discharges quickly if power is lost.

Here's the fixed schematic:

enter image description here

It works very well but I don't know if that's the correct way of doing things. Is it ok to do so? Does it stress the GPIO in any way? Also, do I risk letting the spike through if I quickly power cycle the NodeCPU? (I mean, does the capacitor discharge quickly enough?)

  • \$\begingroup\$ Depends on where you put the capacitor. Best is after the 4.7Kohm as the resistor limits the current. It also gives better control over the timing as the RC time can be calculated. OK, you added the 2nd schematic after I posted. That is fine. Your RC will be determined mostly by the 4.7K and 1uF (Provided you drive high/low not tri-state.) \$\endgroup\$ – Oldfart Apr 13 '19 at 8:12
  • \$\begingroup\$ @Oldfart I added the second picture, please have a look \$\endgroup\$ – Gianluca Ghettini Apr 13 '19 at 8:17
  • \$\begingroup\$ I did, and updated my remark. That circuit is fine. With a GPIO of 5V the current peak will be max 5/4700 = ~1mA. The RC time (90%) is about 0.01 second. \$\endgroup\$ – Oldfart Apr 13 '19 at 8:35
  • \$\begingroup\$ @Oldfart ok thanks. Actually the GPIO is 3.3V but I guess the RC time will be the same \$\endgroup\$ – Gianluca Ghettini Apr 13 '19 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.