So I've been trying to get my Raspberry Pi GPIO to behave correctly by writing to and reading from /sys/class/gpio/
using the command line and sysfs. Based on the following tutorials, I am not able to replicate setting a GPIO to output and setting it's value high (1).
What I expect to happen is that when I run the following commands
$ echo 18 > /sys/class/gpio/export
$ echo out > /sys/class/gpio/gpio18/direction
$ echo 1 > /sys/class/gpio/gpio18/value
that I see the result from the following command
$ cat /sys/class/gpio/gpio18/value
1
To clarify, I have nothing hooked up to the GPIO pins, I really am only interested in reading the output value back and for it to be the same as what I set it. For what it's worth, I'm trying to make my own Ruby driver for the Pi's GPIO pins and I am checking that I am able to read back the same gpio value that I set it to if it's an output.
The following is a shell session as proof.
Permissions and OS versioning
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.14.34+ #1110 Mon Apr 16 14:51:42 BST 2018 armv6l GNU/Linux
pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.4 (stretch)
Release: 9.4
Codename: stretch
pi@raspberrypi:~ $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi
GPIO 18
pi@raspberrypi:~ $ cd /sys/class/gpio/
pi@raspberrypi:/sys/class/gpio $ ls -a
. .. export gpiochip0 unexport
pi@raspberrypi:/sys/class/gpio $ echo 18 > export
pi@raspberrypi:/sys/class/gpio $ ls -a
. .. export gpio18 gpiochip0 unexport
pi@raspberrypi:/sys/class/gpio $ cd gpio18/
Permissions and groups for GPIO 18
pi@raspberrypi:/sys/class/gpio/gpio18 $ stat value
File: value
Size: 4096 Blocks: 0 IO Block: 4096 regular file
Device: fh/15d Inode: 16223 Links: 1
Access: (0770/-rwxrwx---) Uid: ( 0/ root) Gid: ( 997/ gpio)
Access: 2018-05-01 00:41:52.493284419 +0000
Modify: 2018-05-01 00:41:52.493284419 +0000
Change: 2018-05-01 00:41:52.500207389 +0000
Birth: -
Try setting it to out and high
pi@raspberrypi:/sys/class/gpio/gpio18 $ ls -a
. .. active_low device direction edge power subsystem uevent value
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
in
none
0
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo out > direction
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo 1 > value
Here is where I expect the content of the value file to be 1
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
out
none
0
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo in > direction
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
in
none
0
More checks
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo high > direction
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
out
none
0
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo low > direction
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
out
none
0
Try the same for GPIO 23
pi@raspberrypi:/sys/class/gpio/gpio18 $ cd ..
pi@raspberrypi:/sys/class/gpio $ echo 23 > export
pi@raspberrypi:/sys/class/gpio $ ls -a
. .. export gpio18 gpio23 gpiochip0 unexport
pi@raspberrypi:/sys/class/gpio $ cd gpio23/
Permissions and groups
pi@raspberrypi:/sys/class/gpio/gpio23 $ stat value
File: value
Size: 4096 Blocks: 0 IO Block: 4096 regular file
Device: fh/15d Inode: 16238 Links: 1
Access: (0770/-rwxrwx---) Uid: ( 0/ root) Gid: ( 997/ gpio)
Access: 2018-05-01 01:26:37.121782992 +0000
Modify: 2018-05-01 01:26:37.121782992 +0000
Change: 2018-05-01 01:26:37.126033971 +0000
Birth: -
Try setting it to out and high
pi@raspberrypi:/sys/class/gpio/gpio23 $ ls -a
. .. active_low device direction edge power subsystem uevent value
pi@raspberrypi:/sys/class/gpio/gpio23 $ cat active_low direction edge value
0
in
none
0
pi@raspberrypi:/sys/class/gpio/gpio23 $ echo out > direction
pi@raspberrypi:/sys/class/gpio/gpio23 $ echo 1 > value
Here is where I expect the content of the value file to be 1
pi@raspberrypi:/sys/class/gpio/gpio23 $ cat active_low direction edge value
0
out
none
0
pi@raspberrypi:/sys/class/gpio/gpio23 $ echo in > direction
pi@raspberrypi:/sys/class/gpio/gpio23 $ cat active_low direction edge value
0
in
none
0
More checks
pi@raspberrypi:/sys/class/gpio/gpio23 $ echo high > direction
pi@raspberrypi:/sys/class/gpio/gpio23 $ cat active_low direction edge value
0
out
none
0
pi@raspberrypi:/sys/class/gpio/gpio23 $ echo low > direction
pi@raspberrypi:/sys/class/gpio/gpio23 $ cat active_low direction edge value
0
out
none
0
Clean up
pi@raspberrypi:/sys/class/gpio/gpio23 $ cd ..
pi@raspberrypi:/sys/class/gpio $ echo 18 > unexport
pi@raspberrypi:/sys/class/gpio $ echo 23 > unexport
pi@raspberrypi:/sys/class/gpio $ ls -a
. .. export gpiochip0 unexport
pi@raspberrypi:/sys/class/gpio $
This is with a fresh install of NOOBS and the very first commands run on it. It seems like all other sysfs functionality works as expected except for the value file of a GPIO. The only explanation I have for this actual behavior to differ from my expected behavior is that either there are inherit pull up/downs that hold the output to a certain value without a sink to some circuitry (which still wouldn't make sense to me but I've been wrong before) OR that my expectation is flat wrong and that the value file does not reflect the actual physical voltage level that the pin is set to.
Perhaps some more useful information is that I do not want to use a command line utility to do this, I would like to only work with with the file system for my driver I am building. That being said, I have used
gpio write 18 1
but the results were the same as above when reading the contents of the value file for GPIO 18.
It was a bad Raspberry Pi. I just received a new one. This is a session.
pi@raspberrypi:~ $ cd /sys/class/gpio/
pi@raspberrypi:/sys/class/gpio $ ls -a
. .. export gpiochip0 unexport
pi@raspberrypi:/sys/class/gpio $ echo 18 > export
pi@raspberrypi:/sys/class/gpio $ ls -a
. .. export gpio18 gpiochip0 unexport
pi@raspberrypi:/sys/class/gpio $ cd gpio18/
pi@raspberrypi:/sys/class/gpio/gpio18 $ ls -a
. .. active_low device direction edge power subsystem uevent value
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
in
none
0
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo out > direction
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
out
none
0
pi@raspberrypi:/sys/class/gpio/gpio18 $ echo 1 > value
pi@raspberrypi:/sys/class/gpio/gpio18 $ cat active_low direction edge value
0
out
none
1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.