Created
June 20, 2017 07:55
-
-
Save maxchehab/175dd01a80af9c237da2ce90f103bc14 to your computer and use it in GitHub Desktop.
Bash script to read and write RaspberryPi gpio pins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# >sudo sh gpio.sh <action> <pin> <value> | |
# reading example: | |
# >sudo sh gpio.sh read 1 | |
# >0 | |
# writing example: | |
# >sudo sh gpio.sh write 3 1 | |
#!/bin/bash | |
#assign parameters | |
action=$1 | |
pin=$2 | |
value=$3 | |
#create gpio instance | |
echo $pin > /sys/class/gpio/export | |
if [ $action = "read" ];then | |
#assign direction | |
echo in > /sys/class/gpio/gpio$pin/direction | |
#read gpio value | |
cat /sys/class/gpio/gpio$pin/value | |
elif [ $action = "write" ];then | |
#assign direction | |
echo out > /sys/class/gpio/gpio$pin/direction | |
#assign value | |
echo $value > /sys/class/gpio/gpio$pin/value | |
else | |
echo "Unknown parameter" | |
fi | |
#remove gpio instance | |
echo $pin > /sys/class/gpio/unexport |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment