rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Controlling 12VDC from GPIO

Mon May 27, 2013 5:24 am

Hi -

I have a Raspberry Pi that I am using to control a number of garden watering solenoids found here: http://www.adafruit.com/products/997.

I would like to use the GPIO pins on the RPi to activate the solenoids. Ultimately I need the 12V to power the solenoid when the GPIO pin is high, but have run into a number of issues with this. Most relays I find are rated for 5V and don't turn on at the 3.3V available on the GPIO pins. I have also wondered if using a relay may be overkill for what I am trying to do. I have thought about using an op-amp but am not sure if this will meet my needs current-wise.

Any suggestions as to what I can do? Electronics has never been my strong suit, but I am hoping to change that with this project. Thanks in advance.

TonyOddwires
Posts: 7
Joined: Thu Mar 28, 2013 11:05 am

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 1:31 pm

Whenever I need to switch 12volts I use a ULN2803. Each channel can take 500mA, so it should be ok with your solenoids. But if the chip gets hot, you can put two channels in parrallel.
I usually put a put a 1K current limiting resistor on each input to protect the Pi from catastrophic failure of the chip, as you wouldn't want the 12 volts from the solenoids finding its way back to your GPIO ports, but the chip already has internal 2.7K resistors on each input, so it's up to you.

http://www.datasheetcatalog.org/datashe ... 828_DS.pdf
Check out my website... www.oddwires.co.uk

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 2:39 pm

The cheapest way to drive a 12V solenoid or relay is to use a MOSFET. This transistor has three terminals. Connect Source to GND, Gate to the GPIO pin of the Pi, Drain to one end of the coil. If you are using long wires you need to add a 24V zener diode with Anode to GND and Cathode to Drain, to protect the MOSFET against voltage transients. A 0.5W or 1W zener is fine.
To find a suitable MOSFET, search for 30V n-channel MOSFET in SOT-23 package. You need a version that has the ON-resistance specified at 2.5V. A good price is $0.50when you buy only a handful.
RDS(ON) (at VGS =2.5V) should be 100milliohm or lower. One example is Si2300DS.

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 7:28 pm

Tony -

Thanks for the suggestion on the ULN2803. I have looked at the pinout and tried to simulate a Darlington connected transistor...it looks like the output level is the same as the input. So am I running the GPIO pin through an amplifier to get 12V and then connecting that to the input? Thanks again for the help.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 8:39 pm

rcrummett wrote:Tony -

Thanks for the suggestion on the ULN2803. I have looked at the pinout and tried to simulate a Darlington connected transistor...it looks like the output level is the same as the input. So am I running the GPIO pin through an amplifier to get 12V and then connecting that to the input? Thanks again for the help.
Those solenoids will need over 300mA each at 12v, so firstly make sure you have a good 12 power supply.

Use the uln2803's to connect to 5v relays. Use the 5v relays to switch the 12v solenoids for the watering system. You will need a separate 5v supply for the relays though - however, you could use 12v relays, then you just need one good 12v power supply. uln2803's will drive 12v relays from the Pi's 3.3v gpio outputs without any issues.

Note that relays have 2 different tarings - one is the coil voltage - the other is the contact switchting voltage - you need relays that will switch 12v at 0.5amps - in reality most are rated for a lot more than that.

If you are not comfortable designing from scratch, then do consider buying a module - e.g. http://www.skpang.co.uk/catalog/4ch-rel ... -1200.html

If buying one off ebay, do make sure it's rated to work at 3.3v - most of the saneismart ones I've seen are rated for 5v.

-Gordon
--
Gordons projects: https://projects.drogon.net/

BudBennett
Posts: 89
Joined: Fri May 17, 2013 2:45 pm
Location: Westcliffe, Colorado, USA

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 8:52 pm

Be very careful using an octal darlington. The darlington will not saturate with a low VCE voltage and you will get a significant amount of power dissipation per driver. I calculate PD = 0.5Watt for VCE=1.6V and IC = 320mA. If you try to apply this to all 8 drivers your going to get 4watts! The theta-JA is 55C/W which says your IC will be smoking at 220C. Tjmax for ULN2804 is 125C.

Also you can't just parallel darlington outputs. BJT transistors need a ballast resistor either in the emitter (which can't happen on the ULN2804) or in the collector. Otherwise you can get thermal runaway where one of the drivers tries to hog all of the current as its junction temperature goes sky high. MOSFETs don't suffer from thermal runaway because their RDSon has a positive temp coefficient - they can be used in parallel without ballast resistance.

