Advertisement
Tritonio

The GPIO utility WiringPi

Mar 15th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1.  
  2.  
  3. WiringPi comes with a separate program to help manage the GPIO. This program, called gpio, can also be used in scripts to manipulate the GPIO pins – set outputs and read inputs. It’s even possible to write entire programs just using the gpio command in a shell-script, although it’s not terribly efficient doing it that way… Another way to call it is using the system() function in C/C++ or it’s equivalent in other programming languages.
  4.  
  5. The gpio command is designed to be installed as a setuid program and called by a normal user without using the sudo command (or logging in as root).
  6.  
  7. In addition to using the gpio utility to control the GPIO pins, you can:
  8.  
  9. Export/Unexport pins via the /sys/class/gpio interface, where they will then be available to user programs (that then do not need to be run as root or with sudo)
  10. Export pins to enable edge-triggered interrupts via the /sys/class/gpio interface.
  11. Control the pins on the PiFace peripheral device.
  12. Load SPI and I2C modules and set /dev/ permissions to enable read/write by the user running the gpio program.
  13. Set the SPI buffer size and I2C baud rate (when loading the modules)
  14. Output values to the Gertboard DAC
  15. Read inputs from the Gertboard ADC
  16. Determine your Raspberry Pi board hardware revision.
  17.  
  18. See the man page for the gpio program to see what all the features are by typing
  19.  
  20. man gpio
  21.  
  22. at the command prompt.
  23. Usage
  24.  
  25. From the Linux command line:
  26.  
  27. gpio -v
  28.  
  29. This prints the version.
  30.  
  31. gpio -g …
  32.  
  33. The optional -g flag causes pin numbers to be interpreted as BCM_GPIO pin numbers rather than standard wiringPi pin numbers.
  34. Standard input and output commands
  35.  
  36. gpio [-g] mode <pin> in/out/pwm/up/down/tri
  37.  
  38. This sets the mode of a pin to be input, output or pwm and additionally can set the internal pull-up/down resistors to pull-up, pull-down or none.
  39.  
  40. gpio [-g] write <pin> 0/1
  41.  
  42. This sets an output pin to high (1) or low (0)
  43.  
  44. gpio [-g] pwm <pin> <value>
  45.  
  46. Set the pin to a PWM value (0-1023 is supported)
  47.  
  48. gpio [-g] read <pin>
  49.  
  50. Reads and prints the logic value of the given pin. It will print 0 (low) or 1 (high).
  51.  
  52. gpio readall
  53.  
  54. This reads all the normally accessible pins and prints a table of their numbers (both wiringPi and BCM_GPIO, so makes for a handy cross-reference chart), along with their modes and current values.
  55. Module Load Commands
  56.  
  57. gpio load spi [buffer size in KB]
  58.  
  59. This loads the SPI kernel modules and optionally sets the internal buffer to the given size in KB (multiples of 1024). The default is 4KB and is usually more than enough for most application which only exchange a byte or 2 at a time over the SPI bus.
  60.  
  61. The /dev/spi* entries are set to be owned by the person using the gpio program, so there is no need to run subsequent programs as root (unless they use other wiringPi functions)
  62.  
  63. gpio load i2c [baud rate in Kb/sec]
  64.  
  65. This loads the I2C kernel modules and optionally sets the baud rate to the given speed in Kb/sec (multiples of 1000). The default is 100Kb/sec.
  66.  
  67. The /dev/I2c* entries are set to be owned by the person using the gpio program, so there is no need to run subsequent programs as root (unless they use other wiringPi functions)
  68. /sys/class/gpio mode commands
  69.  
  70. gpio export <pin> in/out
  71.  
  72. This exports the given pin (BCM-GPIO pin number) as an input or output and makes it available for a user program running as the same user to use.
  73.  
  74. gpio unexport <pin>
  75.  
  76. Removes the export of the given pin.
  77.  
  78. gpio unexportall
  79.  
  80. Removes all /sys/class/gpio exports.
  81.  
  82. gpio exports
  83.  
  84. This prints a list of all gpio pins which have been exported via the /sys/class/gpio interface and their modes.
  85.  
  86. gpio edge <pin> rising/falling/both/none
  87.  
  88. This enables the given pin for edge interrupt triggering on the rising, falling or both edges. (Or none which disables it)
  89.  
  90. Note: The pin numbers in the sys mode are always BCM-GPIO pin numbers.
  91. Examples
  92.  
  93. gpio mode 0 out
  94. gpio write 0 1
  95.  
  96. This uses the wiringPi pin numbers to set pin 0 as an output and then sets the pin to a logic 1.
  97.  
  98. gpio -g mode 0 in
  99. gpio -g read 0
  100.  
  101. This uses the BCM_GPIO pin numbering scheme and reads pin 0 (SDA0 on a Rev. 1 Raspberry Pi)
  102. Internal pull up/down resistors
  103.  
  104. The GPIO lines have internal pull up or pull-down resistors which can be controlled via software when a pin is in input mode.
  105.  
  106. gpio mode 0 up
  107. gpio mode 0 down
  108. gpio mode 0 tri
  109.  
  110. These set the resistors to pull-up, pull-down and none respectively on wiringPi pin 0.
  111. PiFace Commands
  112.  
  113. The PiFace is somewhat limited in that it has 8 inputs pins and 8 output pins and these are fixed in the hardware, so only the write and read commands are implemented:
  114.  
  115. gpio -p write <pin> 0/1
  116.  
  117. Writes the value 0 (off) or 1 (on) to the output pin on the PiFace
  118.  
  119. gpio -p read <pin>
  120.  
  121. Reads and prints the value on the given input pin.
  122.  
  123. gpio -p mode <pin> up/tri
  124.  
  125. This enables (up) or disables (tri) the internal pull-up resistor on the given input pin. You need to enable the pull-up if you want to read any of the on-board switches on the PiFace board.
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement