Page 1 of 1

Controlling a solenoid

Posted: Thu Dec 19, 2013 5:10 am
by mrtarantula
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.

Re: Controlling a solenoid

Posted: Thu Dec 19, 2013 9:49 am
by texy
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

Re: Controlling a solenoid

Posted: Thu Dec 19, 2013 5:46 pm
by mrtarantula
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 5893 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 5893 times

Re: Controlling a solenoid

Posted: Fri Dec 20, 2013 1:58 am
by mrtarantula
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.

Re: Controlling a solenoid

Posted: Fri Dec 20, 2013 4:09 am
by Tage
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?

Re: Controlling a solenoid

Posted: Fri Dec 20, 2013 6:10 am
by mrtarantula
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 5729 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)

Re: Controlling a solenoid

Posted: Fri Dec 20, 2013 11:36 am
by pksato
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.

Re: Controlling a solenoid

Posted: Fri Dec 20, 2013 12:19 pm
by Burngate
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 5650 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.

Re: Controlling a solenoid

Posted: Sun Dec 22, 2013 12:31 am
by mrtarantula
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 5450 times

Re: Controlling a solenoid

Posted: Wed Dec 25, 2013 8:55 pm
by boyoh
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

Re: Controlling a solenoid

Posted: Wed Dec 25, 2013 10:15 pm
by LateDev
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

Re: Controlling a solenoid

Posted: Sat Jun 25, 2016 11:33 pm
by 0Soul
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.

Re: Controlling a solenoid

Posted: Sat Jun 25, 2016 11:55 pm
by DougieLawson
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.

Re: Controlling a solenoid

Posted: Mon Jun 27, 2016 7:18 am
by texy
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