mrtarantula
Posts: 5
Joined: Thu Dec 19, 2013 4:51 am

Controlling a solenoid

Thu Dec 19, 2013 5:10 am

Disclaimer: I know next to nothing about building electronics.

I am trying to control a solenoid (eventually several) but I am running into a big problem. The RPi isn't triggering it. The solenoid fires when directly connected to pin 1 (3V3) but does not fire when connected to pin 18, using the following test program:

Code: Select all

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

GPIO.output(18, True)
sleep(1)
GPIO.output(18, False)
GPIO.cleanup()
Using a multimeter I can see that the pin is getting power while the program is running. I thought the Pi might not be able to supply the amperage to run the solenoid directly, so I bought an external power supply and tried connecting the GPIO pin to the base of a transistor, but that still does not work. If I jump the base over to the power supply's positive lead, it fires. I hope my explanation has been clear enough.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Controlling a solenoid

Thu Dec 19, 2013 9:49 am

Hi and welcome to the forum.
Sounds like you;ve done every correctly - you will definately need to use an external power source for the solenoid, and you will also need to use a tranny to actgivate it. David Hunt has a very good write up up on how to do it - double check you have wired the circuit correctly. For one thing, it is unsure from your post if you are checking the voltage coming out of GPIO18 from the pi - note this is NOT physical pin 18 of the GPIO port.
http://www.davidhunt.ie/?p=2770
Hope this helps,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

mrtarantula
Posts: 5
Joined: Thu Dec 19, 2013 4:51 am

Re: Controlling a solenoid

Thu Dec 19, 2013 5:46 pm

My setup is similar to David's, but without the flywheel diode, which should not make a difference as far as getting the voltage. When I get home from work I will try to set up as close to David's setup as possible. Here is a view of my full setup:
board.jpg
board.jpg (56.76 KiB) Viewed 4292 times
The line "GPIO.setmode(GPIO.BCM)" should probably have been "GPIO.setmode(GPIO.BOARD)" which should fire the pin labeled 18, correct? I have tried both ways, and have verified with a multimeter that the voltage is good when it fires, but it still does not trigger the transistor. Here is a closer look at the transistor setup. If I were not at work I would draw a diagram. Also, please ignore the bad soldering, I have thoroughly tested each pin all the way through, and physically it is working.
cobbler.jpg
cobbler.jpg (63.14 KiB) Viewed 4292 times

mrtarantula
Posts: 5
Joined: Thu Dec 19, 2013 4:51 am

Re: Controlling a solenoid

Fri Dec 20, 2013 1:58 am

I have double-checked everything, and tested the voltage to make sure the right pin was firing. My setup is exactly the same as David's. Why is it not working? The solenoid fires with direct current to the transistor's base, but the voltage from the Pi has no effect at all, other than moving my multimeter.

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

Re: Controlling a solenoid

Fri Dec 20, 2013 4:09 am

the typical pinout is EBC
check your connections again. it looks like you grounded you base and have also other mistakes. where is the collector and emitter wired?

mrtarantula
Posts: 5
Joined: Thu Dec 19, 2013 4:51 am

Re: Controlling a solenoid

Fri Dec 20, 2013 6:10 am

Tage wrote:the typical pinout is EBC
check your connections again. it looks like you grounded you base and have also other mistakes. where is the collector and emitter wired?

I corrected my mistakes and made a very simple circuit. Here is the flow of electricity. The yellow is coming from the external power, through the solenoid and into the collector, out of the emitter and to ground. The orange is coming from the Pi, through a 1k resistor and into the base.
Image 4.jpg
Image 4.jpg (27.24 KiB) Viewed 4128 times
I also wrote another test program using the wiringPi:

Code: Select all

import wiringpi
from time import sleep
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
io.pinMode(1,io.OUTPUT)
io.digitalWrite(1,io.HIGH)
sleep(1)
io.digitalWrite(1,io.LOW)

