Easy Raspberry Pi GPIO Pin Control with Bash 
Saturday, July 8, 2017, 13:37
Posted by Administrator
I have been experimenting a bit with home automation lately by hooking up some small electrical relays to my Raspberry Pis. So far I have my bedroom fan and a small lamp fully under remote control and connected to my home network. I even have them under voice control now by using an app called Tasker on Android, as well as two helper plugin apps for Tasker called Autovoice and an associated SSH Plugin. I feel an undeniable sense of power whenever I verbally tell my phone to turn my lamp or fan on or off, and it has quickly become a much more natural and pleasant feature of my bedroom over the last few days.

In the past, I have always used Python and a GPIO control library to control the output pins on a Raspberry Pi. This method requires installing both Python and the pin control library on a fresh Raspbian or Retropie install, which can be a bit of a nuisance.

Since the release of the first Raspberry Pi I've been wanting a simple solution to just turn Raspberry Pi pins on or off without having to install any additional software or remember any commands. I've also loved bash scripting for a long time now, and while putting together my last minimal Pi Zero clock project I realized that a pi's output pins can be fully controlled by writing to system files without the need of installing Python or anything else. For example, to turn on pin 26 on a Raspberry Pi one can simply enter these three short commands:

sudo echo "26" > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio26/direction
sudo echo "1" > /sys/class/gpio/gpio26/value

Turning the pin off is as easy as echoing a value of 0 to the pin's value in the same location as the last line.

I first created a number of small scripts to turn certain pins on, and another script to turn the same pin off and was using that with my home automation system. After a couple of days, I decided that I'd like to have a script to toggle the state of any arbitrary pin. This is so that I can issue the same command to turn my lamp on or off without having to be specific about what I want when I yell at the phone. I also wanted this script to be able to specifically turn a pin on or off in the case that I'm not at home and want to make sure something is turned on or off even if I don't recall whether I left it on or not. I quickly came up with the following bash code that accomplishes just that:

#!/bin/bash
# A utility script to toggle a raspberry pi gpio pin on or off
# Spike Snell - July 2017

# If there are command line options to use
if [ $# != 0 ]
then

# Set up our argument variables
arg1="$1"
arg2="$2"

# If the gpio pin was not previously set up
if [ ! -e /sys/class/gpio/gpio"$arg1" ];
then
# Make sure that gpio pin $arg1 is initialized
echo "Initializing gpio pin" $arg1
echo "$arg1" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio"$arg1"/direction
fi

# Check to see if there was a second command line argument
if [ -z "$2" ]
# Argument 2 for the on/off value was not set
# We should just toggle the current state of gpio pin $arg1
then
# If the current value is 1 set it to 0 and vice versa
if grep -q 1 /sys/class/gpio/gpio$arg1/value
then
echo "Toggling gpio pin" $arg1 "to 0"
echo "0" > /sys/class/gpio/gpio"$arg1"/value
else
echo "Toggling gpio pin" $arg1 "to 1"
echo "1" > /sys/class/gpio/gpio"$arg1"/value
fi
# Argument 2 for the on/off value was set
# We should set gpio pin $arg1 to the value of $arg2
else
echo "Setting gpio pin" $arg1 "to" $arg2
echo "$arg2" > /sys/class/gpio/gpio"$arg1"/value
fi

# Else there were no options passed in, let the user know about them
else
echo "Please enter what pin to toggle, ex: sudo ./pin.sh 11"
echo "Optionally enter if the pin should be 1 or 0, ex: sudo ./pin.sh 11 0"
echo " * Make sure to run as super user "
fi

I included some helpful information near the bottom that gets displayed if the script is called without any parameters. To use it the script should be saved as pin.sh and set to be executable with the command sudo chmod +x pin.sh.

I plan to use this script quite a lot in the future and this seems a good place to keep a copy of it. Hopefully this can be helpful to others as well. Feel free to leave any comments or suggestions that you may have.
add comment ( 3276 views )   |  permalink   |  $star_image$star_image$star_image$star_image$star_image ( 3 / 538 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Next> Last>>