# Controlling a Solenoid With Raspbery Pi GPIO Output Using a Transistor

I'm trying to control a solenoid using the RasPi 2's GPIO pins. Since the solenoid needs more power than the RasPi can provide, I'm using a 9V battery to provide the power.

I started with just wiring the circuit and checking if it works by connecting and disconnecting the 3v3 output to the board.

First, here's the wiring I'm currently using:

The diode accross the solenoid inputs is a standard flyback diode, and the resistor is a 1kOhm resistor.

Apparently the RasPi GPIO output pins produce 3.3V at a maximum draw of 16mA.

The inductor represents my solendid, which I got from Amazon. As far as I can tell, it requires 12V / 1A to operate, but connecting the 9V battery straight to it (skipping the transistor) works nicely enough. Even the RasPi 5V output provides a weak activation.

However, when going through the transistor, the solenoid does not activate properly. I have noticed that when the base pin is on though, it will "hold" the end position if pushed manually, just not push towards there by itself. It does not do this if the base pin is not connected.

I tried using a BC547 NPN transistor, then I figured the output current was not enough, so I switched to a BC639 (which should have up to 1-1.5A current output), but that didn't help either.

My question is, what transistor should I use? Am I doing anything else wrong? I've seen other people use a TP120, which seems to have an even higher output current.

Since I'm setting up a circuit with 24 of these, I'd like to know for sure before ordering another batch of transistors.

• Check out this link MOTORS AND THE FLYBACK DIODE You might need a edx account. – Mahendra Gunawardena Sep 26 '15 at 18:10
• Please post a schematics. Wiring diagram of a breadboard doesn't count as a schematic. (Not to mention that the drawing shows an Arduino, while question is asking about Raspberry Pi.) – Nick Alexeev Sep 26 '15 at 18:11
• Never EVER use a transistor to control anything that draws more current than an LED. For a solenoid, use a MOSFET or a relay. – user86234 Sep 26 '15 at 19:01
• @tuskiomi: That's bad advice. There are many good reasons to use BJTs in high-power applications. You just need to know what you're doing. – Dave Tweed Sep 28 '15 at 11:28
• – Passerby Sep 28 '15 at 15:25

You have the solenoid wired between the transistor's emitter and ground. In this configuration, the maximum voltage across the solenoid will be limited to the output voltage of the MCU minus the VBE drop of the transistor, which is about 0.65 V. A 3.3V MCU output will give you at most about 2.6V across the solenoid.

Any significant load current will cause an additional voltage drop across the base resistor. For example, if the transistor has a beta of 100, and you draw 260 mA through the transistor, you'll have a base current of 260 mA / 100 = 2.6 mA, and a voltage drop of 2.6 mA × 1000 Ω = 2.6 V across the base resistor, leaving you with no output voltage at all!

In your case, the solenoid has a DC resistance of about 12 Ω, so you're getting about

$$\frac{2.6 V}{\frac{1000 \Omega}{100} + 12 \Omega} = 118 mA$$

through the solenoid, about 1/10 of what it needs.

You need to connect the solenoid (and its diode) between the positive power supply and the collector of the transistor. Connect the emitter directly to ground. This gives you the full supply voltage across the solenoid, minus the VCE(SAT) of the transistor, which should be just a few hundred mV.

• If that were true, then the solenoid would not hold at all. – Passerby Sep 28 '15 at 11:59
• @Passerby: If what were true? Obviously, at some intermediate value of voltage and current, there will be some magnetic force inside the solenoid, just as the OP described. – Dave Tweed Sep 28 '15 at 13:39
• Too bad I'm out of votes for today! Indeed this is a classic case when a straight high-side switch (as the OP tried) doesn't work while low-side switch would be fine. The high-side switch can also be made to work (with more parts), but it's not really a worthwhile endeavor here because the downsides of low-side switch don't really come into play here. The links are for @mirceapricop and the other confused posters here, not for Dave. – Fizz Sep 30 '15 at 18:40
• Even more doomed, the OP is trying to use a single NPN transistor as high-side switch (not even the right type for that, i.e. PNP). – Fizz Sep 30 '15 at 18:48
• Thank you so much! You just saveed me 60\$ worth of MOSFETS and everything works fine! – mirceapricop Oct 4 '15 at 10:46

Try this logic-level MOSFET

That keeps you from having to deal with current gain problems (Beta/hFE) if the gain is too small. (ie if the Base current is 16mA and the hFE is 25, the collector current will only be 0.4A) (If you're only driving 100mA or so, just go ahead and use the BJT though!)

If you put a 200 Ohm resistor between the GPIO and Gate pin, that will limit the current to 16mA max during switching. That shouldn't be a problem unless you try to switch really fast (which will cause a lot of heating because of how slowly you're switching the MOSFET).

Take care when handling the MOSFET, especially during dry weather. The Gate is very sensitive to electrostatic discharge. Don't wear those awesome fleece pajamas you got while you're working!

• The problem is not with the transistor, the problem is the circuit configuration that it's in. – Dave Tweed Sep 28 '15 at 11:26
• Ok, so if Ib is limited to 16mA, and Beta is only 25 (which is possible in the question's BJT) the transistor could fall out of saturation with as little as 22.5 Ohms of load. There are multiple problems here. – Daniel Sep 28 '15 at 18:54

A few issues.

9V batteries are not that strong. A 1A solenoid will drain it quickly.

A 12V solenoid may trigger at 9V, but th at's already 75% of the target voltage. Any lower and it won't trigger.

A solenoid needs less current and a lower voltage to hold a state than it does to trigger.

A solenoid under load will be worse.

A 1k ohm resistor, with 3.3V source and 0.7V Vbe drop on the transistor is only 2.6mA of current. The transistor will be weak. You need a 162 ohm resistor for the full 16mA. As such, the Ice will not be the full current you want.

The VCE drop, even at full strength may cause the solenoid to not receive enough voltage to trigger. You are reducing the voltage from the 9V even lower. Combined with a weaker battery that's causing the voltage to be closer to 8V most likely.

• You say you don't need a schematic, but you missed the key problem with the circuit! – Dave Tweed Sep 28 '15 at 11:24