r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

[SOLVED] GPIO help

Thu Jan 23, 2020 2:48 pm

so ive connected a button to GPIO 23 & GROUND and setup a python script to execute a shell script when button is pressed

problem is the button press isnt being recognized by the python script.

'sudo python xxxx.py' hangs terminal. heres a test script:

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
while True:
    if GPIO.input(23):
        os.system("sudo shutdown -h now")
    prev_input = input
    time.sleep(0.05)
Last edited by r4v3r23 on Fri Jan 24, 2020 6:39 pm, edited 2 times in total.

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPIO help

Thu Jan 23, 2020 6:40 pm

Please edit your post and place your code in code tags.

You should not need 'sudo' before 'python'. Replace your os.system with a print statement until it works.

Add a photo of your wiring. (Many post image to imgur.com and link into post).

Are you sure you've got the correct pin?

https://pinout.xyz/pinout/pin16_gpio23

User avatar
Burngate
Posts: 6290
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: GPIO help

Thu Jan 23, 2020 6:41 pm

r4v3r23 wrote:
Thu Jan 23, 2020 2:48 pm

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
while True:
    if GPIO.input(23):
        os.system("sudo shutdown -h now")
    prev_input = input
    time.sleep(0.05)
(put that in code tags for you, as the forum removes leading indents)

You haven't mentioned any pull-ups.
If there's no pull-up resistor in your circuit, and you've not enabled the internal pull-up, the GPIO will never rise.

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Thu Jan 23, 2020 6:47 pm

Burngate wrote:
Thu Jan 23, 2020 6:41 pm
r4v3r23 wrote:
Thu Jan 23, 2020 2:48 pm

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
while True:
    if GPIO.input(23):
        os.system("sudo shutdown -h now")
    prev_input = input
    time.sleep(0.05)
(put that in code tags for you, as the forum removes leading indents)

You haven't mentioned any pull-ups.
If there's no pull-up resistor in your circuit, and you've not enabled the internal pull-up, the GPIO will never rise.
heres another test scripts that gives me the same hanging result:

Code: Select all

import RPi.GPIO as GPIO  
import time  
import os  
 
GPIO.setmode(GPIO.BCM)  
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)  

def Shutdown(channel):  
    os.system("sudo shutdown -h now")  
 
GPIO.add_event_detect(23, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)  
 
while 1:  
    time.sleep(1)

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Thu Jan 23, 2020 6:49 pm

rpiMike wrote:
Thu Jan 23, 2020 6:40 pm
Please edit your post and place your code in code tags.

You should not need 'sudo' before 'python'. Replace your os.system with a print statement until it works.

Add a photo of your wiring. (Many post image to imgur.com and link into post).

Are you sure you've got the correct pin?

https://pinout.xyz/pinout/pin16_gpio23
without sudo i get:

Traceback (most recent call last):
File "pi3.py", line 12, in <module>
GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!

User avatar
B.Goode
Posts: 10191
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: GPIO help

Thu Jan 23, 2020 6:57 pm

heres another test scripts that gives me the same hanging result:



Meaning it runs and awaits a button press which it never detects?


Where, specifically, is your button/switch connected?

(As already suggested, an in-focus photo might help... )

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Thu Jan 23, 2020 7:14 pm

B.Goode wrote:
Thu Jan 23, 2020 6:57 pm
heres another test scripts that gives me the same hanging result:



Meaning it runs and awaits a button press which it never detects?


Where, specifically, is your button/switch connected?

(As already suggested, an in-focus photo might help... )
imgur.com/a/cjVf5PY

yes, it never detects the button press. here Ive moved it to Pin 18 to match this guide: https://www.element14.com/community/doc ... berry-pi-b

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPIO help

Thu Jan 23, 2020 10:19 pm

If your pulling a GPIO input up, the input will normally be High and you need to test for Low:

Code: Select all

import RPi.GPIO as GPIO,time,os
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if GPIO.input(24) == GPIO.LOW:
        print('pressed')
    time.sleep(0.05)
This tests fine for me.

python3 test.py

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Thu Jan 23, 2020 10:44 pm

rpiMike wrote:
Thu Jan 23, 2020 10:19 pm
If your pulling a GPIO input up, the input will normally be High and you need to test for Low:

Code: Select all

import RPi.GPIO as GPIO,time,os
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if GPIO.input(24) == GPIO.LOW:
        print('pressed')
    time.sleep(0.05)