Tage has the right idea. Use a n-channel MOSFET with a low rdson at your logic high level as VGS. Small package MOSFETs (i.e. SOT23) have a very high thermal resistance (250-350 C/W) so you need to keep the power dissipation low. You're going to have to purchase a SOT-DIP adapter board (see: http://www.adafruit.com/products/1230#Description) to mount the SOT23 on a normal through-hole PCB or protoboard. These are small boards that won't provide the kind of copper surface area to reduce the theta-JA by much. Tj = 0.1Ohm x 0.32A^2 x 250 C/W + Tambient = 26 C + 25C, which will still be hot to the touch.

Even with short lines to the solenoid it would be a good idea to put a small diode across the solenoid to protect the FET from the flyback voltage when the FET turns off - this is called a snubber.

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 9:15 pm

I certainly agree with the warning against using the Darlington array. With only 3.3V drive voltage from the GPIO there is barely enough drive current into the input to drive 300mA, and the voltage drop across the transistor is significant, in the order 1.5V...2V. So you could only drive a couple of solenoids without overheating the IC. If you use the Darlington array you have to use relays. Relays have the advantage that the load can be completely isolated from the Pi, so if you have a lightning storm there is less danger that energy induced into long control wires can reach you Pi. You could get relays that can be driven by 5V, so you do not need to connect the 12V circuit to the Pi with any galvanic connection.

For anyone wanting to drive much larger 12V loads than solenoids, have a look for example at SiR404DP. This 20V MOSFET is able to drive 25A continuous current while only using the circuit board as heat sink, and if that is not enough you can simply parallel several transistors, they share the load well (in contrast to Darlington transistors).

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Mon May 27, 2013 11:25 pm

Perhaps some more information might clear things up:

I have 10 of these solenoids and a 12v, 2A power supply (from an old external hard drive). Based on the current output I am planning to control them sequentially, probably 1-2 at a time. I am thinking that I have sufficient current available to run the solenoids in that fashion. Based on the discussion about the Darlington IC, I don't think I will ever be running more than 1-2 drivers at any given instant.

Although I like the simplicity of the MOSFET, I don't like the ~$5.00 price for a single adapter board when I would need 10. So on that level, the Darlington array/relay combo is appealing...unless a MOSFET similar to the one described is available in a thru-hole package...

If I did use a MOSFET, wouldn't that "invert" the voltage; wouldn't I have 12V to the solenoid when the GPIO pin is low and 0V when the GPIO pin is high? Could a second MOSFET fix that?

BudBennett
Posts: 89
Joined: Fri May 17, 2013 2:45 pm
Location: Westcliffe, Colorado, USA

Re: Controlling 12VDC from GPIO

Tue May 28, 2013 12:22 am

$5 gets you 5 of the SOT23 adapters (plus some others) which can mount 2 devices each - all of 10 of your drivers. You can hunt for through-hole devices, but the vast majority of new tech is SMT, at lower prices.

The polarity of the GPIO doesn't matter...

User avatar
elektrknight
Posts: 140
Joined: Sat Mar 02, 2013 1:25 pm

Re: Controlling 12VDC from GPIO

Tue May 28, 2013 2:11 am

10 TIP120 or similar darlingtons driven from GPIO via 150 ohm resistor will do the job.

DC steady-state calculation

3.3V from GPIO minus VBE which is 1.2V (datasheet) gives us 2.1V.
2.1V divided by 150 ohm comes out to about 14mA which is well within
the GPIO spec. 14mA times hFE of 1000 produces 14A but TIP120
in steady state will saturate with the 37.5 ohm solenoid resistance at
maximum 2V - worst case. 2V with 320mA current makes 0.64W
of power dissipation in the transistor, which is piece of cake for the
TO220 package. Actually, if you check the characteristics VCEsat
should be 0.7V for the 320mA current, comes to 0.224W of power
dissipated in the transistor.

That 150 ohm resistor can be theoretically increased to about 2.5k to
saturate the TIP120 but I would keep less then 1k to be on the safe side.

However, solenoids present inductive load when switched on and off and
generated transient voltages can kill not only the transistor but also the
GPIO pin and Pi too. To prevent voltage spikes use a clamping diode
parallel with solenoid. Also pay attention to ground wiring, it has to be low
inductance, use separate ground wires for your Pi and solenoids and connect
them at the power supply. Switching will generate all kinds of noise so put
some capacitors between 12V and ground, I would suggest 1 220uF and
at least 1 ceramic 100nF in parallel.

Disclaimer:
I did my best calculating all this but do not trust my math and check it yourself!
Placek Malinowy to jest to!

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Tue May 28, 2013 5:31 am

BudBennett wrote:$5 gets you 5 of the SOT23 adapters (plus some others) which can mount 2 devices each - all of 10 of your drivers. You can hunt for through-hole devices, but the vast majority of new tech is SMT, at lower prices.

