Raspberry Pi Blog - Abandoned Since 2015
  • Home
  • Raspbian
    • Installation
    • Adjustments
  • Guides
    • DNS resolver
    • VNC server
    • Plotting graphs
    • Streaming with VLC
    • VOD with VLC
    • Stream announcements
    • Samba shares
    • NTP client and server
    • DynDNS client
    • No-IP client
    • Bluetooth
    • Internet via GPRS/EDGE/3G/HSPA
    • OpenSSH server
    • XMPP/Jabber server
    • CryptoCat messaging service
    • Converse.js XMPP/Jabber client
  • GPIO
    • Setup GPIO
    • LEDs and buttons
    • Thermal sensor (1Wire)
    • Thermal sensor (I2C)
    • UART to RS-232
  • Case
  • Arduino
  • Contact
  • Sitemap

Setup GPIO


Before you can do anything with GPIO you have to enable it and set some properties. also I will show how to read or set values from GPIO pins. At first choose which GPIO pins you want to use. RPi contains 8 GPIO ports numbered 4, 17, 18, 21, 22, 23, 24 and 25. I choosed GPIO17 and GPIO18. Later I will show you how to connect LED and button, thats why I choose 2.
  • GPIO17 - button
  • GPIO18 - LED
I choosed GPIO port 17 so at first I have to "export" it by command:
echo 17 > /sys/class/gpio/export
GPIO ports can be in 2 directions - input or output. From the safety reasons, when you enable GPIO port it is set in the input direction. You can verify it by command:
cat /sys/class/gpio/gpio17/direction
It should return value "in" as input. If you want read the value on the GPIO pin, it is done by command:
cat /sys/class/gpio/gpio17/value
Without anything connected to GPIO17 it should return value "1". This was a GPIO17 for button. Next I will "export" GPIO18 for LED.
echo 18 > /sys/class/gpio/export
But LED is not an input device so I need to set GPIO18 to output direction by command:
echo out > /sys/class/gpio/gpio18/direction
Since port is defined as output, you can set its value (logical 1 or 0) by command:
echo 0 > /sys/class/gpio/gpio18/value
...or...
echo 1 > /sys/class/gpio/gpio18/value
Create a free web site with Weebly