2

I'm a complete beginner attempting to build a basic circuit using a MOSFET to trigger a 24V push-pull solenoid from a Raspberry Pi's 3v3 GPIO pin.

I had someone else outline a circuit that I could use using the 3v3 GPIO output to toggle the MOSFET.

This is the circuit I'm currently using that works using the following parts:

Raspberry Pi Mosfet Circuit

The Problems I'm Facing

  1. When the Raspberry Pi is turned completely off, the 24V solenoid gets switched ON causing it to heat up (not ideal that it would stay on). I'm assuming this is because there's no current coming from the GPIO pinout to pull the MOSFET gate to off. Is there a way to prevent this? Ideally when Raspberry Pi 3.3v is ON, then the solenoid gets triggered.

  2. When the Raspberry Pi is on and outputting the 3.3v from the GPIO, then the NPN transistor heats up a lot. To keep the solenoid off with Mike's circuit, I need the GPIO set to HIGH, which causes the 12v current to run through the NPN transistor, heating it up a lot.

So my main question, is there a way to build a circuit where the solenoid is OFF by default, and only triggered ON quickly when a 3.3v GPIO signal is detected?

Also, is using a MOSFET an ideal solution for using the GPIO on the Raspberry Pi, or is there a simpler way I could be turning my 24v Solenoid on and off using Raspberry Pi? As a beginner, I can't seem to find a simple, straight-forward answer to toggling a solenoid using 3v3 GPIO pins!

1
  • To get a more complete answer you should specify what pins you are using and the current the solenoid uses. Don't expect people to wade through your links.
    – Milliways
    May 17, 2017 at 0:20

3 Answers 3

4

Here's an answer based on my basic understanding of MOSFETs and learnings building this circuit.

The MOSFET I'm using doesn't fully turn with 3.3V applied to the GATE. Even though the data sheet says it only needs 2V, it actually works best with 5V.

So instead, we can address the issue by introducing an NPN transistor to operate at the lower 3.3V, and leverage the 24V current to trigger the MOSFET gate instead.

Here's the final circuit that worked for me:
I went ahead and labeled all the major components to help other beginners out there :) Raspberry Pi 24V Mosfet Driver Circuit

I ended up using 2 Transistors, because I wanted the initial state of my circuit to be OFF (this is shown in the gif below)

To get a better idea of how this circuit works, you can play with it here. (Drag the GPIO Voltage slider on the right from on to off)

Raspberry Pi 24V Mosfet Driver Circuit

As you can see from above, the following is happening when 3.3V is switched between ON and OFF.

When 3.3V GPIO is OFF

  • Transistor (left) = OFF
  • Transistor (middle) = ON <-- thus pulling current away from the MOSFET gate
  • MOSFET (right) = OFF

When 3.3V GPIO is ON

  • Transistor (left) = ON
  • Transistor (middle) = OFF <-- thus allowing current to flow to the MOSFET gate
  • MOSFET (right) = ON

The diode used in the circuit is added to prevent a voltage spike from traveling the wrong direction into the circuit, thus frying the GPIO or other components. When the solenoid snaps back, it pushes current back through the wires, and the diode forces that current to loop back through the positive channel (since diodes only allow current to flow in one direction).

Many thanks to Mike at Adafruit for help with this circuit.

1

I can't imagine why you would want to use a transistor to drive a MOSFET.

Basically the circuit as printed is doing exactly what you would expect. Just use the MOSFET, assuming it is a device intended to run from 3.3V. See http://elinux.org/RPi_GPIO_Interface_Circuits

NOTE by default most Pi pins are inputs, some with pullup. This may cause a MOSFET to turn on. You should select one with pulldown. See http://www.panu.it/raspberry/

7
  • Applying 3.3V to the MOSFET doesn't work, I've tried that :). It works perfectly fine with 5V though. It has me pulling my hair out! Apparently, this is a common issue with the MOSFET because people on forums says it won't fully turn on unless it's 4.5V.
    – Axel
    May 17, 2017 at 17:11
  • @Axel You just need to use a suitable MOSFET. I regularly use MOSFET with my Pi.
    – Milliways
    May 17, 2017 at 23:48
  • Can you point out the exact MOSFET you use, please? It would be very helpful! Do you know if the MOSFET you use will support a 24V current running through it, being triggered on with only the 3.3V from Raspberry Pi?
    – Axel
    May 18, 2017 at 21:22
  • To add to your answer, the circuit uses a transistor because the transistor operates at a lower voltage requirement for it to turn on, whereas the 3.3V isn't enough for the MOSFET (it only opens it partially, causing it to heat up and behave erratic).
    – Axel
    May 31, 2017 at 15:33
  • @Axel: I tested a couple of different power MOSFETS. Most 1G devices prefer Vgs > 7V for big currents > 10A. But they usually still work for Vgs = 4V or 5V, for small currents, say < 1A, though the fet gets hot, because Rds(on) is big. For Vgs = 3V, some 1G devices still pass current, but only limited to small current, say 30mA. Modern companies often produce power MOSFETs logic level (3V/5V) triggered, for example 3G IRL540N (not 1G IRF540N!) which happily accepts Rpi's 3V GPIO signal and conducts > 2A (yes, I verified it.)
    – tlfong01
    Feb 28, 2019 at 5:21
1

The Mosfet has a Vgs of 2 volts, this means that the 3.3 volts should be able to turn it on. The Rds is very small, so the Mosfet should never get hot. The Solenoid may get hot depending on the coil resistance, we need to know the DC resistance rating. The NPN transistor should never get hot, the 10K should limit current to about 1.2mA. The gate current will be around 300 uA, just enough to turn it on ok if we assume minimum Beta of 50. Double check that the 10k resistors are really that value. A 1 Meg resistor from the Mosfet gate to to ground will keep the solenoid off even the the pins are inputs. The only thing the NPN is doing is inverting the control logic, you could probably eliminate it and just drive the Mosfet.

2
  • 3.3V doesn't turn it on, but simply using 5V does. I'm not sure why, but that's the case. I bought a pack of the same MOSFETs, and all of them behave this way.
    – Axel
    May 17, 2017 at 17:12
  • to @Alex: As I said above, fake devices do not meet spec, but I often buy those possibly rejected grade stuff because they are dirt cheap, and still useful for hobbyist prototyping projects.
    – tlfong01
    Mar 15, 2019 at 1:43

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.