# NodeMCU control 12v solenoid

I am trying to control a 12v solenoid from a NodeMCU. I bought the wrong MOSFET, I've got an IRF520 (the website I bought it from said the gate threshold voltage was 3.3v, so I thought it would work, but it appears to require much higher voltages than a GPIO can provide in order turn on).

I'm wondering whether I can make a voltage divider with a couple of resistors to get the gate to the required voltage. This is what I am thinking:

I'm not entirely sure how to work out the resistor values. I've read that a NodeMCU can sink 20mA. So the total resistance needs to be 600Ω in total.

The way I've calculate it, if it is 300Ω each, then the gate voltage varies between 6v and 4.35v as the GPIO goes between 3.5v and 0v.

Is this correct? Will it work?

• as an alternative, you can put a transistor between the mosfet and the microcontroller pin and drive the mosfet from a higher voltage potential. That makes the transistor a simple gate driver. – tedder42 Aug 4 '18 at 19:24

I'm wondering whether I can make a voltage divider with a couple of resistors to get the gate to the required voltage.

You cannot. A voltage divider can only reduce voltage, while you need to increase it.

Also note that using what looks suspiciously like a 9v transistor radio battery to power a "solenoid" is going to be a mistake.

Buy a proper FET with a threshold voltage less than half of what you can apply and data sheet performance indicating low loss in your actual gate voltage, and temperature conditions.

If you really wanted to make a higher threshold FET work, you'd need to build an FET driver. To do this right would require two active devices - one high side switch to drive the gate high, and a low side switch to control it from your MCU.

If you try to "cheat" and use a pullup resistor defeated by a low side switch, you'll need to balance the conductance of the pullup resistor to turn the gate on quickly, vs. the current wasted in driving the gate low against it.

• Further, Elliot makes a good point that what you've drawn is not only unworkable, but dangerous to the MCU. – Chris Stratton Aug 4 '18 at 19:52
• Some of the discussion I've seen online says that the NodeMCU has a built in voltage regulator and can handle 20v – user1379351 Aug 4 '18 at 20:01
• You can actually use a passive resistor Diode network to get the drive you need. You don't need to put a transistor or FET in front or select ultra low threshold FETs. – Jack Creasey Aug 5 '18 at 0:54

No, you shouldn't connect a 9V source to an output pin of the MCU when the MCU is running at 3V. You will forward bias the input protection diode on the MCU. It might work, for a while, but it's really not a good idea.

Also, changing the gate voltage from 4.35V to 6V will not give good on/off control of the solenoid.

• Ok so would it be ok to use a separate DC-DC step-down to power the NodeMCU? – user1379351 Aug 4 '18 at 19:57
• No, the issue is that you are connecting a 9V supply to the NodeMCU output pin. Think about how current will flow...it will flow into the output pin even if the output value is '1'. You shouldn't do that, even through resistors. In this case you might get away with it because the current through the resistors will be low but it is not recommended. – Elliot Alderson Aug 4 '18 at 20:25

While many may propose that you drive the FET gate from 0 V to some large value, you actually need to. It is possible to use a passive network to drive the gate.

The datasheet for the IRF520 shows the gate threshold as 2 - 4 V range across all devices.

Depending on the device you bought it may work or not at 3.3V gate drive voltage. For example if the device you bought is closer to 4 V VGS(th) then it won't turn on at all with 3.3 V drive.
To ensure that you drive the gate of the least capable device (the highest Vg) you need to ensure that you can define the off state and the on state voltage.

It is clear that ALL devices will be OFF when the gate voltage is below 2 V and ALL devices will be ON or at the threshold (Id => 250 uA) at 4 V.

If you look at the general (typical) characteristics you can see roughly what Id current is possible at a given Vg.

From this graph if you could select an ON gate drive voltage of 4.5 V or more this would ensure that all devices would support an Id of at around .5 A or more.

For simplification lets set the VGS(ON) at 4.7 V and use a resistive and diode divider to drive the FET:

simulate this circuit – Schematic created using CircuitLab

This gives an ON drive of about 4.7 and an OFF drive of about 1.4 volts, which would cope with the potential spread of VGS(th) for the all this device type.

The correct answer is: just use a bipolar transistor.

Anyway, you can do what you want, but don't use a voltage divider (it will reduce the off-on swing at the gate).

It is a bit of a juggling act, but can be a useful trick in the right circumstances.

The right circumstances are:

• The current is high, so a power fet is a better choice that a bipolar or darlington
• there is a useful combination of VGS-OFF and VGS-OnEnoughForYourLoad for the fet you have chosen, across part variation and temperature.
• The fet will turn on while the MCU is resetting
• there is always current drain from V+ through R1

simulate this circuit – Schematic created using CircuitLab

You have to first find what the maximum OFF voltage is, for all parts, at all temperatures.

Add this voltage to VDDmin and see if it is high enough to turn the fet on.

Now choose how to do it:

Using the output as open drain results in the output pulling up above VDD by D2 drop - 0.8V. (for cmos outputs, not 5V tolerant outputs).

• 0x D1 Off=0 On=VDD+0.8
• 1x D1 off=0.7, On=VDD+2.2
• 2x D1 off=1.4, On=VDD+2.1
• 1x GreenLED Off=1.9 On=VDD+2.7 =6V

So in the last case, a typical power fet will be off at VGS<2, and able to drive a couple of amps saturated at VGS=6;

Beware of putting the MCU into deep sleep, and having R1 (slowly) pull VDD up to V+

Did I mention: just use a bipolar?