Programming the Raspberry Pi’s GPIO pins from command line and Bash script

There are many ways to program the GPIO pins of the Raspberry Pi. The original intent of the Raspberry Pi foundation, back when they designed the Pi, was to make it programmable through Python. And that is one possibility, but people have come up since with ways to control the GPIO pins from C/C++, Java, Ruby, Perl and many other languages/technologies. One of the most surprising, elegant and simple ways to talk to the GPIO pins is from the command line, or if you want to do something more complex, from Bash scripts. For this we have to thank Gordon Henderson, author of the WiringPi library, who also freely supplies with it a command line utility simply named gpio.

The WiringPi library was developed to allow GPIO communication from C/C++ in a style similar to the Arduino Wiring programming language and many wrappers for WiringPi have been created since, which allow us to use other programming languages too for this purpose. But if you want to keep it simple, you can just use the gpio utility to issue simple commands which set the pins to input, output or even PWM mode, to read values from the GPIO pins or to write values to them. For example, let’s say that you want to light up an LED connected to GPIO pin 5 (WiringPi numbering). With two calls to the gpio command line utility you can set the pin mode to output and the pin value to HIGH:

gpio mode 5 out

gpio write 5 1

To turn the LED off:

gpio write 5 0

To read the value of a pin, set it to input mode and get its value:

gpio mode 5 in

gpio read 5

As simple as that! For a detailed description of the gpio utility‘s capabilities visit it’s official page here.

The best part is that, just like any other regular commands, the calls to the gpio utility can be combined in any way to write more advanced shell scripts, and this way it is possible to develop some quite complex functionality. Let us mention, however, that from a performance point of view it’s not a great idea to implement such complex GPIO control in shell scripts, because the speed of the Linux shell and that of the gpio utility is not so great and the scripts can keep the CPU quite busy. On a device like the Raspberry Pi this actually might matter. If your software is more complex and needs good performance, consider some other programming language. you might use the WiringPi library for example from C or C++ and there should be no major difference in the way you can control the pins compared to how you do it with the gpio utility.

Note that before you can control the GPIO pins from command line or from shell scripts, you will need to install the gpio utility on your system. Follow the author’s instructions provided here.

 

Monitoring the used disk space with GPIO-controlled LEDs

A simple example of how we can program the Raspberry Pi’s GPIO pins from a Bash script in order to create a useful utility is the used disk space monitor script. We will use a Bash script which periodically checks how many percents of the disk space is used from a partition, and lights up a green, yellow or red status LED based on the value. For the sake of this example we will light up the green LED if 50% is used at most, we will light up the yellow LED if the used space is between 50% and 80% and we will light up the red LED if more than 80% is used. These limit values can easily be changed in the script, just like the name of the monitored partition, for which we will use dev/root.  The default poling interval is 5 seconds, but it would probably make sense to set it to higher values (60 seconds for example) if the monitored disk is unlikely to become full very fast. The script is designed to run forever, so it needs to be stopped manually (CTRL+C) when no longer needed, but this behavior can easily be changed by modifying the condition of the while loop from always true to something that can actually change.

The three status LEDs (green , yellow, red) will be connected to the GPIO pins 0, 1 and 2 (WiringPi notation) through resistors of appropriate values (we will use 330 Ohm resistors for this example). This is how the wiring diagram looks like:

Raspberry Pi model B controlling 3 LEDs

Raspberry Pi model B controlling 3 LEDs

 

Below is the Bash script which does all the “magic” by issuing several calls to the WiringPi gpio utility (which will actually communicate with the hardware):

#! /bin/bash

#Parameters
diskToCheck=’dev/root’
refreshInterval=5 #Refresh interval in seconds

#GPIO 0 is set to HIGH if the used disk space is between 0%-limit1%
#GPIO 1 is set to HIGH if the used disk space is between limit1%-limit2%
#GPIO 2 is set to HIGH if the used disk space is between limit2%-100%
limit1=50
limit2=80

#Initialize all the GPIO pins to output mode
for i in `seq 0 2`; do gpio mode $i out; done

#Main loop
while (true); do

#Obtain the used disk space percent
usedDiskPercent=`df -H | grep $diskToCheck | awk ‘{ print $5 }’`
usedPercent=${usedDiskPercent%?}
echo -n Used disk space: $usedPercent%” “

#Set all the GPIO pins to LOW
for i in `seq 0 2`; do gpio write $i 0; done

#Set the appropriate GPIO pin to HIGH depending on the used disk space
highPin=0
if [ $usedPercent -gt $limit1 ]; then
if [ $usedPercent -le $limit2 ]; then
highPin=1
else
highPin=2
fi
fi
echo Setting pin $highPin to HIGH
gpio write $highPin 1

#Suspend until next cycle
sleep $refreshInterval

done

You may also download the bash script from here.

 


Comments

Programming the Raspberry Pi’s GPIO pins from command line and Bash script — 1 Comment

  1. Very cool, thank you for this post, it helps me understand the gpio interface much better, and the ability to access the interface from the command line is very cool as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>