User avatar
JoelHinson
Posts: 90
Joined: Tue Dec 27, 2016 3:48 am

gpio/switch + python

Wed Jul 17, 2019 11:16 pm

i have a wire connected to 3V and one to appears GPIO17 but then the gpio_status.py program is reporting these to be pins 21 & 8,.. i do not see which naming convention this is "BCM, BOARD or wiringPi",.. does anyone know which naming convention does that particular program conform to? also here is my code and the program is throwing on the load and does not wait for the button press "momentary on" to occur. this is a program i posted online with an older unit and went back to in order to rebuild what i had and it isnt working the same for some reason,. does anyone see any problem with the initial state of the pin? or anything else, the wiring of it (see pic)??

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

run_once1 = 1
DEBUG = 1

while True:
    if run_once1 == 1:
        if GPIO.input(17) == GPIO.HIGH:
            run_once1 = 0
            print "17"
    continue
Attachments
IMG_20190717_180716.jpg
IMG_20190717_180716.jpg (138.72 KiB) Viewed 397 times

Andyroo
Posts: 3756
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: gpio/switch + python

Wed Jul 17, 2019 11:19 pm

The program uses BCM mode and is set by

Code: Select all

GPIO.setmode(GPIO.BCM)
See https://www.raspberrypi.org/documentation/usage/gpio/ for the package details.

This great site shows both sets of PIN numbers together https://pinout.xyz/
Need Pi spray - these things are breeding in my house...

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

Re: gpio/switch + python

Thu Jul 18, 2019 6:03 am

While you code does work and produces the 17 on the first button press as it should , with subsequent button presses producing no 17 as per the program.

you would normally use a 1 or True in place of the GPIO.HIGH in your if statement, and there is no need to use a continue at the end of the while True loop .

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

run_once1 = 1
DEBUG = 1

while True:
    if run_once1 == 1:
        if GPIO.input(17) == 1:
            run_once1 = 0
            print "17"
    
Your switch should be wired between gpio17 and a 3.3v pin

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
JoelHinson
Posts: 90
Joined: Tue Dec 27, 2016 3:48 am

Re: gpio/switch + python

Thu Jul 18, 2019 9:47 pm

switch is wired like that,.. but then which naming convention has those two as pin 8 and pin 21?? because you see the status program those are the two numbers receiving activity. you like the code without the "GPIO.HIGH"?? i will try it without,,.. python i havent much experience with and wouldnt have it like that if i had not seen it elsewhere (see pic (taken from tutorial website online)).
Attachments
IMG_20190718_180214.jpg
IMG_20190718_180214.jpg (104.46 KiB) Viewed 312 times
Last edited by JoelHinson on Thu Jul 18, 2019 11:03 pm, edited 1 time in total.

User avatar
davidcoton
Posts: 3929
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: gpio/switch + python

Thu Jul 18, 2019 10:02 pm

JoelHinson wrote:
Thu Jul 18, 2019 9:47 pm
you like the code without the "GPIO.HIGH"??
My personal preference is "GPIO.HIGH" rather than "1". It's more expressive, though in this context it's clear either way.

In a strongly typed language (modern versions of C++) "1" would give a compiler warning, because it could be a mistake. In other contexts that can be extremely useful, though this example is probably too trivial to matter. Python doesn't object, so using an integer instead of the strictly typed enumeration is permissible.
Signature retired

User avatar
JoelHinson
Posts: 90
Joined: Tue Dec 27, 2016 3:48 am

Re: gpio/switch + python

Fri Jul 19, 2019 2:44 am

so then its 6 of one and half dozen of the other??

it is still throwing the if statement return at the open of the program or not at all,.. which is not a button press exercise,.. i am checking out the gpio_status.py and he is using a ,..for loop over all of the pins,.. though it doesnt have "HIGH" or "LOW" has "EITHER EDGE" but that is ahead of the "do while" which is the iterative, the rest is all initialization. but then that is the only clear way to see that the circuit is good and the error is in the coding.

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

Re: gpio/switch + python

Fri Jul 19, 2019 6:53 am

JoelHinson wrote:
Thu Jul 18, 2019 9:47 pm
switch is wired like that,.. but then which naming convention has those two as pin 8 and pin 21?? because you see the status program those are the two numbers receiving activity. you like the code without the "GPIO.HIGH"?? i will try it without,,.. python i havent much experience with and wouldnt have it like that if i had not seen it elsewhere (see pic (taken from tutorial website online)).
Well for a start the 3.3v pin ( physical pin 1 ) will never show up on any status program because it is not a gpio pin but just a power pin.

in your switch example gpio17 ( physical pin 11 ) is the only one that going to change from an off to an on.

Image

I am guessing you are using a program that reports the gpio state using pigpio , in which case its would be using Broadcom numbering

http://abyz.me.uk/rpi/pigpio/

you would also need to rescan the gpio while the switch is pressed to see the change of start I suspect.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
JoelHinson
Posts: 90
Joined: Tue Dec 27, 2016 3:48 am

Re: gpio/switch + python

Sat Jul 20, 2019 7:35 pm

i see what you are saying, the numbering scheme in the program isnt broadcom or board rather the array cast in the program when removing the ground pins,.. is that what it is? are there any programming changes from a Pi2 to a Pi3 that would make a program that did work suddenly not work? because like i was saying the program is throwing the switch procedure without a press and upon running the program and on an older model it ran just fine,.. are there any changes that took place gpio-python? and if so where online to read about them?

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

Re: gpio/switch + python

Sat Jul 20, 2019 7:47 pm

As far as I am aware there are no changes in the gpio between pi2 and pi3, your code runs perfectly well on my pi3 as I said before ,

have you tried removing the switch to see if you code still runs and reports a switch press when there is no switch connected , that would tell you if it was a software or hardware error.

as you have only posted limited pictures of your set-up cant say if its wired correctly.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
davidcoton
Posts: 3929
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: gpio/switch + python

Sat Jul 20, 2019 8:58 pm

JoelHinson,

If you suspect a bug in your code, then you need not only to list it (as you have) but to describe very carefully:
  1. what you expect to happen, and
  2. what actually happens.
Then we may be able to work through it to find why the two descriptions differ, and how to bring b into line with a.
Signature retired

User avatar
JoelHinson
Posts: 90
Joined: Tue Dec 27, 2016 3:48 am

Re: gpio/switch + python

Sat Jul 20, 2019 10:24 pm

"b" i have as an insertion, "d" i have as an appendage.

a few years ago i built a thing,.. and the process was a study in this operating system and it was pretty involved so then i documented it here:

https://www.aguitarforum.com/threads/ra ... st-1525913

this is the whole of the program that used to work,. i am asking did we change some things around??,.. is there a new import to include? if you have a good eye at this,.. could you tell me why this is running and not waiting for button press.,.. its not the hardware or the circuit because the status program runs fine.

Code: Select all

import psutil
import RPi.GPIO as GPIO
import time
import os


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

run_once1 = 1
run_once2 = 1
run_once3 = 1
run_once4 = 1
run_once5 = 1
run_once6 = 1
run_once7 = 1
run_once8 = 1
DEBUG = 1
PROCNAME = 'puredata'



while True:
    if run_once1 == 1:
        if GPIO.input(17) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/backmask.pd &')
            run_once1 = 0
    if run_once2 == 1:
        if GPIO.input(27) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/chorus.pd &')
            run_once2 = 0
    if run_once3 == 1:
        if GPIO.input(26) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/delay.pd &')
            run_once3 = 0
    if run_once4 == 1:
        if GPIO.input(24) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/phaser.pd &')
            run_once4 = 0
    if run_once5 == 1:
        if GPIO.input(12) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/tremolo.pd &')
            run_once5 = 0
    if run_once6 == 1:
        if GPIO.input(16) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/vibrato.pd &')
            run_once6 = 0
    if run_once7 == 1:
        if GPIO.input(22) == GPIO.HIGH:
            os.system('puredata /home/pi/pd/whawha.pd &')
            run_once7 = 0
    if run_once8 == 1:
        if GPIO.input(25) == GPIO.HIGH:
            for proc in psutil.process_iter():
                    if proc.name() == PROCNAME:
                       proc.kill()
                       run_once1 = 1
                       run_once2 = 1
                       run_once3 = 1
                       run_once4 = 1
                       run_once5 = 1
                       run_once6 = 1
                       run_once7 = 1
    continue
    

User avatar
joan
Posts: 14069
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: gpio/switch + python

Sat Jul 20, 2019 10:35 pm

The only pin on the expansion header which has had a change of GPIO is pin 13. On the first Pi model that was connected to GPIO 21, on subsequent models it was connected to GPIO 27.

Return to “General discussion”