GPIO 제어

2019. 8. 18. 01:06Linux/Raspberry Pi

GPIO(General-Purpose Input/Output) 제어

GPIO 할당
$echo [gpio num] > /sys/class/gpio/export
$echo [in or out] > /sys/class/gpio/gpio[num]/direction
$echo [value] /sys/class/gpio/gpio[num]/value

GPIO 해제
$echo [gpio num] > /sys/class/gpio/unexport

 

Permission denied 발생시

$sudo sh -c 'echo [gpio num] > /sys/class/gpio/export'
$sudo sh -c 'echo [in or out] > /sys/class/gpio/gpio[num]/direction'
$sudo sh -c 'echo [value] /sys/class/gpio/gpio[num]/value'

GPIO 해제
$sudo sh -c 'echo [gpio num] > /sys/class/gpio/unexport'

 

 

GPIO pin

ex) GPIO26 output으로 사용할 경우
GPIO 할당
echo 26 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio26/direction
echo 0 /sys/class/gpio/gpio26/value

GPIO 해제
echo 26 > /sys/class/gpio/unexport

 

 

[참조]
https://www.raspberrypi.org/documentation/usage/gpio/