Lab 4.2: Raspberry Pi – working with GPIO

In this lab we’ll learn how to work with GPIOs on Raspberry Pi. To start the lab, connect keyboard, mouse, monitor, and Ethernet cable to your Raspberry Pi and power it on. If you know the IP address of your Raspberry Pi, you can remotely connect to it using SSH or VNC (refer to lab 4.1).

The Raspberry pi has a set of pins for general purpose input/output or GPIO for short. A double row of 13 pins for 26 pins in all as shown in the figure. Using these pins, you can control external devices like motors or lights and receive signals from switches or sensors.

There are 8 general purpose digital I/O pins – these can be programmed as either digital outputs or inputs. One of these pins can be designated for PWM output too. Additionally there is a 2-wire I2C interface, 6-wire SPI interface, and the serial UART with 2 pins. The I2C and SPI interfaces can also be used a general purpose I/O pins when not being used in their bus modes, and the UART pins can also be used if you reboot with the serial console disabled, giving a grand total of 8 + 2 + 6 + 2 = 18 I/O pins.

There are two different methods to write to or read from these pins on a Raspberry Pi. The first one is creating a file-type access to the GPIO in the file system and the second is to write/read the base address of the memory allocated to the GPIO. In this lab, we’ll use the first method. The interface to allow working with GPIO is at the following path


If you want to work with a particular GPIO you must first reserve it and set the input/output direction. You can then start using it. Once you finished using it, you need to free the GPIO to allow other modules or processes to use it. From the user level side the operation to reserve the GPIO is called “export” the GPIO. This can be done by echoing the GPIO number you are interested in to a special path as follow (Wherever you see XX, change it with the GPIO number you want to use e.g. 27):

A new “folder” will show up in the GPIO interface path as shown below:


This new “folder” will allow you to work with the GPIO you just reserved. Next you need to set the in/out direction of the GPIO. Execute one of the following echo commands depending on whether you want to make it an input or an output pin:


Once you set the direction, you can directly manage the value of GPIO. You can do this by executing an echo command like:

to make the pin HIGH or the following echo command to make the pin LOW.

You can read the current pin value by using the following command:

Once finished to use your GPIO you have free it by make the same echo command but to different path:


Writing to GPIO

First we learn how to use a GPIO as output by programmatically switching a LED on and off. To do this, we need to connect the LED between a general-purpose input/output pin (GPIO) and the ground. Then we need to change the state of the pin between 1 and 0 to switch the LED on and off.The easiest way to manipulate the Pi GPIO pins is via console. Here’s a simple shell script to periodically toggle the GPIO 27:

Before we can run this script, we need to connect an LED to Raspberry Pi. According to the figure above, pin #13 corresponds to a software-controllable GPIO27 and pin #14 (for convenience, the pins are shown again in the figure below):

Now connect the LED with a resistor to pins 13 and 14. Remember to connect the side with resistor to GROUND i.e. pin 14. Warning! be careful when connecting things, as short-circuiting certain pins will restart (and might even burn) our board and any devices attached to it. Use an expandable USB phone charger instead of your actual computer as the power source. Turn off your Raspberry PI board and carefully connect the LED (as shown below), then turn on the Raspberry Pi.

Run the above bash script (remember to make it executable). The LED should start blinking. You can now stop the shell script and free (unexport) the GPIO.


Reading from GPIO

Now we’ll see how to connect a DHT22 Temperature and Humidity sensor to Raspberry Pi and read data from the sensor. We need to install a library developed by Adafruit. The installation is quite simple and can be done with two commands. These commands download Adafruit_DHT form Adafruit’s github repository and copy it in /usr/bin:

To test the electronic assembly and the software installation execute the following command after connecting the DHT22 sensor to Raspberry Pi (ask the TAs to help you connecting the sensor):

You should see the information about temperature and humidity as shown below. In this example we used GPIO pin 4, however any GPIO pin can be used.

You can use the following very simple shell script to periodically read data from the sensor. The DHT series of sensors will only respond every 2 seconds so if you are not getting data, be sure to wait 3 seconds before trying again!


Rpi Monitor

RPi-Monitor is a self monitoring application designed to run on Raspberry Pi. It monitors your Raspberry Pi software version, update info, uptime, cpu-load, temprature, memory, sd-card space, network usage and has secure shell (ssh) build in. The extraction of the information is done by a process designed to run as a daemon (which is executed as root). The extracted key performance indicators (KPI) from the computer are stored into a Round Robin Database (RRD) to keep a history of the health of the computer. rpimonitord has the perl script which starts an embedded web server giving access to the pages. The web server is running as a separate process owned by a non root user (the user ‘pi’ by default).

To install Rpi monitor, run the following commands:

Open a web browser on your local machine and enter the following url to access Rpi Monitor. Replace the IP address with the IP address of your Raspberry Pi.

Click on Start button to open the status page which shows various details about your Raspberry Pi. The Statistics tab also has several nice looking graphs. You can add a custom category to the Statistics tab to display live data received from temperature/humidity sensor. Edit the file /etc/rpimonitord.conf and add the following code at the end. Remember to add an extra space after the second = in the third line.

Close the file and restart RPi-Monitor for changes to take effect:

Open the Rpi Monitor in the web browser and click on the Statistics tab. A new category named “Sensor Data” should have been added. Click on it to see live data from the temperature/humidity sensor.