Follow by Email

Blog Archive

Search This Blog

Raspberry Pi: control and program GPIO with "sysfs" (shell / bash)

how to program and control Raspberry Pi GPIO with sysfs
Share it:
Raspberry Pi: control and program GPIO with "sysfs" (shell / bash)
GPIO-sysfs
The GPIOs are represented on Raspberry Pi as files and can be found in the directory tree under "/ sys / class / gpio". This is called "sysfs". If you look at the contents of the files, you get the state out. This is either "1" or "0".
ls / sys / class / gpio
When controlling and programming you have to make sure that you do not choose the pin number, but the BCM number of the GPIO. In the following example, we configure the GPIO 17 (pin 11) as the output and the GPIO 18 (pin 12) as the input.
Note: Programming the GPIOs via "sysfs" is not recommended. The recommended way is with the command line tool "pigpio".
Configure GPIO as input or output
To configure a GPIO you have to create it first. Then you decide on the direction. So either as input or output. First, we turn on the "GPIO 17".
echo 17> / sys / class / gpio / export

If the GPIO is to be configured as an input, the following command applies.
echo in> / sys / class / gpio / gpio17 / direction

If the GPIO is to be configured as an output, the following command applies.
echo out> / sys / class / gpio / gpio17 / direction

A GPIO configured as an output should then be given a status, ie "high" or "low".

State of a GPIO output set
Basically you can only set outputs. To set the state of a GPIO output to "high", the following command is sufficient.
echo 1> / sys / class / gpio / gpio17 / value

To set the state of a GPIO output to "low" the following command is sufficient.
echo 0> / sys / class / gpio / gpio17 / value

Determine GPIO status
To determine the state, ie "high" or "low", at a GPIO, the following command is sufficient. The GPIO can be both an input and an output.
cat / sys / class / gpio / gpio17 / value

The output is "1" for "high" or "0" for "low".

Disable GPIO
If you no longer need GPIOs, then you should disable them. The following commands will cause the files and directories of the GPIOs to disappear. When restarting, the GPIOs are automatically reset.
echo 17> / sys / class / gpio / unexport
Share it:

Raspberry Pi

Post A Comment:

0 comments: