With the parts in the mail, haven't had the time to test, but the simple question: will this work, or what improvements are needed?

What it's supposed to do: a MOSFET switch for 12V, 0.5-3A loads, driven by a 3.3V logic level (which turned out to be too low even for my logic level MOSFETs). The chip starts up with the ports high, MOSFET off. Setting the pin low switches it on.

enter image description here

Then the background.

I'm using a 3.3V logic level NodeMCU board for my project. For this I got myself a handful of IRL540N MOSFETs (after reading forums, and the things are cheap). From the spec sheet I had the idea it would work at 3.3V but no luck there. At 5-6V gate level it opens up fine (I hooked up 21W worth of 12V LEDs to test, worked like a charm, the IRL540N didn't even get warm). But at 3.3V no luck, the LEDs barely lit up.

Using a driver transistor worked, but then it became inverted: port low was the on, so effectively default on, and that's not safe for my project. It has to be default off, so at startup (pins low) it's simply off.

So then I realised I had to use higher voltage, and this is where the optocoupler comes in play. It allows me to pull the gate of the MOSFET to the 12V level using just 3.3V.

The below schematic I devised for the combination with a PCF8574 port expander. Its pins should default to output, high. In high state it can source only 0.3 mA or so, very little, enough to drive a transistor but not an LED. It however can sink at least 20 mA, so that's good enough to drive an LED, or an optocoupler.

The idea behind this schematic: when switched on, the ports are high, no current through the LED, MOSFET gate pulled low, and it's off.

Then when I make the port low, it sinks the current for the LED in the optocoupler, the gate is pulled high, and the MOSFET switches on. The 12V ensures it's saturated.

Load may be lamps or a motor or even a solenoid, D1 is there to prevent the circuit from frying upon turning off.

  • \$\begingroup\$ Intrinsically the circuit looks sound. Though I have not checked the component values. \$\endgroup\$
    – Trevor_G
    Commented Mar 25, 2017 at 17:59

2 Answers 2


Everything seems to be OK, Just some comments:

  1. The forward current through the input diode of the opto it's a bit too low (6-7 mA). A value of 150 Ohm for R1 would allow an \$I_F\$ between 10 and 15 mA, still within the 20 mA sink limit of your PCF8574. This will allow you higher collector currents (heavier loads) in the output transistor of the opto, and higher peak currents during charging of the MOSFET, without taking the transistor out of saturation (look at the table below).


  1. The load on the output transistor of the opto has too much impedance for fast switching in case you want to do PWM. At 10 kOhm your rising and falling times will be skyrocketing, thus severely limiting your duty cycle operating range:

Response times

  1. There is also another timing issue, which Jack Creasey has correctly pointed out: the collector current may not be high enough to ensure fast charging of the MOSFET gate. To overcome this, you might need an additional transistor stage between the opto and the MOSFET to boost current. But note that this is in addition to the opto needing a lower R2 so not to become the bottleneck itself.

  2. Add a decoupling capacitor at the collector of the output transistor of the opto. If your 12V supply is connected through long wires (= parasitic inductance), switching off the MOSFET may generate an enormous voltage spike in the 12V rail if the current delivered to the load is high. Your flyback diode protects the MOSFET, but not the rail. Take a look at this question.

Source of the figures: PC817 datasheet.

  • 1
    \$\begingroup\$ Comment #4 is a very good one, considering this is just a trial and will expand to more switches on the same microcontroller. Definitely going to add those capacitors, they're a cheap insurance. And I'll change the resistor value, it's just that I have a bag of 330R ones at hand. I'll probably just use two of those in parallel. \$\endgroup\$
    – Wouter
    Commented Mar 26, 2017 at 11:57
  • 1
    \$\begingroup\$ I've built the schematic by now, switching 5V and 12V loads, and want to add that it works exactly as intended. Only modification with the original schematic: I lowered the value of the series resistors of the optocouplers to 165R (2x 330R in parallel), haven't tried with the originally design with 330R series resistor. No decoupling capacitors as suggested in #4 for my prototype, using short lines only. \$\endgroup\$
    – Wouter
    Commented Jun 7, 2017 at 12:55

There is nothing logically wrong with your schematic, but there are a couple of points you need to think about.

Your biggest potential problem is that the opto-coupler acts like a constant current source. As you turn it on it will always be current limited by the gate capacitance charging. In addition the Opto has to provide current through the 10k resistor, which to hold your device on (VGS > 2.5 V @10 A) requires 0.25 mA. To get your Gate to somewhere near 10 volts this load current increases to around 1 mA.

You have approximately 6 mA through the LED on your PC817, therefore likely 3 mA current limit on the output (worst case). The IRL540N will have approximately 2000 pF of input capacitance. This will result in a slow turn on/off of the FET (I'd expect worst case in the range of 1 - 5 uS).
You will also have very large turn off delays ( >15 uS) if the Gate voltage does get up to 10 V or above. The turn off time is soft too since it's defined by the 10 k * 2000 pF discharge rate.
Neither the turn on/off or delay times may be a problem at your envisaged output current levels, you just need to be aware of the issues particularly if you are ever intending to use PWM control (eg PCA9685).

  • \$\begingroup\$ Thanks, very helpful. Timing doesn't seem to be a problem now (on/off in the tune of seconds to minutes if not hours), but will definitely keep it in mind for possible future projects. \$\endgroup\$
    – Wouter
    Commented Mar 26, 2017 at 11:59
  • \$\begingroup\$ Spec sheet indeed makes me believe 2.5V is enough to open it, but even with a <2A load and 3.3V it didn't work for me. \$\endgroup\$
    – Wouter
    Commented Mar 26, 2017 at 12:00

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.