This tests fine for me.

python3 test.py
$ python3 pi.py

Traceback (most recent call last):
File "pi.py", line 3, in <module>
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!

sudo hangs (waits for button press)

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

Re: GPIO help

Thu Jan 23, 2020 10:51 pm

You need to post the code you are running and the traceback.

The photo you posted seems to show a connection to pins 18 and 20 which are GPIO 24 and ground.

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPIO help

Thu Jan 23, 2020 11:10 pm

r4v3r23 wrote:
Thu Jan 23, 2020 10:44 pm
rpiMike wrote:
Thu Jan 23, 2020 10:19 pm
If your pulling a GPIO input up, the input will normally be High and you need to test for Low:

Code: Select all

import RPi.GPIO as GPIO,time,os
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if GPIO.input(24) == GPIO.LOW:
        print('pressed')
    time.sleep(0.05)
This tests fine for me.

python3 test.py
$ python3 pi.py

Traceback (most recent call last):
File "pi.py", line 3, in <module>
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!

sudo hangs (waits for button press)
Try running my code. As joan mentioned physical pin 18 is BCM GPIO 24.

Is your Raspbian Buster fully updated?

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Thu Jan 23, 2020 11:32 pm

rpiMike wrote:
Thu Jan 23, 2020 11:10 pm
r4v3r23 wrote:
Thu Jan 23, 2020 10:44 pm
rpiMike wrote:
Thu Jan 23, 2020 10:19 pm
If your pulling a GPIO input up, the input will normally be High and you need to test for Low:

Code: Select all

import RPi.GPIO as GPIO,time,os
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if GPIO.input(24) == GPIO.LOW:
        print('pressed')
    time.sleep(0.05)
This tests fine for me.

python3 test.py
$ python3 pi.py

Traceback (most recent call last):
File "pi.py", line 3, in <module>
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!

sudo hangs (waits for button press)
Try running my code. As joan mentioned physical pin 18 is BCM GPIO 24.

Is your Raspbian Buster fully updated?
this is running your code. im running Manjaro Minimal & installed rpi-gpio packages

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: GPIO help

Fri Jan 24, 2020 12:22 am

r4v3r23 wrote:
Thu Jan 23, 2020 6:49 pm
rpiMike wrote:
Thu Jan 23, 2020 6:40 pm
Please edit your post and place your code in code tags.

You should not need 'sudo' before 'python'. Replace your os.system with a print statement until it works.

Add a photo of your wiring. (Many post image to imgur.com and link into post).

Are you sure you've got the correct pin?

https://pinout.xyz/pinout/pin16_gpio23
without sudo i get:

Traceback (most recent call last):
File "pi3.py", line 12, in <module>
GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!
What model of Raspberry are you using? What OS? What version of RPi.GPIO?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Fri Jan 24, 2020 12:32 am

DougieLawson wrote:
Fri Jan 24, 2020 12:22 am
r4v3r23 wrote:
Thu Jan 23, 2020 6:49 pm
rpiMike wrote:
Thu Jan 23, 2020 6:40 pm
Please edit your post and place your code in code tags.

You should not need 'sudo' before 'python'. Replace your os.system with a print statement until it works.

Add a photo of your wiring. (Many post image to imgur.com and link into post).

Are you sure you've got the correct pin?

https://pinout.xyz/pinout/pin16_gpio23
without sudo i get:

Traceback (most recent call last):
File "pi3.py", line 12, in <module>
GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!
What model of Raspberry are you using? What OS? What version of RPi.GPIO?
Raspberry 4 (4GB), Manjaro minimal, RPi.GPIO 0.7.0-1

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: GPIO help

Fri Jan 24, 2020 12:51 am

RPi.GPIO is only supported on Raspbian. You'll need to pull the source package and hack it to work on your alien operating system.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Fri Jan 24, 2020 1:08 am

DougieLawson wrote:
Fri Jan 24, 2020 12:51 am
RPi.GPIO is only supported on Raspbian. You'll need to pull the source package and hack it to work on your alien operating system.
it already is maintained on manjaro

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPIO help

Fri Jan 24, 2020 7:14 am

r4v3r23 wrote:
Thu Jan 23, 2020 11:32 pm
rpiMike wrote:
Thu Jan 23, 2020 11:10 pm
r4v3r23 wrote:
Thu Jan 23, 2020 10:44 pm