The polarity of the GPIO doesn't matter...
Yeah...maybe I need to read the description a little better... :oops: In that case, the MOSFET route sounds like the way to go. I've ordered the SOT23 adapters and will get the FETs tomorrow.

Now, based on Tage's suggestions...
Tage wrote:The cheapest way to drive a 12V solenoid or relay is to use a MOSFET. This transistor has three terminals. Connect Source to GND, Gate to the GPIO pin of the Pi, Drain to one end of the coil. If you are using long wires you need to add a 24V zener diode with Anode to GND and Cathode to Drain, to protect the MOSFET against voltage transients. A 0.5W or 1W zener is fine.
Where do I connect the 12V supply?
Can you explain the role of the zener diode?
Do I need a diode across the terminals of the solenoid to protect the FETs from the solenoid shutoff voltage?

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Controlling 12VDC from GPIO

Tue May 28, 2013 1:58 pm

If you have everything including the solenoids within a meter distance from the Pi, then the simplest way to protect the MOSFET against overvoltage is to add a diode (any diode, but 1N4148 is commonly used) directly across the solenoid. The cathode to the +12V end and the anode to the other end of the solenoid winding. The reason for this is that because the solenoid is an inductor, the 350mA current will continue to flow for a brief period when the MOSFET turns OFF (GPIO goes low). If there is no path for that current other than the MOSFET, the voltage across it will rise to above the 30V rating of the MOSFET, until the MOSFET conducts anyways. It acts as a zener and clamps the Drain voltage to perhaps 35V or more. This results in a sudden heating of the transistor, and if it is too small it will fail, usually there will be a short circuit between Drain and Source, so it will now stay ON even if the GPIO commands zero volts on the Gate.
The diode across the solenoid will create a path for the current to flow and the voltage across the MOSFET will stay around 12V after turnoff. The voltage transient is not appearing. If you do not use a diode across the solenoid, OR if the wires from the MOSFET to the solenoid are many meters (creating a wire inductance), you can instead use a zenerdiode directly across the Drain-Source of the MOSFET, to clamp the voltage transient below the voltage rating of the MOSFET. A 15V to 24V zener would work with 12V supply. The zener will now take the energy transient at turn OFF. If it fails it will fail with a shortcircuit and the MOSFET and the PI are unharmed.
The MOSFET that I suggested is able to drive over 2A continously without overheating. You can find cheaper devices that are smaller, but a larger MOSFET will give you more safety margin, so that is why I suggested a 2A device when you could have used a 0.5A MOSFET. You may in fact not need a diode or a zener diode for protection, as the larger MOSFET can take the occasional transient, but it is good practice to use diode/zener diode to avoid that voltage transient - better be safe than sorry.

The 12V supply for the solenoid is connected with GND to Pi GND and +12V to one end of the solenoid. The other end of the solenoid connects to Drain of the MOSFET. When GPIO pin goes to 3.3V, it supplies voltage to the Gate which will cause the MOSFET to conduct. There is no dc current flow in the Gate so you could add a (1k to10k) resistor from Gate to GPIO pin to protect the Pi in case of some disaster. The resistor will also slow down the turn off speed of the MOSFET (by filtering the gate voltage, the gate has some capacitance so you get an RC filter) which will help to keep voltage transients low and might save you from using a zener.

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Wed May 29, 2013 3:44 am

Like this?
solenoid-control.png
solenoid-control.png (8.05 KiB) Viewed 13328 times
Not sure if there is a circuit symbol for a solenoid, so I used the inductor/switch combo.

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Controlling 12VDC from GPIO

Wed May 29, 2013 4:15 am

would the gate sink enough current to light the LED?

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Wed May 29, 2013 4:30 am

Good question - I'm not really sure, I just thought it might be a nice touch, having an LED turn on when a solenoid is activated.

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Controlling 12VDC from GPIO

Wed May 29, 2013 1:17 pm

your schematic is correct except for two mistakes: you should remove the mechanical switch, and if you want an LED to light up when the solenoid is ON, you should but that LED in parallel with the solenoid coil, (first, add a resistor in series with the LED).
Your circuit shows all three countermeasures against the inductive voltage spike that happens at turn off. Gate resistor that slows down turn off, zener across the MOSFET and diode in parallel with the coil. You could remove one or two of those fixes and still sleep well at night.
The LED in the schematic would not light up, and the MOSFET would stay ON for a long time after turning on. The gate of the MOSFET is a capacitor, this type of transistor is controlled by a voltage on the gate instead of a current into the base of an npn transistor.

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Thu May 30, 2013 4:58 am

Tage-

Taking into consideration your suggestions, I redrew the circuit. I just want to make sure that I am thinking about this right before I do something stupid and bring my whole project down.
solenoid-control-2.png
solenoid-control-2.png (9.38 KiB) Viewed 13187 times
Hopefully I have understood you correctly and made the suggested changes properly. If I did get rid of any countermeasure, is there a suggestion as to which one is most beneficial?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Controlling 12VDC from GPIO

Thu May 30, 2013 5:10 am

You might want to bump up the current-limiting resistor for the LED.

Code: Select all

I = V/R
 = (12V -Vled) / 100
 = 10 / 100
 = 100mA
seems a bit high, 20mA is more typical.

(assuming 2V drop over the LED, see
https://en.wikipedia.org/wiki/Light-emi ... _materials )

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Thu May 30, 2013 5:17 am

You're probably right...I just left in the default value from CircuitLab. Your math suggests a 500 ohm resistor; would 1k be okay?

Aside from that, does everything else look good?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Controlling 12VDC from GPIO

Thu May 30, 2013 5:26 am

Try it and see if the LED is still bright enough for you. If not put another 1k in parallel:

https://en.wikipedia.org/wiki/Series_an ... esistors_2

The rest of the circuit looks good to me, but I haven't done much with FET's and Tage picked up a few things I totally missed last time.

rcrummett
Posts: 9
Joined: Mon May 27, 2013 5:10 am

Re: Controlling 12VDC from GPIO

Thu May 30, 2013 5:57 am

Okay, components have been ordered. Can't wait for them to get here and put this together!

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Controlling 12VDC from GPIO

Fri May 31, 2013 12:48 am

yes, 1k is ok for the LED, that gives about 10mA current. all seems OK. just one precaution: if you add the LED like this in parallel with the coil you should keep the diode in parallel with the coil, because the inductive voltage transient will otherwise create a reverse voltage on your LED, and most LEDs do not take more than a few volts in reverse.
if you have short leads (less than one meter) you could remove the zener. with the 10k gate resistor in place the MOSFET will switch so slowly that you will not see any voltage transient caused by long leads, so the only reason to keep the zener would be to have additional protection in case you have a lightning strike nearby. this could induce a high energy pulse into the long wiring from you Pi/MOSFET to the solenoid. if that happens the zener will probably die and become a short circuit, which saves you MOSFET and Pi.
the only purpose for having the 10k gate resistor is to slow down the switching of the MOSFET so that the inductance in long leads (many meters) does not cause a voltage spike on your MOSFET at turn off. and the other reason for using the 10k gate resistor is to protect you Pi in case you make a mistake and the MOSFET fails so that the 12V is applied to the Gate node. that would kill your Pi instantly, but the 10k resistor will most likely limit the current to less than 1mA which should be safe for the GPIO pin.
keep the schematic as is, if you want maximum protection.

tarunpetluri
Posts: 2
Joined: Thu Dec 12, 2013 9:07 am

Re: Controlling 12VDC from GPIO

Sat Dec 14, 2013 9:36 am

im trying to control a 12v latch using pi's gpio.....i hav connected gpio4 to a 1k ohm resistor..and the resistor to the base of 2n2222 transistor...collector end i have connected to the positive terminal of 1n4001 diode...and the negative of the diode to a 12v supply
the 12v supply is also connected to the relay's A terminal..and B terminal is connected to the collector end, i have grounded the emitter and connected the common terminal of the relay to 12V supply....problem is the relay is not switching....please suggest some ideas.. :cry:

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Controlling 12VDC from GPIO

Sat Dec 14, 2013 3:00 pm

a schematic sketch would make it easier, it appears that you have the relay connected correctly. you are talking about a "latch", what is that? if you are trying to control a latching relay you may need a special circuit configuration. if you have a conventional relay you just have to keep checking your circuit, perhaps replace the transistor. try to move the base resistor to 3.3V pin instead of GPIO to test the function of the driver circuit.

Paul Moir
Posts: 46
Joined: Wed Dec 04, 2013 7:25 pm
Location: Nova Scotia, Canada

Re: Controlling 12VDC from GPIO

Sat Dec 14, 2013 3:52 pm

How big is the relay? It's not some big honking one is it? With 3ma base current you'll only be able to drive ~300ma.

Hook up a multimeter pin and measure the gpio output directly to make sure it's going to 3.3v when you think it is. If you can't, you can test your drive circuit by disconnecting it from the gpio pin and trying it on the 3.3v and ground rails.

PS - You have the "band" end of your diode attached to the +12v side right? Diodes have anodes and cathodes, not positive or negative sides. That is to say, I'm not certain what you mean by each side. No need to be fancy with the schematic: a photo of a hand drawn one will do.

Return to “Automation, sensing and robotics”