JoshuaLewis1809
Posts: 9
Joined: Fri Jan 25, 2013 7:32 pm

Controlling solenoids with GPIO

Fri Jan 25, 2013 7:42 pm

Hi!
My mate and I are in a band, and will be performing Omen by The Prodigy in our school battle of the bands. We thought it'd be really cool to get the Pi to play the glokenspiel in the song. So we have 3 solenoids to play three notes, wire, stripboard, resistors, transistors, springs for the solenoids to return to normal position(?), diodes, and a 12v power supply that we have to taken the end off of so there's just bare wire.

I assume ill have to write a python script thing to do this.
Anyone got any coding/circuit suggestions? My mate and I are AS Physics standard electronics, so were not too awful (ha)

Thanks for your time, anything you have to say would be valuable!
Josh

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

Re: Controlling solenoids with GPIO

Fri Jan 25, 2013 11:40 pm

JoshuaLewis1809 wrote:Hi!
My mate and I are in a band, and will be performing Omen by The Prodigy in our school battle of the bands. We thought it'd be really cool to get the Pi to play the glokenspiel in the song. So we have 3 solenoids to play three notes, wire, stripboard, resistors, transistors, springs for the solenoids to return to normal position(?), diodes, and a 12v power supply that we have to taken the end off of so there's just bare wire.

I assume ill have to write a python script thing to do this.
Anyone got any coding/circuit suggestions? My mate and I are AS Physics standard electronics, so were not too awful (ha)

Thanks for your time, anything you have to say would be valuable!
Josh
You don't have to use Python to program the Pi's GPIO. It's supported by C, BASIC, Shells, Pascal, Perl, PHP, Ruby and probably many others...

And did you see this? http://www.raspberrypi.org/archives/1471

As for circuits - you seem to have the bits - just join the dots ;-)

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

JoshuaLewis1809
Posts: 9
Joined: Fri Jan 25, 2013 7:32 pm

Re: Controlling solenoids with GPIO

Sat Jan 26, 2013 1:11 am

yeah i did see that! but I swear we thought of it without seeing that first haha.
hmm, well would you not recommend python? seems like the easiest way of doing it
and also, I don't think the circuit will be particularly difficult, just not too sure as which pins to attach the negative terminal wire to - ground or the 0V pin. well have three of these, so i dont know where the other two would go...

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12356
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Controlling solenoids with GPIO

Sat Jan 26, 2013 2:27 am

GND, and 0V are interchangeable terms, all GND's are, or should be, interconnected. Electric signals always need to be able to run in a complete loop, so the wire into which all the return currents flow (back), and which is used as a zero volt reference, is called the GND wire (GND short for ground, the earth).

also see http://en.wikipedia.org/wiki/Ground_(electricity)

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

Re: Controlling solenoids with GPIO

Sat Jan 26, 2013 9:35 am

JoshuaLewis1809 wrote:yeah i did see that! but I swear we thought of it without seeing that first haha.
hmm, well would you not recommend python? seems like the easiest way of doing it
and also, I don't think the circuit will be particularly difficult, just not too sure as which pins to attach the negative terminal wire to - ground or the 0V pin. well have three of these, so i dont know where the other two would go...
I would recommend Python if you are a python programmer. I'm a C programmer (mostly), so I would recommend C.

Looks like mahjongg has answered your ground question though - Connect and the grounds/0v together in your circuit then connect that to the 0v pin on the Pi's GPIO connector.

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

JoshuaLewis1809
Posts: 9
Joined: Fri Jan 25, 2013 7:32 pm

Re: Controlling solenoids with GPIO

Sun Jan 27, 2013 5:57 pm

haha, im a nothing programmer! :P

JoshuaLewis1809
Posts: 9
Joined: Fri Jan 25, 2013 7:32 pm

Re: Controlling solenoids with GPIO

Wed Jan 30, 2013 10:28 pm

