34

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.

  • 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 '12 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 Wilson Jul 17 '12 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 '12 at 1:59
  • @Malvineous: Ah, gotcha. – Emmaly Wilson Jul 18 '12 at 7:26
11

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

  • Can you clarify slightly? When the Raspberry Pi boots, the GPIO are set to a high-impedance, input state? – Alex Chamberlain Jul 18 '12 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) – John La Rooy Jul 18 '12 at 10:29
9

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.

  • 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. – John La Rooy Jul 18 '12 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? – Alex Chamberlain Jul 18 '12 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 Wilson Jul 18 '12 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 – John La Rooy Jul 18 '12 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 – John La Rooy Jul 18 '12 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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