# Help me calculate which transistor I need

I have virtually zero electronics knowledge, but I am a software engineer, so it's something I should be able to learn.

I'm playing around with a Raspberry Pi, and I want to use it to power a 12v solenoid. I can supply the 12v input from an external supply, and use the Pi to provide the Base input to a transistor, which I hope to use a switch to turn the solenoid on and off.

My issue is that I don't understand how to work out which transistor to use. I've narrowed my search to an NPN type, whereby if I programmatically supply a signal from a GPIO port, this will put 3.3v on the Base of the transistor. But I'm struggling to calculate which transistor I will need to produce 12v across the Collector/Emitter (ultimately delivering 12v to the solenoid). I'm not so much looking for the answer, although I'll accept that, but moreover how in laymans terms I can calculate it all.

I'm told I'll need resistors in the circuit also, so perhaps if someone can point in the direction of some grass roots guides of where to start? Thanks in advance for any help

The voltage applied between the base of the NPN transistor and its emitter controls the current flowing between collector and emitter. Transistors can be operated in a variety of modes, but in your case you simply want to use the transistor as a way to switch a big voltage using a small one (i.e. "saturated"). In this case, you just need to provide a voltage above about 0.7V between the transistor's base and emitter. This voltage is pretty standard for most BJT transistors (e.g. BC109). The 3.3V you plan to use is perfectly fine. Transistors have some upper voltage limit, but definitely much higher than 3.3V.

You also need to put a resistor in series with the 3.3V to limit the current flowing into the transistor's base. The current you allow to flow into the base sets the current allowed to flow between the collector and emitter, with some gain factor, typically 100 or more. So, if you know the current you want to flow through the relay, just divide this number by around 100 to get the current you need to supply to the base. Because the base should be at least 0.7V above ground, take your 3.3V, subtract 0.7V to get 2.6V (the voltage you should drop over the resistor), and divide this by the current you worked out that you need at the base to arrive at the resistor to choose. You may wish to provide more current to the base just to be safe, by choosing a slightly lower resistor. A lower resistor won't hurt, up until the current it allows to flow exceed's the transistor's absolute maximum base current rating.

The solenoid's current requirement will determine the resistor to use. Depending on the solenoid, you may find your Raspberry Pi cannot drive the current you need.

Since you're driving a reactive load (the solenoid stores energy in its magnetic field, which will discharge in the opposite direction once power is removed), you should put a large diode (e.g. 1N4148) in parallel to absorb the energy, otherwise this could hurt the transistor.

