Simple DC Solenoid Valve Circuit, How to size components?

I'm trying to build a circuit controlled by a 3.3V GPIO pin on a raspberry pi to open / close a 12VDC Solenoid Valve. Part of me feels I'm in over my head and I should back away slowly, but hopefully I'm not too far off. I know there's a deficiency in my understanding of how its supposed to go together, but I believe my ultimate circuit will be something like this:

First Question: Is this diagram correct? Assuming each component here is sized correctly, setting the incoming GPIO pin to high will cause the valve to open and setting it to low will cause the valve to close (all without destroying my precious Pi and/or MOSFET)?

Second Question: How do I determine what voltage / resistance each component should have?

I've included values for everything I know; the control signal from the GPIO pin is 3.3V, my Battery and Solenoid Valve are both 12V. From here, I read that the maximum amount of current I should attempt to pass through a GPIO on the Pi is ~16mA. According to this, opening the solenoid valve with 12V will take 320mA. How do I determine all of the other values that I need?

I'm tempted to say that `D` should be a 12V diode, but only because everything else on the circuit is 12V. I realize it's there to help protect the MOSFET from back EMF for when current to the valve is cut, but going to Radio Shack and digging through the drawer of diodes didn't help me understand what rating `D` should have. As far as I know there was only a single voltage rating on the diodes I looked at.

After reading this, I'm also tempted to believe that `M` should be rated higher than 12V, but I don't really understand why. I also don't understand why that particular MOSFET the OP chose was a poor choice for a microcontroller.

I tried reading this, but I feel like I would need a degree in electrical engineering to understand it. I also went through this article, which led me to add in `R1` and `R2`. From there, I can see that `R1` should have enough resistance to counter the parasitic capacitance of the MOSFET, but how much do I need and what does it depend on? Same for `R2`.

Edit

I added R2 after reading this article. It states: "If you don't hook up R2, Q1's gate is floating when the microcontroller is in reset. If you add R2, you ensure the gate is pulled low and the transistor Q1 is off." I understand that, but I don't understand whether R2 should be 10-50 Ohms or 1kOhm and why.

• Hello and welcome! Why not ask at electronics.SE? You'll get better answers faster there :) For starters, your diode "D" is shorted by a wire. it should be parallel to the solenoid. And I am not sure R2 is really needed (but than again I do not have a degree in electrical engineering ;) )
– Ghanima
Jan 12, 2015 at 20:55
• :) Thanks Ghanima. I asked here mostly because this is specifically for a raspberry pi. I thought to go to Electronics.SE, but I hadn't yet asked a question here yet Jan 12, 2015 at 20:58
• If R2 is just to prevent floating it should be pretty high. It will probably higher than R1, most likely in the high kohms range if not even Mohm. The transistor should be rated well above 12 V (but that is not really an issue) and more importantly according to the current rating of the solenoid (asume a nice safety margin). Power rating of the transistor should be selected according to the voltage drop (refer to data sheet) and again the current of the solenoid as well as expected ambient temperatures. But it does not seem to be critical.
– Ghanima
Jan 12, 2015 at 21:28

R2 should be ~100kΩ R1 is not strictly necessary with a MOSFET, but does not harm, and provides some protection if you make a bad connection - use ~1kΩ.

This must be a pretty hefty solenoid if it needs 320mA @ 12V. Don't worry about the voltage ratings of the diode or MOSFET ~50V will be OK, but you will definitely need a power device.

Build your circuit (without connecting to the Pi) and check it works by connecting the input to 3.3V. You are unlikely to damage anything (provided you use a MOSFET with a high enough current ratting).

• Thanks for the advice Milliways, it is much appreciated. The only MOSFET transistor in the drawer at the local store was a 60V one which I just went ahead and picked up thinking it was way overboard, now I'm glad I did. Jan 13, 2015 at 15:01
• I initially marked this as accepted but then took it back. I'll mark it again if you can update with any general guidelines on how to come up with the design considerations when looking for the MOSFET specifically, but I'd also appreciate any info on how you came up with the values for the resistors. What would change if I upgraded to a 24V battery with a 24V solenoid valve? Jan 15, 2015 at 15:52
• @watcher 45 years experience as an Electrical Engineer. Frankly most component values are not critical - the secret, of course, is to know the difference. R2 could be any value from 10kΩ to 1MΩ, R1 is based on maximum current flow from the GPIO. The MOSFET needs to be able to carry the current, and have a voltage 2*supply voltage. The most critical is the gate voltage needed to turn it on. Using 24V doesn't affect the Pi side, only the MOSFET current/voltage. Jan 15, 2015 at 22:43
• Ok thanks for the update, I'll go ahead and mark as accepted. I ask because I only have 2mos experience as an electrical hobbyist :) Jan 15, 2015 at 23:10

@Milliways has some great pointers. You appear to have a diode in there, that should go over the FET's source and drain to cope with any nasty currents the solenoid will create when activeted/deactivated by the FET (without the diode the FET will die eventually).

I use IRF540's to do this kind of stuff. Big, heavy, very low resistance when closed meaning no heat even when you run severals amps through. I use them to control/PWM power leds, 12v halogen lights etc. The IRF540's have this diode built in, so one less component to mount :)

With that, the IRF540 becomes almost a clean cut solid state relais; you can leave out R1, You need R2 if the GPIO pin won't ground hard but go Open Collector. I use this with Atmel AVR processors that can control their IO pins as push pull (hard 0 or hard 1) and I have them drive the FET directly without any resistors involved.

• The purpose of the diode built into MOSFETS is quite different to the flywheel diode. The flywheel diode provides a path for the current generated by the collapsing magnetic field, which would otherwise generate very high voltages. It should be across the solenoid. Jan 13, 2015 at 22:14
• You're right @Milliways. In fact I'd want diodes across both solenoid AND fet... Jan 14, 2015 at 7:04