Tom Lewis
Posts: 22
Joined: Mon Jul 02, 2012 12:14 pm

Very basic siwtch & python?

Thu Feb 28, 2013 10:34 pm

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...

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: Very basic siwtch & python?

Fri Mar 01, 2013 1:48 am

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
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

jazzy
Posts: 11
Joined: Tue Feb 26, 2013 12:06 am

Re: Very basic siwtch & python?

Fri Mar 01, 2013 3:11 am

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)

Tom Lewis
Posts: 22
Joined: Mon Jul 02, 2012 12:14 pm

Re: Very basic siwtch & python?

Fri Mar 01, 2013 10:32 am

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

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

Re: Very basic siwtch & python?

Fri Mar 01, 2013 10:57 am

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
--
Gordons projects: https://projects.drogon.net/

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Very basic siwtch & python?

Fri Mar 01, 2013 2:52 pm

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'/... ;)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Tom Lewis
Posts: 22
Joined: Mon Jul 02, 2012 12:14 pm

Re: Very basic siwtch & python?

Sat Mar 02, 2013 3:52 pm

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!

Tom Lewis
Posts: 22
Joined: Mon Jul 02, 2012 12:14 pm

Re: Very basic siwtch & python?

Sat Mar 02, 2013 4:38 pm

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

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

Re: Very basic siwtch & python?

Sat Mar 02, 2013 5:10 pm

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
--
Gordons projects: https://projects.drogon.net/

Return to “General programming discussion”