Raspberry Pi Raspbian Distro Installation and GPIO Port

Raspberry Pi Raspbian Distro Installation and GPIO Port

When you get your Raspberry Pi computer, one of the first things you will need to do is burn a Linux Distro onto an SD Card. Without this your Raspberry Pi is just a nice looking brick.

The recommended distro to use is the Raspbian "wheezy" distro and is the one we shall be using.

If you don't have any asperations to use (or hack) the Raspberry Pi GPIO port then you can stop at burning the distro to SD Card. If you want to use the GPIO port in your electronics world-domination project then there are a number of updates and applications that need to be downloaded and installed. Don't worry though it's all fairly straightforward and we will go through it step by step.

Burn the Raspbian Distro to SD Card

The first step is to download the latest Raspbian distro for the Raspberry Pi and burn it to an SD Card. You will need at least a 2GB SD Card for this, but bigger is better and SD Cards are quite cheap so i recommend an 8GB card.

Go to www.raspberrypi.org/downloads and download the latest Raspbian "wheezy" distro. Choose the .zip file and extract the .img file from it.

Also on the downloads page is a link to a peice of software you will need in order to burn the distro onto the SD Card. For Windows this is called Win32DiskImager.

Download and run Win32DiskImager

If you get an error like the following, just ignore it. Win32DiskImager is still in development and it can report errors with some SD Cards. Don't worry, it will still work ok.

Select the img file for the distro and the SD Card drive and click on the Write button. It may take a while to burn the image to the SD Card.

Once the image is successfully burned to the SD card, eject the card and load it into your Raspberry Pi. You are ready to play.

GPIO Connections

Later in this tutorial we shall be flashing an LED on the GPIO port and also using the MCP23017 port expander chip to test out the I2C communications.

For this setup we have used a breadboard and added the following items

You will also need a suitable serial connection to your PC. The Raspberry Pi runs at 3.3V and you can damage it if you put 5V on it's pins so we would recommend the FTDI Basic Breakout 3.3V board. We had to hand the 5V version of this board so we have also used a 4-way Bi-Directional logic level converter but if you have a suitable 3.3V device then use that.

Our setup looks like this.

We have our USB Serial connection connected to the RX and TX pins on the GPIO breakout (via logic converter as we are using a 5V board)

An LED and resistor connected from GPIO pin #4 to GND

On the Port expander board we have an LED and resistor connected from pin A0 to GND.

Boot the Raspberry Pi

There are two options for booting up the Raspberry Pi.

  1. You can plug it into a TV/Monitor via the HDMI interface and also attach USB keyboard and Mouse
  2. You can connect to the Raspberry Pi GPIO port's serial UART and use terminal software on a PC to communicate with The Raspberry Pi.

As the remainder of this tutorial concerns updating the distro to be able to use the GPIO port, we will use the second method. You can still do all the software steps using method 1.

The default buad rate for the serial port is 115200, so set your terminal software to this.

Boot up the Raspberry Pi and logon using the default username and password for the Raspbian distro

Username: pi
Password: raspberry

Internet Connection

To download and install the software packages needed, your Raspberry Pi needs to be connected to the internet

Install Python Development Software

Python development software is worth installing as there are many examples on the internet using this language for the Raspberry Pi

Enter the following at the command line

sudo apt-get install python-dev

Follow the prompts to download and install the python package

Download Raspberry Pi GPIO tools

In order to access the Raspberry Pi GPIO port we need to download and install the GPIO package.

The latest version is 0.3.1a but you can check for a newer version by going to http://pypi.python.org/packages/source/R/RPi.GPIO/

Enter the following at the command line (alter if a newer version is available)

wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz

This will download an archived file. We need to extract the files. Run the following

tar -zxf RPi.GPIO-0.3.1a.tar.gz

This will create a directory with the files in. To install the application we need to change to this directory and run the install

cd RPi.GPIO-0.3.1a

sudo python setup.py install

Return to your home directory by typing

cd ..

I2C Tools Package

There is a useful package which has some great command line tools for using I2C

Download by entering

sudo apt-get install i2c-tools

Again, follow the prompts to install this package

Enable I2C and SPI protocols

I2C and SPI protocols are turned off in the distro by default, so before we can use them we need to enable them.

We need to edit the file /etc/modprobe.d/raspi-blacklist.conf

Everyone has their favourite editor (and if you are doing this in a graphical environment then there will be a graphical editor you can use)

We will use the editor called vi. If you don't know the vi commands just type what we tell you

Run the following

sudo vi /etc/modprobe.d/raspi-blacklist.conf

You will see two lines. We need to disable the blacklisting of these by putting a hash # character in front of them

#blacklist spi-bcm2708
#blacklist i2c-bcm2708

Use the arrow keys to position the cursor at the beginning of each line and press the 'i' key on your keyboard to enter insert mode. Press the # key. Exit insert mode by pressing the Esc key Do the same for the other line

Save the file by entering the following (>> type the colon)


Start I2C automatically at boot

As we will be using I2C, we will enable it automatically at boot time. To do this we add a line to the file /etc/modules


sudo vi /etc/modules

Use the cursor keys to move down to the last line in the file and press the 'A' key on your keyboard to append to the file. Hit the return key to start a new line and type


Press the Esc key to exit data entry mode and save the file by typing


Add the user 'pi' to I2C users

To save having to run the I2C tools as root, we can add the 'pi' user to the I2C group


sudo adduser pi i2c

Update Packages

Ok, nearly done, but lets make sure we have all the latest packages installed.


sudo apt-get update

Then run

sudo apt-get upgrade


We have installed packages and modified settings. We need to reboot for these to take effect.

Run the command

sudo shutdown -r now

This will reboot the Raspberry Pi.

Note that you should never just switch off the power (just as you shouldn't on a PC). To shutdown your Raspberry pi before switching it off run

sudo shutdown -h now

and wait for the "Power Down" text to be displayed.

Flash the LED

After rebooting it's time to make sure all is working

We will create a python program called flash.py to flash the led on the GPIO port.


vi flash.py

Press 'i' to enter insert mode and type (or paste) the following program

import RPi.GPIO as GPIO
GPIO.setup(7, GPIO.OUT)
import time
while True :
    GPIO.output(7, True)
    GPIO.output(7, False)

Press Esc to exit insert mode and save the file


Run the program by entering

sudo python flash.py

You should see the LED flashing

Press Control-C to exit the program

Test the I2C Port Expander

Using the i2ctools application we installed we can check to see if there are any I2C devices connected.

If you have an Original Raspberry Pi (Sold before October 2012) - the I2C is port 0:

i2cdetect -y 0

If you have a second rev Raspberry Pi, the I2C is on port 1:

i2cdetect -y 1

You should see the following output with our MCP23017 chip on port 0x20

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Now we can use the command line tools to send commands to the MCP23017 chip

Change the port number from 0 to 1 in the following commands if you have a Rev 2 board

i2cset -y 0 0x20 0x00 0x00

This sets bank A as outputs

Next set all of bank A to high

i2cset -y 0 0x20 0x12 0x01

You should see the LED come on

Set bank A to low

i2cset -y 0 0x20 0x12 0x00

The LED should go off


Well that's it. The GPIO port works and you have I2C installed and working.