PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Default GPIO output

Mon Mar 11, 2013 5:18 pm

Hi,

If nothing is hooked up to a GPIO pin, does the OS read it as HIGH or LOW. I am trying to run a python script to shut down the Pi safely when a GPIO is set to high. With no GPIO pins connected, pin 2 is reading HIGH. Is this the normal behavior?

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
GPIO.setup(23, GPIO.OUT)
print("Shutdown script started. Wait for shutdown request")
while True:
  GPIO.output(23, GPIO.HIGH) // sent HIGH to arduino - lets arduino know PI is running
  if(GPIO.input(2)):
     print("Request from button to shutdown")
     os.system("sudo shutdown -h now")
     break
time.sleep(1)


User avatar
RaTTuS
Posts: 10774
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Default GPIO output

Mon Mar 11, 2013 5:21 pm

it will float if it has not been set so could be read as high or low IIRC
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: Default GPIO output

Mon Mar 11, 2013 5:28 pm

RaTTuS wrote:it will float if it has not been set so could be read as high or low IIRC
How do I set the Pin LOW, when it is setup for IN?

User avatar
jojopi
Posts: 3426
Joined: Tue Oct 11, 2011 8:38 pm

Re: Default GPIO output

Mon Mar 11, 2013 5:49 pm

The SoC has the facility to enable a weak pull-up or pull-down on any GPIO configured as an input. This is equivalent to connecting the pin to 3.3V or 0V with a resistor of around 50kΩ, and will hold the pin at the required level until pulled more strongly by an external circuit. To enable this with RPi.GPIO:

Code: Select all

GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
However, GPIO2/3 (or 0/1 on Revision1 boards) have physical 1.8kΩ pull-up resistors soldered on the board, for use with I²C. A weak pull-down will not overcome this, so these two GPIOs always read high when not connected.

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

Re: Default GPIO output

Mon Mar 11, 2013 7:27 pm

jojopi wrote:The SoC has the facility to enable a weak pull-up or pull-down on any GPIO configured as an input. This is equivalent to connecting the pin to 3.3V or 0V with a resistor of around 50kΩ, and will hold the pin at the required level until pulled more strongly by an external circuit. To enable this with RPi.GPIO:

Code: Select all

GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
However, GPIO2/3 (or 0/1 on Revision1 boards) have physical 1.8kΩ pull-up resistors soldered on the board, for use with I²C. A weak pull-down will not overcome this, so these two GPIOs always read high when not connected.
Not (always) true. As of the foundations 3.6 kernel release BCM_GPIO pins 0 and 1 are set to outpus and driven low. So if you have a Revision 1 board then that trick isn't advisable anymore )-:

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
jojopi
Posts: 3426
Joined: Tue Oct 11, 2011 8:38 pm

Re: Default GPIO output

Mon Mar 11, 2013 8:52 pm

gordon@drogon.net wrote:Not (always) true. As of the foundations 3.6 kernel release BCM_GPIO pins 0 and 1 are set to outpus and driven low. So if you have a Revision 1 board then that trick isn't advisable anymore )-:
I did not intend to suggest that GPIO0/1/2/3 would be high even when driven low, just that they cannot be weakly pulled down. I am not sure what trick you mean, or whether it was ever advisable.

Also, I have a Rev1 board running 3.6.11+ #371 kernel, and GPIO0/1 are not driven low at any time during boot.

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

Re: Default GPIO output

Mon Mar 11, 2013 9:21 pm

jojopi wrote: Also, I have a Rev1 board running 3.6.11+ #371 kernel, and GPIO0/1 are not driven low at any time during boot.
Hm. Now you've got me there.

When I first got the 3.6 release something set GPIO pins 0 & 1 to outputs and set them low. I checked this on a couple of boards - I noticed it immediately as I had a ladder board pluged into one and normally on a reboot the small LEDs come on (dimly) - they're on BCM_GPIO 0 & 1... they were off. I even posted about it: http://www.raspberrypi.org/phpBB3/viewt ... 24&t=35275

Oh well. Possibly another update since then has reverted that change.

As you were.

-Gordon
--
Gordons projects: https://projects.drogon.net/

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: Default GPIO output

Tue Mar 12, 2013 12:17 am

Thanks guys for the help.

Return to “General programming discussion”