I'm a musician and I'm working on a funny project : control solenoids with a midi keyboard.

So I have a MIDI keyboard connected to my raspberry pi connected to my breadboard. Until now everything is working correctly with 12 LEDs, every time I press a key, a different LED is turned on.

Where I'm stuck, mainly because I lack of electronic and electricity education, is where it come to control the solenoids, I don't really know how to power them, and how to prevent the raspberry from frying.

I've planned to use 12v solenoids using 1.4A

Here is what came out of my researches on the internet. Please warn me, correct me, (help me) if something is wrong...

  • When a pin on the raspberry is turned on, it's delivering 3,3v power.

  • The pin should be connected to a resistance (1k), then to a LED (for witness), then to a transistor (TIP120 ?).

  • The "ground leg" of the transistor should be connected to the ground of the raspberry and the ground of the power supply without any resistance or diode. (I'm really in doubt of that, afraid of frying the raspberry...) .

  • The 12v power supply (+) should be connected to the solenoid (+).

  • The solenoid (-) should be connected to the middle leg of the transistor (the one activated when it receive the 3,3v from the pin) and also to a diode (1N4004 ?). That diode should be connected to the + of the power supply.

Is that correct ?

Questions :

  • What to use as power supply ?
  • Can I use batteries ? one for each solenoid ?
  • Any idea for supply all the solenoids at once ?

I hope I was clear, thanks you very much for your time and answers :)


2 Answers 2


From the datasheet, the TIP120 has a minimum current gain of 1000 (probably a little more than this at 1.4A, see figure 1). This means that for a 1.4A solenoid current through the collector pin, at least 1.4mA must be supplied into the base pin by the Raspberry Pi. However, for this type of switching application the transistor should be turned on 'strongly', and this is done by putting more base current into the transistor - perhaps four times the minimum 1.4mA, or 5.6mA.

The Raspberry Pi's maximum output current across all GPIO pins is 50mA (see this question and links therein), with a maximum of 16mA per pin. This means that with the TIP120 transistor you would only be able to power a maximum of 8 solenoids simultaneously. If this is acceptable, (and you are enforcing this limit in software), then the following circuit should suffice. I think this is roughly what you are suggesting, but the LED has moved in order to minimise the current drain required from the Raspberry Pi.


simulate this circuit – Schematic created using CircuitLab

The resistors are calculated as follows. According to figure 2 of the datasheet, V_BE(sat) = 1.6V or thereabouts at 1.4A collector current. This means that, when turned on, the base of the transistor will be at 1.6V. When turned on, the Raspberry Pi pin is at 3.3V, so we need a base resistor R1 that will provide the require 5.6mA of current, given that there is 3.3V-1.6V = 1.7V across it. Using Ohms law, R=V/I = 1.7/0.0056 = 300 ohms.

The voltage at the collector of the transistor when turned on is about 0.8V (datasheet figure 2, V_CE(sat) figure). Assuming a couple of volts drop across the LED, this means that a resistance of 1Kohm for R2 gives about 9mA through the LED - a value suitable for most LEDs, but check the datasheet for your particular LED.

The flyback diode conected across the solenoid could easily be a 1N4004 as suggested, but a fast Schottky type diode, as illustrated, is marginally preferable.

Since the voltage between the collector and the emitter of the transistor is about 0.8V, and the current is about 1.4A, the transistor is dissipating about a 0.8V*1.4A = 1.12 watt of power as heat when turned on. Though the datasheet lists 2W as the maximum power dissipation (when the device is in a 25°C ambient temperature), the device will probably get hot -- 100°C or more. I would advise putting a small heatsink on each transistor.

