deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Problems with GPIO3/pin5

Tue Oct 29, 2019 4:28 pm

I'm trying to use GPIO3 (pin 5) on Raspbian Stretch Lite to shut down the RPi.
Please note that I'm running headless, i.e. nothing connected to HDMI.

In relation to these two threads:
https://www.raspberrypi.org/forums/view ... 8&t=254003
https://www.raspberrypi.org/forums/view ... 5&t=245054

I can't remember where I found the code, but it's an attempt to setup an event for detecting when GPIO3 (pin 5) is being connected to GND on pin 6.

Code: Select all

#!/bin/python
# Simple script for shutting down the raspberry Pi at the press of a button.
# by Inderpreet Singh

import RPi.GPIO as GPIO
import time
import os

# Use the Broadcom SOC Pin numbers
# Setup the Pin with Internal pullups enabled and PIN in reading mode.
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Our function on what to do when the button is pressed
def Shutdown(channel):
    os.system("sudo shutdown -h now")

# Add our function to execute when the button pressed event happens
GPIO.add_event_detect(3, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

# Now wait!
while 1:
    time.sleep(1)
The idea was to later add a call to "shutdown -h now".
But I get this error when I run the script with python3:

Code: Select all

pi@raspberrypi:~ $ python3 test.py
test.py:12: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
Traceback (most recent call last):
  File "shutdown.py", line 19, in <module>
    GPIO.add_event_detect(3, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
How can I detect changes to GPIO3?

/Mogens
Last edited by deepo on Tue Oct 29, 2019 4:59 pm, edited 1 time in total.

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

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 4:56 pm

What error? That is simply warning you that a pull-up is attached to that particular GPIO.

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 4:58 pm

Oh, I didn't get the whole error over - here it is:

Code: Select all

test.py:12: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
Traceback (most recent call last):
  File "shutdown.py", line 19, in <module>
    GPIO.add_event_detect(3, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
RuntimeError: Failed to add edge detection
/Mogens

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

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 5:25 pm

The same code works for me. It gives the warning but runs (I used control C to exit).

Not sure if that is a useful result or not. Perhaps see if there is an updated RPi.GPIO for your Pi.

Code: Select all

python q.py
q.py:12: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
q.py:12: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)

^CTraceback (most recent call last):
  File "q.py", line 23, in <module>
    time.sleep(1)
KeyboardInterrupt

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 7:54 pm

joan wrote:
Tue Oct 29, 2019 5:25 pm
The same code works for me. It gives the warning but runs (I used control C to exit).

Not sure if that is a useful result or not. Perhaps see if there is an updated RPi.GPIO for your Pi.

Code: Select all

python q.py
q.py:12: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
q.py:12: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)

^CTraceback (most recent call last):
  File "q.py", line 23, in <module>
    time.sleep(1)
KeyboardInterrupt
Does it work for you, if you run the RPi headless?
I have problems with the gpio-shutdown overlay, if I run headless. And now also Python GPIO.
I suspect that some IO stuff is somehow bad if I run headless.

/Mogens

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

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 8:44 pm

I pretty much always run headless. For that test I ssh'd in from a Linux lap-top.

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 9:00 pm

joan wrote:
Tue Oct 29, 2019 8:44 pm
I pretty much always run headless. For that test I ssh'd in from a Linux lap-top.
OK, thanks.
I already have the latest version installed.

Code: Select all

pi@raspberrypi:~ $ sudo apt-get update
...
pi@raspberrypi:~ $ sudo apt-get install python3-rpi.gpio
...
python3-rpi.gpio is already the newest version (0.6.5~stretch-1).
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
What version do you have?

/Mogens

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

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 9:18 pm

I was using a Pi4B which is buster and the version is 0.7.0.

Code: Select all

pi4b:~ $ python2
Python 2.7.16 (default, Apr  6 2019, 01:42:57) 
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO
>>> print(RPi.GPIO.VERSION)
0.7.0
>>> 
pi4b:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO
>>> print (RPi.GPIO.VERSION)
0.7.0
>>> 

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 9:48 pm

I flashed a card with Buster Lite, updated it and ran my code - and viola it almost worked... :)
It shutdown my RPi 3B+ immidiately, as if the button was pressed.
I'll test some more and get to know the RPi.GPIO library.
Thanks for your help!

/Mogens

User avatar
Milliways
Posts: 587
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: Problems with GPIO3/pin5

Tue Oct 29, 2019 11:55 pm

There is NO NEED for ANY code; gpio-shutdown is included in the kernel.

Code: Select all

Name:   gpio-shutdown
Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.
Load:   dtoverlay=gpio-shutdown,<param>=<val>
Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Wed Oct 30, 2019 3:50 pm

Milliways wrote:
Tue Oct 29, 2019 11:55 pm
There is NO NEED for ANY code; gpio-shutdown is included in the kernel.

Code: Select all