• Ok, so if I want 12v at the Solenoid, I need to provide 0.12v to the base? Is this in addition to the 0.7v above ground.. making a total of 0.82v? So I need to add a resistor in between the 3.3v pin and the transistor base, which reduces 3.3v down to 0.82v. Is that right? May 31, 2018 at 16:07
• @Detail: not quite. The 12V at the solenoid is fixed by the 12V external supply you give it. While the voltage you apply between the base and emitter must be more than 0.7V to fully switch the transistor on, the current is also important. The 3.3V from the Pi is fine, but you must set the current flowing into the base to (a) enough to allow the solenoid to operate (remember the transistor's current gain is typically 100ish, but can be lower or higher - check the datasheet), and (b) not so much that the current flowing into the base exceed's the transistor's rating. What solenoid are you using?
– Sean
May 31, 2018 at 16:14
• It's a solenoid valve for controlling water flow:coolcomponents.co.uk/products/solenoid-valve-12v-3-4 May 31, 2018 at 16:48
• The datasheet says the coil resistance is 4.75k+/-0.25k. Let's call it 4.5k. That means its load is 12V / 4.5k = 2.7 mA. If we take the BC109 transistor, the current gain listed on the datasheet is around 200 when the base is driven with 2mA (i.e. up to 400mA), which is more than enough for the solenoid. That means that you need a resistor that limits the base current to 2 mA. As you will use 3.3V from the Pi, and need 0.7V at the base, the resistor must drop 3.3-0.7V while providing 2mA, so it should be 2.6/0.002 = 1.3k. In reality, anything from 0.5-5k would probably work in this case.
– Sean
May 31, 2018 at 17:16

You can use a circuit like this:

The particular MOSFET shown will handle easily 6A or more without getting too hot, however the diode D1 should be upgraded if the current is more than a couple of amperes (eg. 1N5405). The main missing piece of information is the solenoid current.

Look at the datasheet for the MOSFET and see if you can predict the power dissipation when on. The resistor R2 is to turn the MOSFET off if the drive line goes open circuit. R1 slows the switching a bit and may help to protect your MCU if something bad happens.

simulate this circuit – Schematic created using CircuitLab

If you use an NPN transistor you will have to supply a lot of base current (typically about 1/10 to 1/20 of the solenoid current), which will require at least another transistor to drive the NPN unless the solenoid is very low current. One possible configuration to do that is with a darlington, but that drops about 1V so the transistor will get hot if the solenoid draws much current. If your 3.3V supply can handle the base current it would be better to use a PNP + NPN transistor (active low input). Switching is hard on the output transistor so one with a good safe operating area (SOA) needs to be considered to avoid 2nd breakdown failure.

Based on your supplied datasheet link, there is no solid information on the solenoid current at 12V. The 4750 ohms is for 220VAC making it about a 10W coil, so I might guess it will be more like 5W at 12VDC (usually AC coils are less efficient), making the current about 450mA. So a small MOSFET will do a good job, and a 1N4005 is more than okay. You would need 25-50mA base current to drive a BJT deep into saturation so a MOSFET is better.

If you think as software engineer could be able to handle electronics as you say you should, I started saying a big NO.

If you have knowledge using TDD, I will show you how to deal with electronics using TDD as software engineer.

# RED-GREEN-REFACTOR.

In electronics, we play all the time with tests. A datasheet is, in fact, the design tests converted into a human readable values, expected output for given and very known input.

Think your Raspberry Pi as a piece of software delieves data. The data is being processed by various elements to make a desireable result.

## Unit testing comes in hand.

You want to put a transistor in power switch mode. If you put a GPIO on, the solenoid starts, and if you put your GPIO off, your solenoid stops.

Things you need to deliver to the transistor you want:

• MOSFET: the MOSFET transistor does not need any input current, only needs a voltage between Gate and Source. You can read it from any datasheet searching "Vth". The usual is a N-MOS transistor with, at most 3.3 volts of "Vth" if Raspberry Pi delivers 3.3 volts from GPIO. You can put the output GPIO directly into the Gate without any resistor.
• BJT: the BJT needs constantly a current flowing from base to emitter. This current must be previously calculated and you must include a set of resistors between Base and GPIO to generate a intake current to the Base terminal. You need to calculate the current generated using the "Hfe" value in the datasheet to ensure your circuit will work as expected. BJT's get old easier than MOSFETs, and they degrade their properties, even the "Hfe" one.

### RED

If you choose MOSFET, which I reccommend you, you must insolate the output and make the test harness outside the Raspberry.

Note: the main difference between electronics and software is the tests: we love testing in electronics and we make all the parts the most isolated, testable and expectable as much as we can. In my experience between firmware and software developing, I could ensure you the software part doesn't want any tests, only the big thing works and move on the next project, hoping the zombie will be never be resurrected.

Your test will fail: you don't have any voltage source. Good, you will need two: one for 3.3 volts and other one for 12 volts.

Mount the Spehro Pefhany circuit without the resistors in the gate. DO NOT PUT YOUR SOLENOID YET Use a resistor to dummy it. The resistor is your mock object.

Power on both power supplies. The order is not very important, but I recommend you the 3.3 first and the 12v, later.

Note again: NEVER DISCONNECT THE 3.3V WHEN THE 12V IS ON. That's why there are resistors there. Gate is like a capacitor that charges if leaves unconnected. If you are mocking the input circuit part, is desireable to switch between 0 and 3.3 volts.

### Green

Now, you can put your voltmeter and ensure the voltage is the same as expected in the formulas.

When you put 0v, the MOSFET is in the cut zone and it will not drive any current. When you put in the 3.3v, the MOSFET will probably be in the saturation zone (or switching zone).

If the values you measure are similar to the numerical approach, congratulations, you passed the unit tests and will be ready to the integration test.

### Refactor

How it will be the refactor in electronics?

• Sustitution the design with other designs covers other features

• Adding new features while maintain the original ones

• Change some part that becames obsolete

Did any of those sounds familiar to you? In software, you made this all the time. Or you should do.

In electronics, our work material gets old, breaks or have malfunctions over his lifespan.

And go on using the red-green-refactor cycle until you get all the parameters tested and working as expected.

I hope you enjoyed this answer. It contains related info and useful tips, not really a complete answer.