If you would like to run all 12 solenoid valves simultaneously, you will need to use a different transistor, and a MOSFET type is probably the best choice. There are MANY available, but something like the NXP PSMN017-30PL ( http://www.farnell.com/datasheets/1596019.pdf ) or PSMN022-30PL would work very well. It could replace the TIP120 in the circuit below without any other modifications to the circuit - and would not need a heatsink.

  • \$\begingroup\$ Thank you very much Chris Johnson ! I think you've answered all my questions clearly ! If I use MOSFETs, what's the maximum number of solenoids I could control with 1 R-pi ? (assuming that I have gpio extentions). The end of this project, is to have 88 solenoids, one for every note on the piano. Right now, I only have 12, one for every note in one octave. \$\endgroup\$
    – Walkyrie
    Apr 27, 2014 at 13:22
  • \$\begingroup\$ @Walkyrie MOSFETs take negligible drive current unless they are being switched on an off very rapidly (thousands of times a second), so you could drive all 88 easily from RPi outputs (if it had 88 GPIOs!). How did you plan to expand the RPi to this many outputs? \$\endgroup\$ Apr 27, 2014 at 13:26
  • \$\begingroup\$ That's a very good question... I'll probably use the I2C: MCP23016 that can be combine up to 128 gpio... Otherwise I was thinking of using 1 raspberry per octave, but I don't like this idea. \$\endgroup\$
    – Walkyrie
    Apr 27, 2014 at 13:39
  • \$\begingroup\$ @Walkyrie Yes, six MCP23016 (or its modern replacement, the MCP23017) would work well. By the way, 88 solenoids * 1.4A is nearly 125 amps - a LOT of current! Do you need to run all 88 solenoids at once? If so, dealing with the power required will require some careful thought... \$\endgroup\$ Apr 27, 2014 at 13:47
  • \$\begingroup\$ I'll never have all the 88 solenoids running in the same time, big maximum 30. It will go very fast, since I want it to be synchronized to the sound of the piano. But it's a very good point you're coming with, what kind of alimentation would you recommend ? \$\endgroup\$
    – Walkyrie
    Apr 27, 2014 at 13:56

Seems good to me. The transistors used as a power switch may not be the best - their DC current gain means your R-Pi will have to output ALOT of base current into each Transistor (and with 12, that is far more than your R-PI Can or should put out at the same time), especially if you want the best (lowest) voltage drop over the transistor. I suggest you get a MOSFET with a very low turn-on voltage (1-2V VGS Threshold)

You can use a 3-cell Li-Po battery pack, with whatever Amp-hour capacity you need for approximate length of operation. If you are constantly pulling 1.4A, then a 5 Amp-hour battery pack will supply you for ~3 hours down to 11V when things probably will stop working, for 12V devices. Obviously if you only have the solenoids on average activated 10% of the time, you will get ~30 Hours out of the battery charge.

I suggest you put a 5Watt power resistor (maybe 8 Ohms, for 1.4A at 12V) just in case of short circuits but one that should not limit your device during normal operation.. I guess you should play with these values though!

  • \$\begingroup\$ Except "•The pin should be connected to a resistance (1k), then to a LED (for witness), then to a transistor". The LED forward voltage is probably too high to make this a good circuit. Remove the LED (but leave the resistor). Then connect the LED, with a 1k resistor in series, across the relay coil. Another possibility for a replacement for the transistor is a Darlington, and there are a number of TIP series units available. \$\endgroup\$ Apr 27, 2014 at 11:08
  • \$\begingroup\$ Oh yes @WhatRoughBeast I agree, no LED should be put in series with all this \$\endgroup\$
    – KyranF
    Apr 27, 2014 at 11:48
  • \$\begingroup\$ The transistor suggested in the question is a darlington (TIP120), with minimum beta of 1000, which at 2mA per base (=1K base resistors, given 3.3V supply and the 1.3V vbe drop of a darlington) seems very suitable - well within the maximum output current of the pi? \$\endgroup\$ Apr 27, 2014 at 12:02
  • \$\begingroup\$ Thank you for your answers gentlemen :) Just to sum up and make sure I understood correctly... \$\endgroup\$
    – Walkyrie
    Apr 27, 2014 at 12:51
  • \$\begingroup\$ - I need to move the LED after the transistor - I need to replace the transistor by a MOSFET. Which MOSFET would you recommend ? \$\endgroup\$
    – Walkyrie
    Apr 27, 2014 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.