$ python3 pi.py

Traceback (most recent call last):
File "pi.py", line 3, in <module>
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
RuntimeError: Not running on a RPi!

sudo hangs (waits for button press)
Try running my code. As joan mentioned physical pin 18 is BCM GPIO 24.

Is your Raspbian Buster fully updated?
this is running your code. im running Manjaro Minimal & installed rpi-gpio packages
From the error message it looks like you changed 24 to 18. Do you understand the difference between physical pin numbers (BOARD) and BCM GPIO pin numbers. https://pinout.xyz/

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Fri Jan 24, 2020 12:12 pm

rpiMike wrote:
Fri Jan 24, 2020 7:14 am
r4v3r23 wrote:
Thu Jan 23, 2020 11:32 pm
rpiMike wrote:
Thu Jan 23, 2020 11:10 pm


Try running my code. As joan mentioned physical pin 18 is BCM GPIO 24.

Is your Raspbian Buster fully updated?
this is running your code. im running Manjaro Minimal & installed rpi-gpio packages
From the error message it looks like you changed 24 to 18. Do you understand the difference between physical pin numbers (BOARD) and BCM GPIO pin numbers. https://pinout.xyz/
changed to 24, get this output:

pressed
pressed
pressed
pressed
pressed

ad nauseum

User avatar
B.Goode
Posts: 10191
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: GPIO help

Fri Jan 24, 2020 12:19 pm

A suggestion for diagnostic/troubleshooting purposes -

Do you get the same results if you run the Raspberry Pi recommended Raspbian Operating System on your RPi board?

(Not saying your choice of Operating System is wrong, but Raspbian is probably the OS used by most volunteer helpers in these forums, and this will ensure that advice us relevant and accurate.)

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPIO help

Fri Jan 24, 2020 12:42 pm

r4v3r23 wrote:
Fri Jan 24, 2020 12:12 pm
rpiMike wrote:
Fri Jan 24, 2020 7:14 am
r4v3r23 wrote:
Thu Jan 23, 2020 11:32 pm


this is running your code. im running Manjaro Minimal & installed rpi-gpio packages
From the error message it looks like you changed 24 to 18. Do you understand the difference between physical pin numbers (BOARD) and BCM GPIO pin numbers. https://pinout.xyz/
changed to 24, get this output:

pressed
pressed
pressed
pressed
pressed

ad nauseum
Is that with your button connected between GPIO24 and Ground? Is your button push-to-close or push-to-open? As others have mentioned I suggest you use Raspbian Buster.

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

Re: GPIO help

Fri Jan 24, 2020 12:49 pm

We don't know what code is being used. We don't know how the switch is connected. The question is pointless.

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Fri Jan 24, 2020 12:59 pm

rpiMike wrote:
Fri Jan 24, 2020 12:42 pm
r4v3r23 wrote:
Fri Jan 24, 2020 12:12 pm
rpiMike wrote:
Fri Jan 24, 2020 7:14 am


From the error message it looks like you changed 24 to 18. Do you understand the difference between physical pin numbers (BOARD) and BCM GPIO pin numbers. https://pinout.xyz/
changed to 24, get this output:

pressed
pressed
pressed
pressed
pressed

ad nauseum
Is that with your button connected between GPIO24 and Ground? Is your button push-to-close or push-to-open? As others have mentioned I suggest you use Raspbian Buster.
yes GPIO24 (red) & Ground (black) as per this pic: https://imgur.com/a/cjVf5PY

how should the script be responding?

cant use raspian buster this has to work on manjaro

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Fri Jan 24, 2020 1:06 pm

joan wrote:
Fri Jan 24, 2020 12:49 pm
We don't know what code is being used. We don't know how the switch is connected. The question is pointless.
both a photo of the connection & the code used are in this thread

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

Re: GPIO help

Fri Jan 24, 2020 1:38 pm

Get it saying "Pressed" repeatedly. Then press and hold the button. Any change?
Signature retired

r4v3r23
Posts: 24
Joined: Thu Jan 23, 2020 2:40 pm

Re: GPIO help

Fri Jan 24, 2020 2:01 pm

davidcoton wrote:
Fri Jan 24, 2020 1:38 pm
Get it saying "Pressed" repeatedly. Then press and hold the button. Any change?
No change it keeps printing "pressed"

Return to “Python”