Raspberry PI GPIO – BASH script with functions

Raspberry Pi computerI took my test code from my previous PI post and made a few changes to make things easier to read and use. This tutorial shows the functions, how to use them, and then demonstrates how to add functions to BASH memory so that they can be called quickly and easily without clogging up your scripts.

Files: pi.sh.bash_profile

Pi.sh is the script that I wrote to control the GPIO. It has a description on how to use it, but I’ll list the major points here. First, a list of the functions and what they expect for input and output.

  • ex_port [port#] [direction*]
  • unex_port [port#]
  • direct_port [port#] [direction]
  • output_port [port#] [state]
  • read_port [port#] #returns port state in variable $return_value

The port# can be any of the Raspberry Pi’s supported GPIO ports: 0-1, 4, 7-11, 14-15, 17-18, 21-25. *In export, the direction is optional. You can follow it with direct_port. The only supported directions are in or out. The only supported states are 0 or 1. Read_port returns the value read to the variable $return_value.

The cool part about what I discovered is also the coolest. This enables control of the RasPi outside of an actual script. It involves saving the functions described above into the Bash memory. First, download the .bash_profile to your home directory and rename it as such. The only way to see it is to ‘ls -a’. To enable it to control your GPIO ports, change to root, then load it into memory:

pi@raspberrypi:~$ su
root@raspberrypi:/home/pi# . ./home/pi/.bash_profile

Now, you’re ready to roll. You can either call a simple script or you can type the commands in the terminal. Another cool thing is just skipping the entry of the .bash_profile and make your script do it. Here’s a simple script that does it all for you.

#Filename: script.sh
. ./home/pi/.bash_profile
#must reference home directly since you're running as root
echo "Script running..."
ex_port 4 out
output_port 4 1
output_port 4 0
unex_port 4
echo "Script done!"

Now, switch user to root and call your script.

pi@raspberrypi:~$ su
root@raspberrypi:/home/pi# ./script.sh
Script running...
Script done!

I hope that you find these ideas helpful. I’ve chosen to use Bash because I like that I don’t have to compile it, and I don’t have to learn a new language. I feel that the tools to use Bash to control the GPIO are simple and easy to implement. There’s no mess with a Bash script, but ‘to each his own’ or ‘to each her own’.

Good luck, and as soon as I have a GPIO cable and my Pi box, I’ll test these out with real parts and components!

This entry was posted in Electronics, Linux/Ubuntu, Programming, Tutorials and tagged , , , by robbie. Bookmark the permalink.

About robbie

I am an electronics enthusiest and a ham radio operator (W1RCP). I like to play with electronics. It's fun and educational. I looked forward to working in the engineering field in the future. I have a BS in Electronics Engineering Technology from DeVry University. I also have an Associate's degree in Marketing Management from Moultrie Tech, and a diploma in Electronics from MTC.

One thought on “Raspberry PI GPIO – BASH script with functions