Right! The circuit works on its own! we us a 9V battery in place of the pi's gpio for testing purposes and the solenoids work and hit the glokenspiel.
Now, for the coding:
Here's what I've done as the foundations, I just need to get the timings right to match the song, ill do that in my own time
BTW, this isnt actually what the solenoids will do on sunday, I just used these timings to test with

#Set up program
import RPi.GPIO as GPIO
import time
#Which numbering system to use (Here, Physical pin number i.e. 7th pin, not Broadcomm names)
GPIO.setmode(GPIO.BOARD)
#Tell program which GPIOs are going to be used
GPIO.setup(7, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
#Sequence of events
while True:
GPIO.output(7, True)
time.sleep(1)
GPIO.output(12, True)
time.sleep(1)
GPIO.output(15, True)
time.sleep(1)
GPIO.output(7, True)
time.sleep(1)
GPIO.output(12, True)
time.sleep(1)
GPIO.output(15, True)
time.sleep(1)
#Reutn GPIOs to standard states
GPIO.cleanup()

So that program will play the 3 notes/3 solenoids twice each in theory with a one second gap between
I cannot test this for a couple of days as I need GPIO compatible wire! Ugh
Anyway, some questions if you wouldnt mind helping me
How long does gpio.output(x, true), last for? I mean, how long is a voltage applied from gpio x?
WHERE CAN I FIND SOME WIRE THAT SAFELY WORKS WITH GPIO instead of poking LED's at the pins!
Is there any other python wizardry anyone can recommend to me for this?
Thanks!

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: Controlling solenoids with GPIO

Wed Jan 30, 2013 10:43 pm

The voltage is applied for as long as you want (ie until you make it False or Low)

Code: Select all

GPIO.output(7, True)
time.sleep(1)
GPIO.output(12, True)
time.sleep(1)
Should be

Code: Select all

GPIO.output(7, True)
time.sleep(1)
GPIO.output(7,False)
GPIO.output(12, True)
time.sleep(1)
GPIO.output(12,False)
As for wire I got some male to female jumper wires and a breadboard from eBay pretty cheap.

JoshuaLewis1809
Posts: 9
Joined: Fri Jan 25, 2013 7:32 pm

Re: Controlling solenoids with GPIO

Wed Jan 30, 2013 10:46 pm

Ah that's cool. Does the time.sleep command also work for False?
Ah yeah I thought of eBay but this needs doing for Sunday!

davidmam
Posts: 101
Joined: Tue Dec 06, 2011 4:13 pm

Re: Controlling solenoids with GPIO

Thu Jan 31, 2013 3:02 pm

Sleep is independent of the pin settings.

Think of True/False as two different settings on switches. Once it is set one way it will stay like that till you tell it differently.

ie turn switch A to on.
Wait ten minutes
turn switch B to off.

Switch A is still on all this time..

As for wires etc. you woudl probably find it really useful to get a bunch of jumper wires and a breadboard to set stuff up on before you start soldering. Bell wire (solid core wire) can make good jumpers.

Which solenoids were you using?

..d

JoshuaLewis1809
Posts: 9
Joined: Fri Jan 25, 2013 7:32 pm

Re: Controlling solenoids with GPIO

Thu Jan 31, 2013 4:10 pm

That's why I use the gpio cleanup command at then end, to return everything to its normal state right?
I'm using 3 little 12v ones from RS - bloody extortionate price! But should be worth it if it works on Sunday. In hoping to send the pi gpio voltages to 3 transistors, which will then send the 12v from my power pack to the solenoids. Don't see how this can fail! (Cough)

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: Controlling solenoids with GPIO

Wed May 08, 2019 1:53 pm

Could you share your circuit diagram. I also want to control a solenoid with raspberry as a button presser.
Thanks

pcmanbob
Posts: 7153
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Controlling solenoids with GPIO

Wed May 08, 2019 2:43 pm

rajbuddy007 wrote:
Wed May 08, 2019 1:53 pm
Could you share your circuit diagram. I also want to control a solenoid with raspberry as a button presser.
Thanks
You are unlikely to get an answer as JoshuaLewis1809 has not been on the forum since 01 Feb 2013, 21:43
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Automation, sensing and robotics”