Name:   gpio-shutdown
Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.
Load:   dtoverlay=gpio-shutdown,<param>=<val>
Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.
You should have read my original thread about gpio-shutdown on Stretch.

It does not work when working headless, i.e. nothing in the HDMI connector.
And I still can't make it work on Buster either.
I need to connect a monitor to make it work.

/Mogens

User avatar
Milliways
Posts: 587
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: Problems with GPIO3/pin5

Wed Oct 30, 2019 11:56 pm

deepo wrote:
Wed Oct 30, 2019 3:50 pm
Milliways wrote:
Tue Oct 29, 2019 11:55 pm
There is NO NEED for ANY code; gpio-shutdown is included in the kernel.
You should have read my original thread about gpio-shutdown on Stretch.

It does not work when working headless, i.e. nothing in the HDMI connector.
And I still can't make it work on Buster either.
I need to connect a monitor to make it work.

/Mogens
I don't know what "my original thread" is, but this works perfectly on my various headless Pi - running Stretch or Buster.

Indeed there is little point if the Pi is NOT headless.

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Thu Oct 31, 2019 7:14 am

Milliways wrote:
Wed Oct 30, 2019 11:56 pm
deepo wrote:
Wed Oct 30, 2019 3:50 pm
Milliways wrote:
Tue Oct 29, 2019 11:55 pm
There is NO NEED for ANY code; gpio-shutdown is included in the kernel.
You should have read my original thread about gpio-shutdown on Stretch.

It does not work when working headless, i.e. nothing in the HDMI connector.
And I still can't make it work on Buster either.
I need to connect a monitor to make it work.

/Mogens
I don't know what "my original thread" is, but this works perfectly on my various headless Pi - running Stretch or Buster.

Indeed there is little point if the Pi is NOT headless.
Please see my first post in this thread.
There is a link to my original issue in another thread.

I'm glad it works for you, but it consistently does not work for me.
That's why I'm trying to find out what's wrong.

I can tell you are a little annoyed with me, but nobody gives any pointers to what log to look at, or other tools to try. There are just statements contrary to my findings. And everything I then try gets shot down with a famous Star Wars quote: "These are not the droids you are looking for" :)
You can probably tell I'm a little frustrated, but I don't know what to do to fix my problem.

/Mogens

User avatar
Milliways
Posts: 587
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: Problems with GPIO3/pin5

Thu Oct 31, 2019 7:55 am

deepo wrote:
Thu Oct 31, 2019 7:14 am

Please see my first post in this thread.
There is a link to my original issue in another thread.

I'm glad it works for you, but it consistently does not work for me.
That's why I'm trying to find out what's wrong.

I can tell you are a little annoyed with me, but nobody gives any pointers to what log to look at, or other tools to try. There are just statements contrary to my findings. And everything I then try gets shot down with a famous Star Wars quote: "These are not the droids you are looking for" :)
You can probably tell I'm a little frustrated, but I don't know what to do to fix my problem.

/Mogens
With respect the linked post doesn't indicate any problem. There is no overlay loaded in any of my Pi (because AFAIK the code is in the kernel).

Obviously you have done something which affects this, but as you have not provided any detail no one can say what.

PS I wouldn't use pin5 (GPIO3) because I use I²C - although it does work; I use pin 39/40 (gpio_pin=21) because it easy to use a 2 pin header.

deepo
Posts: 619
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Problems with GPIO3/pin5

Thu Oct 31, 2019 9:56 am

Milliways wrote:
Thu Oct 31, 2019 7:55 am
deepo wrote:
Thu Oct 31, 2019 7:14 am

Please see my first post in this thread.
There is a link to my original issue in another thread.

I'm glad it works for you, but it consistently does not work for me.
That's why I'm trying to find out what's wrong.

I can tell you are a little annoyed with me, but nobody gives any pointers to what log to look at, or other tools to try. There are just statements contrary to my findings. And everything I then try gets shot down with a famous Star Wars quote: "These are not the droids you are looking for" :)
You can probably tell I'm a little frustrated, but I don't know what to do to fix my problem.

/Mogens
With respect the linked post doesn't indicate any problem. There is no overlay loaded in any of my Pi (because AFAIK the code is in the kernel).

Obviously you have done something which affects this, but as you have not provided any detail no one can say what.

PS I wouldn't use pin5 (GPIO3) because I use I²C - although it does work; I use pin 39/40 (gpio_pin=21) because it easy to use a 2 pin header.
Yesterday I started with a clean Buster image, and added only the dtoverlay=gpio-shutdown line to /boot/config.txt
So I can't see what I have done to mess that up.
And for me it only works if I connect my RPi 3B+ to a HDMI monitor.

Can you point to documentation that states that gpio-shutdown is part of the kernel, and does not need a dtoverlay line in config.txt?
I'll try a new Buster image with altering config.txt tonight.

I would prefer not to use any other GPIO, as GPIO3 should be available as a general input, and I also ude GPIO3 to wake up the RPi again from halt.

/Mogens

Return to “Python”