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
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
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.