My First Raspberry Pi (RasPi) Post…GPIO

Reading and Writing to the RasPi GPIO using BASH

The gPIo of the Raspberry Pi

The top header is the gpio. It’s nice that it’s called the gPIo. How convenient!

I’ve finally taken some time to play with my Pi after getting some of the peripherals to work. I struggled getting the sound to work, but finally found a post that described what to do. I’d like to point out that I’m still using Debian Squeeze. That might be where some of my issues stem from. has the latest downloads, and many of the bugs that I deal with are probably solved by now in newer distributions.

What I have done is taken the time to play with the GPIO port using a Bash script. My Bash skills are not top notch, but with a little searching I can usually get things done. Here’s what I discovered using my simple Bash script.

I wanted to see how the Pi treats certain files when the ports were exported. Normally, the files used for output don’t even exist. The Bash script demonstrates this. Here’s the code for setting up a GPIO port for output, reading the state of the pin, changing the state of the pin and reading it again, and then cleaning up.

# /sys/class/gpio/export
# /sys/class/gpio/unexport
# /sys/class/gpio/gpio4/direction
 echo "This is a test...."
 echo -e "I need to relearn the Bash\n"
#display the gpio folder
 echo "ls /sys/class/gpio BEFORE export"
 ls /sys/class/gpio
#set up gpio 4 for output
 echo "4" > /sys/class/gpio/export
#display the gpio folder
 echo -e "\nls /sys/class/gpio AFTER export"
 ls /sys/class/gpio
#change the direction of the pin
 echo "out" > /sys/class/gpio/gpio4/direction
#output a 1 to gpio4
 echo "1" > /sys/class/gpio/gpio4/value
#read value
 value="$(cat /sys/class/gpio/gpio4/value)"
 echo -e "\t\tThe value is $value"
#output a 0 to gpio4
 echo "0" > /sys/class/gpio/gpio4/value
#read value
 value="$(cat /sys/class/gpio/gpio4/value)"
 echo -e "\t\tThe value is $value"
#clean up
 echo "4" > /sys/class/gpio/unexport
#display the gpio folder
 echo -e "\nls /sys/class/gpio AFTER unexport"
 ls /sys/class/gpio
 echo -e "\n\ndone..."

This script shows what happens before and after each command. It’s neat to see. Here’s how it works:

root@raspberrypi:/home/pi/gpio# ./ 
This is a test....
I need to relearn the Bash

ls /sys/class/gpio BEFORE export
export	gpiochip0  unexport

ls /sys/class/gpio AFTER export
export	gpio4  gpiochip0  unexport
		The value is 1
		The value is 0

ls /sys/class/gpio AFTER unexport
export	gpiochip0  unexport


You can see that files are created and destroyed after exporting or unexporting. This is how the Pi works when working with the files. I’ve also demonstrated how you can read the state of a port pin, whether it is an input or an output.

I’ve ordered a Raspberry Pi case from Adafruit along with a GPIO header cable. I’ll start designing my own interfaces for solderless breadboards once it all arrives. I’m looking forward to being able to interact with my RasPi via SSH and the Bash. I’m hoping to find ways to send data to web pages, too. I just have to find a need.

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.