GPIO(21st October 2012)

NAME
gpio - Command-line access to Raspberry Pi and PiFace GPIO

SYNOPSIS
gpio -v

gpio [ -g ] read/write/wb/pwm/clock/mode ...

gpio [ -p ] read/write/wb ...

gpio readall

gpio unexportall/exports

gpio export/edge/unexport ...

gpio drive group value

gpio pwm-bal/pwm-ms

gpio pwmr range

gpio load i2c/spi ...

gpio gbr channel

gpio gbw channel value

DESCRIPTION

GPIO is a swiss army knife of a command line tool to allow the user easy access to the GPIO pins on the Raspberry Pi and the SPI A/D and D/A converters on the Gertboard. It's designed for simple testing and diagnostic purposes, but can be used in shell scripts for general if somewhat slow control of the GPIO pins.

It can also control the IO's on the PiFace IO board and load the SPI and I2C kernel modules if required.

Additionally, it can be used to set the exports in the /sys/class/gpio system directory to allow subsequent programs to use the /sys/class/gpio interface without needing to be run as root.

OPTIONS

-v  Output the current version including the board revision of the Raspberry Pi.

-g  Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. NOTE: The BCM_GPIO pin numbers are always used with the export and edge commands.

-p  Use the PiFace interface board and its corresponding pin numbers.

read <pin>
    Read the digital value of the given pin and print 0 or 1 to represent the respective logic levels.

write <pin> <value>
    Write the given value (0 or 1) to the pin. You need to set the pin to output mode first.

wb <value>
    Write the given byte to the 8 main GPIO pins. You can prefix it with 0x to specify a hexadecimal number. You need to set pins to output mode first.

readall
    Output a table of all GPIO pins values. The values represent the actual values read if the pin is in input mode, or the last value written if the pin is in output mode.

pwm <pin> <value>
    Write a PWM value (0-1023) to the given pin. The pin needs to be put into PWM mode first.

clock <pin> <frequency>
    Set the output frequency on the given pin. The pin needs to be put into clock mode first.

mode <pin> <mode>
    Set a pin into input, output or pwm mode. Can also use the literals up, down or tri to set the internal pull-up, pull-down or tristate (off) controls.

unexportall
    Un-Export all the GPIO pins in the /sys/class/gpio directory.

exports
    Print a list (if any) of all the exported GPIO pins and their current values.

export
    Export a GPIO pin in the /sys/class/gpio directory. Use like the mode command above however only in and out are supported at this time. Note that the pin number is the BCM_GPIO number and not the wiringPi number.

    Once a GPIO pin has been exported, the gpio program changes the ownership of the /sys/class/gpio/gpioX/value and if present in later kernels, the /sys/class/gpio/gpioX/edge pseudo files to that of the user running the gpio program. This means that you can have a small script of gpio exports to setup the gpio pins as your program requires without the need to run anything as root, or with the sudo command.

edge
    This exports a GPIO pin in the /sys/class/gpio directory, set the direction to input and set the edge interrupt method to none, rising, falling or both. Use like the export command above and note that BCM_GPIO pin number is used not not wiringPi pin numbering.

    Like the export commands above, ownership is set to that of the calling user, allowing subsequent access from user programs without requiring root/sudo.

unexport
    Un-Export a GPIO pin in the /sys/class/gpio directory.

drive
    group value

    Change the pad driver value for the given pad group to the supplied drive value. Group is 0, 1 or 2 and value is 0-7. Do not use unless you are absolutely sure you know what you're doing.

pwm-bal/pwm-ms
    Change the PWM mode to balanced (the default) or mark:space ratio (traditional)

pwmr
    Change the PWM range register. The default is 1024.

load i2c [baudrate]
    This loads the i2c or drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to them. Optionally it will set the I2C baudrate to that supplied (or as close as the Pi can manage) The default speed is 100Kb/sec.

load spi [buffer size in KB]
    This loads the spi drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to them. Optionally it will set the SPI buffer size to that supplied. The default is 4KB.

gbr channel

    This reads the analog to digital converter on the Gertboard on the given channel. The board jumpers need to be in-place to do this operation.

gbw channel value

    This writes the supplied value to the output channel on the Gertboards SPI digital to analogue converter. The board jumpers need to be in-place to do this operation.

WiringPi vs. BCM_GPIO Pin numbering

WiringPi | GPIO-r1 | GPIO-r2 | Function
0 | 17 | 17
1 | 18 | 18 | (PWM)
2 | 21 | 27
3 | 22 | 22
4 | 23 | 23
5 | 24 | 24
6 | 25 | 25
7 | 4 | 4
8 | 0 | 2 | I2C: SDA0
9 | 1 | 3 | I2C: SCL0
10 | 8 | 8 | SPI: CE0
11 | 7 | 7 | SPI: CE1
12 | 10 | 10 | SPI: MOSI
13 | 9 | 9 | SPI: MISO
14 | 11 | 11 | SPI: SCLK
15 | 14 | 14 | TxD
16 | 15 | 16 | RxD
17 | - | 28
18 | - | 29
19 | - | 30
20 | - | 31

Note that "r1" and "r2" above refers to the board revision. Normally wiringPi detects the correct board revision with use for it's own numbering scheme, but if you are using a Revision 2 board with some of the pins which change numbers between revisions you will need to alter your software.

FILES

gpio
    executable

EXAMPLES

gpio mode 4 output # Set pin 4 to output

gpio -g mode 23 output # Set GPIO pin 23 to output (same as WiringPi pin 4)

gpio mode 1 pwm # Set pin 1 to PWM mode

gpio pwm 1 512 # Set pin 1 to PWM value 512 - half brightness

gpio export 17 out # Set GPIO Pin 17 to output

gpio export 0 in # Set GPIO Pin 0 (SDA0) to input.

gpio -g read 0 # Read GPIO Pin 0 (SDA0)

NOTES

When using the export, edge or unexport commands, the pin numbers are always native BCM_GPIO numbers and never wiringPi pin numbers.

SEE ALSO

WiringPi's home page

    https://projects.drogon.net/raspberry-pi/wiringpi/

AUTHOR

Gordon Henderson

REPORTING BUGS

Please report bugs to <projects@drogon.net>

COPYRIGHT

Copyright (c) 2012 Gordon Henderson This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

TRADEMARKS AND ACKNOWLEDGEMENTS

Raspberry Pi is a trademark of the Raspberry Pi Foundation. See http://raspberrypi.org/ for full details.