Page 1 of 1

Very basic siwtch & python?

Posted: Thu Feb 28, 2013 10:34 pm
by Tom Lewis
Hello!
Im new to python, coming from PHP so i hardly understand it.
I have a very basic "launch switch" attached to GPIO 23 & GPIO GND and am running this python.

Code: Select all

import time
import RPi.GPIO as io

io.setmode(io.BCM)
door_pin = 23
io.setup(door_pin, io.IN)
counter = 0

while True:
    if io.input(door_pin):
        counter += 1
        print "Switch:", counter
    time.sleep(0.5)
But it works in reverse, when the switch is off or disconnected the "switch + number" clocks, but when I activate the switch it stops, is this a problem in my python? I have tried switching cables around.
Another slight problem, is that it does not tick smoothly, as in a get a response every half second or so for about 11 ticks, then it pauses, then another 11 or so ticks then it pauses...

Re: Very basic siwtch & python?

Posted: Fri Mar 01, 2013 1:48 am
by jackokring
So turn your switch upside down. :D

It looks like you are switching the grounding on! And not the 3.3v.
The nature of linux requires you to renice your python script with a negative priority maybe.

Code: Select all

man renice

Re: Very basic siwtch & python?

Posted: Fri Mar 01, 2013 3:11 am
by jazzy
could a switch and code set up like this give you the ability to have the switch trigger a script to

Code: Select all

sudo shutdown -h now

(sorry dont mean to thread hi jack, just a quick question)

Re: Very basic siwtch & python?

Posted: Fri Mar 01, 2013 10:32 am
by Tom Lewis
jazzy wrote:could a switch and code set up like this give you the ability to have the switch trigger a script to

Code: Select all

sudo shutdown -h now
Yeah You could run any script you wanted :)

I wish it was as simple as turning the switch upside down, problem being this is my switch (missile switch)
Image

The original code was pulled form adafruits sensing movements, which would have been a bid switch which is constantly on, so i presumed it was the python.
http://learn.adafruit.com/adafruits-ras ... t/software

I'll look into that renice

Re: Very basic siwtch & python?

Posted: Fri Mar 01, 2013 10:57 am
by gordon@drogon.net
Tom Lewis wrote:Hello!
Im new to python, coming from PHP so i hardly understand it.
I have a very basic "launch switch" attached to GPIO 23 & GPIO GND and am running this python.

Code: Select all

import time
import RPi.GPIO as io

io.setmode(io.BCM)
door_pin = 23
io.setup(door_pin, io.IN)
counter = 0

while True:
    if io.input(door_pin):
        counter += 1
        print "Switch:", counter
    time.sleep(0.5)
But it works in reverse, when the switch is off or disconnected the "switch + number" clocks, but when I activate the switch it stops, is this a problem in my python? I have tried switching cables around.
Another slight problem, is that it does not tick smoothly, as in a get a response every half second or so for about 11 ticks, then it pauses, then another 11 or so ticks then it pauses...
It works in reverse because ....

You're pulling the pin to ground - therefore the pin will read 0 when you push the switch. When the switch isn't pushed it's reading one. Or it should be - you didn't give the full circuit details, but I hope you have either enabled the on-board pull-up resistor or conntected an external (e.g. 10K) resistor from the pin ot +3.3v. If you do not do this, then you will get random inputs when the switch is open.

So - the switch is reading an inverted signal - all you need to do is change your code. I am not a Python programmer, but instead of

if io.input(door_pin):

you need to test for the opposite. If this works in Python then:

if !op.input(door.pin):

might work, or however you check for "not"..

But do make sure there is a pull-up resistor on that pin, however you do it.

-Gordon

Re: Very basic siwtch & python?

Posted: Fri Mar 01, 2013 2:52 pm
by -rst-
To complete Gordon's expert advice...

Python uses the good old English 'not', so:

Code: Select all

if not op.input(door.pin):
...feel free to add parenthesis according to preference/tradition/'company policy'/... ;)

Re: Very basic siwtch & python?

Posted: Sat Mar 02, 2013 3:52 pm
by Tom Lewis
Such a simple fix, im currently not using a resistor but I'll throw one in now and add that "not" in, I really need to learn python!
Thanks for the proper explanation of what's going on Gordon, It just happens that we have talked before on your blog comments section, you helped me set-up a 16x2 character display!

Thanks again!

Re: Very basic siwtch & python?

Posted: Sat Mar 02, 2013 4:38 pm
by Tom Lewis
This does not work, It just runs on and off temperamental even if i take the switch out of the breadboard all together there is no way the pi can make a circuit between ground and 23

Re: Very basic siwtch & python?

Posted: Sat Mar 02, 2013 5:10 pm
by gordon@drogon.net
Tom Lewis wrote:This does not work, It just runs on and off temperamental even if i take the switch out of the breadboard all together there is no way the pi can make a circuit between ground and 23
Make sure we're all talking about the same '23' here...

So from the command-line:

gpio -g mode 23 in
gpio -g mode 23 up # enable internal pull-up
gpio -g read 23

and then repeat the gpio -g read 23 with the switch open and closed.

Pin 23 here - I'm assuming BCM_GPIO pin 23 - (it's wiringPi pin 4) and it's physical pin 16 on the GPIO connector - this is on the outside row, near the middle.

If you're using physical pin 23, then it's BCM_GPIO 11 (or wiringPi 14) and is on the inside row near the composite video end.

-Gordon