39

What is the state of the GPIOs when power is applied? eg High, Low, Hi-Z?

Also does any OS change the state of any GPIOs when it loads? Hopefully not.

5
  • Unless it's in the datasheet, you'd likely have to measure it to be sure. Linux doesn't "know" about GPIOs so it wouldn't change them during boot up (unless you made a startup script that did), but there's always a chance the ROM might.
    – Malvineous
    Jul 13, 2012 at 11:30
  • @Malvineous, you're incorrect. Linux does know about GPIOs and support is built right into the kernel. Here's some documentation: kernel.org/doc/Documentation/gpio.txt
    – Emmaly
    Jul 17, 2012 at 22:11
  • 2
    @DustyWilson: Sorry if I was unclear, I meant the core Linux kernel doesn't make use of GPIOs during boot. It only makes them available for userspace and particular drivers. So the stock kernel shouldn't alter the state of GPIO pins during boot.
    – Malvineous
    Jul 18, 2012 at 1:59
  • @Malvineous: Ah, gotcha.
    – Emmaly
    Jul 18, 2012 at 7:26
  • Information from the future: raspberrypi.org/documentation/configuration/…
    – Sarien
    Apr 30, 2020 at 22:06

3 Answers 3

13

I have found the answer here thanks to russellstrong. I quote the relevant part below

I have jumped to conclusions about my pull up resistors / sensitivity of my circuit to the pull down resistors. The RPi is not setting the GPIO to output when first booted. It is turning on a pull down resistor for 740 milliseconds.

I have used two 18K resistors ( 3v3 -> GPIO -> GND ) to look at exactly what is going on with the pins. Here is the trace. Blue line is 3v3 power, yellow line is GPIO line.

enter image description here

2
  • Can you clarify slightly? When the Raspberry Pi boots, the GPIO are set to a high-impedance, input state? Jul 18, 2012 at 10:26
  • 1
    @AlexChamberlain, Looks like the broadcom chip starts up with pulldowns, but the loader clears the pulldowns so they will be high impedence (inputs) Jul 18, 2012 at 10:29
11

When started using the recommended Debian distro for RPi, GPIO is disabled. You have to enable each pin individually.

If you're doing it via /sys you will find "Paths in Sysfs" interesting (search within http://www.kernel.org/doc/Documentation/gpio.txt). In particular, you would be enabling a pin by "exporting" it. Any commands below assume you are running with root privileges (sudo or otherwise) or you have changed the permissions/ownership of the virtual files being modified.

echo 4 > /sys/class/gpio/export

This enables GPIO pin #4 which then causes /sys/class/gpio/gpio4 to exist, which contains several virtual files. Those files include "direction" which defines whether it's an input or an output pin, "value" which is either read-only for input or writable for output and contains the current value, and others.

echo out > /sys/class/gpio/gpio4/direction # set it as an output pin
echo 1 > /sys/class/gpio/gpio4/value # set the value to ON
echo 0 > /sys/class/gpio/gpio4/value # set the value to OFF
echo in > /sys/class/gpio/gpio4/direction # set it as input
cat /sys/class/gpio/gpio4/value # get the value
echo 4 > /sys/class/gpio/unexport # disables pin 4 and removes the gpio4 directory

Of course, you'll probably prefer to use some preexisting library to do GPIO supplied with or compatible with your language of choice. But if you're wanting something simple, you can just interface directly with sysfs to do very basic GPIO.

6
  • This is helpful. I looked at the datasheet but could not find the default state of the pins. This is significant if you are controlling things with the pins and don't want peripherals turning on while the os is loading. Jul 18, 2012 at 2:15
  • 3
    This is a great answer, but I'm not sure it really answers the question. What is the state of the GPIO straight after it was enabled? and direction set? Jul 18, 2012 at 6:33
  • @gnibbler: I believe you'll just need to test it. I tried it myself a bit ago and I saw nothing unexpected with regard to something flicking on or off. I don't know or promise that my experience is the expected one. I'd say you should just toss on a multimeter or such to the pins you want to test and then reboot or power-cycle the Pi many times to test. What I saw was that all pins were off, as if there was no power to the Pi whatsoever until I enabled each pin myself.
    – Emmaly
    Jul 18, 2012 at 7:30
  • @DustyWilson, perhaps they are set up as inputs with no pull ups or pull downs then. This means they are effectively floating if connected to a FET and would cause random behaviour if a pull up/down resistor is not added to the gate Jul 18, 2012 at 7:36
  • indeed... cat /sys/class/gpio/gpio4/direction says "in". I read in the datasheet that it's not possible to read the state of the pullups/pulldowns. I'll try to measure them Jul 18, 2012 at 7:39
0

GPIO 14 and GPIO 23 does go high for about 1 sec on reboot. GPIO 24 does not. PI3b+

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.