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
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:
It should return value "in" as input. If you want read the value on the GPIO pin, it is done by command:
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
echo 1 > /sys/class/gpio/gpio18/value