pksato
Posts: 295
Joined: Fri Aug 03, 2012 5:25 pm
Location: Brazil

Re: Controlling a solenoid

Fri Dec 20, 2013 11:36 am

Hi,
That is code of transistor in use?

Solenoid is a high current device (more than 1A),
For example, this
https://www.sparkfun.com/products/10391
Need 36V and 2.7A. (work on 12V, with loss in strength)

To correct drive solenoid need to use power bi-polar transistor and a signal transistor do drive it.
Or, preferably, a mosfet power transistor, with gate bellow 3V.

And a need a way to protect transistor and RPi against flyback effect of solenoid self inducted electromotive force. This protection generally is done by a reverse biased diode.

User avatar
Burngate
Posts: 6046
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Controlling a solenoid

Fri Dec 20, 2013 12:19 pm

I've tried to sketch your circuit from your latest photo.
I'm assuming the transistor legs are as I've labeled them.
Solenoid.PNG
Solenoid.PNG (10.36 KiB) Viewed 4049 times
You don't seem to have any ground connection from the Pi - though you seem to have done so in your previous photos.

The Pi's ground must be connected to the ground of the solenoid supply.

mrtarantula
Posts: 5
Joined: Thu Dec 19, 2013 4:51 am

Re: Controlling a solenoid

Sun Dec 22, 2013 12:31 am

For some reason I was under the impression that the GPIO ground pin is a ground, not goes to ground. I don't know why. Anyway, this is my current setup, but it still does not fire the solenoid. Like before, I have verified the voltage from the GPIO.
diagram.jpg
diagram.jpg (22.63 KiB) Viewed 3849 times

boyoh
Posts: 1341
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Controlling a solenoid

Wed Dec 25, 2013 8:55 pm

Make sure the relay is in the voltage & current range



( K ( Diode ) A
5v+-----------( Relay )-----------------c

GPIO o/p-------( 330R )---------b ( transistor 1n2222 )

0v-------------0v--------------------------e
5v( ov ) ( Pi ov)
( Common
Last edited by boyoh on Wed Dec 25, 2013 11:33 pm, edited 1 time in total.
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

LateDev
Posts: 41
Joined: Fri Dec 20, 2013 6:27 pm

Re: Controlling a solenoid

Wed Dec 25, 2013 10:15 pm

Can you give specifics as to what current the Relay requires, or at the very least the coil resistance ?
Whenever you want to test any cct out of this nature, you should replace the relay with a resistor, and make sure everything works.

As far as missing out the flywheel diode, you musn't, unless you want to take the chance that you may blow everything to bits.

http://www.scribd.com/doc/101830961/GPIO-Pads-Control2

Try changing the base resistor to a 180R
To help where possible, and always to learn.

0Soul
Posts: 5
Joined: Fri Dec 11, 2015 2:37 am

Re: Controlling a solenoid

Sat Jun 25, 2016 11:33 pm

wiringpi is old and outdated, mostly no longer used in the same way, it now has a different name, hence the
RPi.GPIO plus you were closer to the right code at the start. you might have to adjust the sleep times though depending.

User avatar
DougieLawson
Posts: 36305
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Controlling a solenoid

Sat Jun 25, 2016 11:55 pm

0Soul wrote:wiringpi is old and outdated, mostly no longer used in the same way, it now has a different name...
Balderdash and poppycock.

The WiringPi C/C++ library is still current, still developed and still useful and still called "WiringPi". The only problem with it was the extra pin numbering scheme (which you can ignore because it supports Broadcom's GPIO numbering).

The alternate to WiringPi is Joan's excellent pigpio and pigpiod daemon.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Controlling a solenoid

Mon Jun 27, 2016 7:18 am

0Soul wrote:wiringpi is old and outdated, .
I really do wonder about the logic of this statement when you are replying to a 2 1/2 year old thread.......

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Beginners”