Electronic – Control 12 solenoids with a raspberry pi

power supplyraspberry pisolenoidtransistors

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 🙂

Best Answer

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.

schematic

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.