以下是针对BCM编号27, 28, 29, 30, 31的shell命令
shell方式
导出GPIO引脚
echo 27 > /sys/class/gpio/export
echo 28 > /sys/class/gpio/export
echo 29 > /sys/class/gpio/export
echo 30 > /sys/class/gpio/export
echo 31 > /sys/class/gpio/export
设置引脚为输出模式
echo out > /sys/class/gpio/gpio27/direction
echo out > /sys/class/gpio/gpio28/direction
echo out > /sys/class/gpio/gpio29/direction
echo out > /sys/class/gpio/gpio30/direction
echo out > /sys/class/gpio/gpio31/direction
设置所有指定引脚为低电平
echo 0 > /sys/class/gpio/gpio27/value
echo 0 > /sys/class/gpio/gpio28/value
echo 0 > /sys/class/gpio/gpio29/value
echo 0 > /sys/class/gpio/gpio30/value
echo 0 > /sys/class/gpio/gpio31/value
测试输出,设置所有指定引脚为高电平
echo 1 > /sys/class/gpio/gpio27/value
echo 1 > /sys/class/gpio/gpio28/value
echo 1 > /sys/class/gpio/gpio29/value
echo 1 > /sys/class/gpio/gpio30/value
echo 1 > /sys/class/gpio/gpio31/value
检查引脚状态 (如果需要)
echo "Pin 27 is $(cat /sys/class/gpio/gpio27/value)"
echo "Pin 28 is $(cat /sys/class/gpio/gpio28/value)"
echo "Pin 29 is $(cat /sys/class/gpio/gpio29/value)"
echo "Pin 30 is $(cat /sys/class/gpio/gpio30/value)"
echo "Pin 31 is $(cat /sys/class/gpio/gpio31/value)"
当你完成操作后,可以通过以下命令取消导出GPIO引脚
#echo 27 > /sys/class/gpio/unexport
#echo 28 > /sys/class/gpio/unexport
#echo 29 > /sys/class/gpio/unexport
#echo 30 > /sys/class/gpio/unexport
#echo 31 > /sys/class/gpio/unexport
脚本方式
导出GPIO引脚
for pin in 27 28 29 30 31; do
echo $pin > /sys/class/gpio/export
done
设置引脚为输出模式
for pin in 27 28 29 30 31; do
echo out > /sys/class/gpio/gpio$pin/direction
done
设置所有指定引脚为低电平
for pin in 27 28 29 30 31; do
echo 0 > /sys/class/gpio/gpio$pin/value
done
测试输出,设置所有指定引脚为高电平
for pin in 27 28 29 30 31; do
echo 1 > /sys/class/gpio/gpio$pin/value
done
如果你想再次设置回低电平,可以重复上述低电平设置步骤
检查引脚状态 (如果需要)
for pin in 27 28 29 30 31; do
value= ( c a t / s y s / c l a s s / g p i o / g p i o (cat /sys/class/gpio/gpio (cat/sys/class/gpio/gpiopin/value)
echo "Pin $pin is $value"
done
取消导出GPIO引脚
for pin in 27 28 29 30 31; do
echo $pin > /sys/class/gpio/unexport
done