Issue
As seen on the website: rel="nofollow">https://luketopia.net/2013/07/28/raspberry-pi-gpio-via-the-shell/
So if we want to be able to access pin 4, we would type echo 4 > /sys/class/gpio/export
and
To specify that we want to use the pin as an output, we can do echo out > /sys/class/gpio/gpio4/direction
I am a bash noob, and I wanted to access RPi's GPIO pins through terminal. I searched Google to find out what exactly what ">" operator does, but all I find is string comparison (Which I think returns 1 or 0). How exactly does this enable us to reach the pins?
Solution
It looks to me like there's two aspects to this question,
- How the redirection operator works in itself, and...
- How the GPIO driver works through RPi linux
I'll answer these individually below:
1. The Redirection Operator >
The >
is what's known as a "redirection operator". It takes the output of a process or function, and redirects it from the terminal to somewhere else. The echo
command writes output to the terminal. In this specific case, the terminal output from echo 4
(i.e. the value 4
) is passed to /sys/class/gpio/export
. See section 2 for more details on what that actually does.
Another common use of >
, that may give a more tangible example, is to redirect the output into a file, e.g.:
$ echo "This text will go into a file." > "myfile.txt"
The file myfile.txt
will be created and the line we echo'd out will be put into it, so its contents will be "This text will go into a file.".
It's not just echo
either, any command's console output can be redirected, e.g.:
$ ls -la > "directoryListing.txt"
The above would do a directory listing of all files, and output the result into the text file "directoryListing.txt" as opposed to the console.
For more details on linux concepts, particularly useful ones like redirection >
and pipe |
, I would advise trying out the link below:
2. The GPIO Driver access
From reading the description on this RPi linux site, it looks like /sys/class/gpio/export
taps into the device driver for the GPIO pins and tells it you wish to use the pin provided (in this case, pin 4). You provide input to this virtual directory by redirecting it from the console (see above, section 1).
Once you've called export, you've told the driver which pin (or pins) you wish to work with and it creates something akin to a virtual filesystem to allow control.
This is where the gpio4/direction
part comes in. That gpio4
folder was created when you exported pin 4
above. There are a number of (what you could think of as) virtual files in that directory, which then let you set up the pin configuration. The direction
one tells the driver whether the pin is an input pin (in which case it is passed the word in
), or an output pin (in which case we pass it the word out
).
Again, we set this using the console redirection operator >
and we can read it just like a normal file, to get the assigned value, like so:
$ cat /sys/class/gpio/gpio4/direction
To read/write values to the pin (i.e. whether it is set to on or off), one simply follows the same idea with the value
file:
$ echo 1 > /sys/class/gpio/gpio4/value
Sets the value of pin 4 to 1
, which means on. Equivalently, it can be turned off by doing
$ echo 0 > /sys/class/gpio/gpio4/value
Finally, if you had specified the direction of the pin as an input, by doing the following:
$ echo in > /sys/class/gpio/gpio4/direction
You could read that value thusly:
$ cat /sys/class/gpio/gpio4/value
If you wanted to store that in a variable, you might do something like:
$ myPinValue=$(cat /sys/class/gpio/gpio4/value)
Please note these examples are untested and my Linux-fu is a little rusty as I now sadly work in Windows world.
Answered By - n00dle Answer Checked By - Mildred Charles (WPSolving Admin)