Before using a GPIO, ensure it is not configured for another function (e.g., I2C, SPI). If it is, you can reconfigure it as a standard GPIO by applying a Device Tree Overlay.
Once you have identified the correct GPIO number (e.g., 465), follow these steps to control it via the sysfs interface.
Make the GPIO accessible to user space:
$ echo 465 | sudo tee /sys/class/gpio/export
Configure the GPIO as either an input or an output:
# Set GPIO output $ echo out | sudo tee /sys/class/gpio/gpio465/direction # Set GPIO input $ echo in | sudo tee /sys/class/gpio/gpio465/direction
If configured as an output, you can set its logic level:
# Set GPIO output high $ echo 1 | sudo tee /sys/class/gpio/gpio465/value # Set GPIO output low $ echo 0 | sudo tee /sys/class/gpio/gpio465/value
If configured as an input, read its current logic level:
$ cat /sys/class/gpio/gpio465/value
The command returns 0 (low) or 1 (high).
When finished, release the GPIO:
$ echo 465 | sudo tee /sys/class/gpio/unexport
If you encounter an error similar to the following when exporting, the GPIO pin is likely reserved for another function (e.g., by the device tree):
echo 465 | sudo tee /sys/class/gpio/export tee: /sys/class/gpio/export: Invalid argument
Solution: Modify the Device Tree Overlay configuration to free the pin and define it as a standard GPIO.