Setting Up Your Raspberry Pi For Python GPIO Projects
The Raspberry Pi is a great tool to get started with electronic tinkering projects. It's inexpensive, runs a full-fledged Linux operating system, and has I/O pins that can be used to control other electronic devices. This quick guide will walk you through the initial setup from unboxing your Raspberry Pi, to installing the Python GPIO library.
This guide assumes that you are using Linux to setup your Raspberry Pi (hereafter called RPi). The RPi itself runs Linux, and the basic skills and commands you learn will become very valuable later once you are up and running.
What You'll Need:
- A Raspberry Pi model A or B
- Micro SD Card with capacity of at least 4GB. Make sure you have an adapter that will allow you to plug it into the USB port on your computer.
- An internet connection via ethernet cable, or a micro-usb WiFi adapter such as the Trendnet TEW-648UBM
- A working computer with an internet connection. I will be using a computer running Ubuntu Linux for this guide. If you have trouble with these steps, or are using an OS other than Linux, you may find detailed installation instructions in this quick start guide. Return here after you have installed the Raspbian image onto you SD card, and proceed with Step 2.
- An HDMI or composite cable to plug the Raspberry Pi into your TV or monitor.
- A keyboard and mouse
Step 1: Installing the Raspbian Operating System Using Linux
The first step to setting up you Raspberry Pi is to install the Raspbian OS image from the Raspberry Pi website onto your MicroSD card. Before starting, be sure that your Micro SD card is clean with no existing partitions. If you have trouble with these steps, or are using an OS other than Linux, you may find detailed installation instructions in this quick start guide. Return here after you have installed the Raspbian image onto you SD card.
- Go to the Raspberry Pi Download site and download the latest Raspbian release. A the time of this writing, the most recent version is "Wheezy". I use the direct download link for sake of simplicity.
- When the download is complete, open up the terminal and use the cd command to change directories to the folder where you downloaded the distribution, for example:
cd Downloads
- Run unzip to extract pi image. Remember, the filename of the distribution may have changed since the time of this writing:
unzip 2012-12-16-wheezy-raspbian.zip
- Now change directory to the directory where you unzipped the Raspbian distribution
cd 2012-12-16-wheezy-raspbian
- Insert your blank SD card into your computer using a card reader or adapter. Run the command df in the terminal to determine the name and location of your SD card.
df -h
From this command, I was able to determine that my usb card was called /dev/sdc1 by looking for the disk that matched the capacity of my SD card
- Be aware that some SD cards come pre-formatted with a filesystem. This may interfere with the installation of the Raspbian OS image. You will want to delete any existing partitions prior to copying the Raspbian image on your SD card. To do this, we will use the fdisk utility. Run fdisk as a superuser (prefix the command with the word sudo) using the disk name you discovered in the previous step
sudo fdisk /dev/sdc1
You should see a command prompt that looks like this:Command (m for help):
Type p to see all the partitions on the list. Type d to delete each partition on the card (you may need to do this several times). Once you've deleted all partitions, enter w at the command prompt to save your changes and exit.
- Run the df -h command again to list all connected disks. Again, make note of your SD card's name. The name may have changed if you deleted any partitions using fdisk. For example, my disk name changed to /dev/sdc.
- Copy the Raspbian image to your SD card using the dd command as a superuser. This action may appear to hang, be patient and wait until you see the command prompt. You may need to change the /dev/sdc or the name of the Raspberry Pi image depending on the type of card you are using, and the current Raspberry Pi distribution.
sudo dd bs=4M if=2012-12-16-wheezy-raspbian.img of=/dev/sdc
- Run the sync command as superuser
sudo sync
- Once the image is written onto the SD card, it's safe to remove the card from your system.
Step 2: First Boot - Configuration Options
Insert the SD card into your Raspberry Pi along with an ethernet cable/WiFi adapter, keyboard, and mouse. Connect the HDMI or composite cable to your TV/monitor, and turn it on. Make sure that your TV/monitor is set to the correct input. Finally plug in the RPi into the AC adapter to power it up.
On the first boot, you will be confronted with a configuration screen. You may want to make the following changes, at a minimum:
- System Password: You may want to chang the administrator password on the RPi. By default, it is set to username pi and password raspberry.
- Keyboard Language: Change the keyboard setting to your language and locale
- Expand the partition: This option will expand the size of your primary partition to the maximum size of your SD card during the next boot. Keep in mind that this will take a long time. It takes over 10 minutes to do this on an 8GB card.
- Boot to GUI: You may want to have your RPi boot directly into the Graphical User Interface(GUI) rather than to the command line. If you leave this option disabled, you can always launch the OS GUI by typing startx on the command line.
If you ever need to re-run the configuration options panel, use the following command: sudo raspi-config
Step 3: Optional - Setting Up Your WiFi Adapter
If you are planning to connect to the Internet using an ethernet cable, you can skip this section. This section will walk you through the process of connecting to your WiFi network using a WiFi adapter. Please note that you will need to purchase a separate WiFi adapter such as the ... The RPi does not have a built-in WiFi adapter.
- From the GUI, double-click the WiFi Config icon on the desktop
- On the Manage Networks tab, click the Scan button.
- Double-click the name of the network you wish to connect to. You may be prompted to enter security settings for your network.
Step 4: Installing The Python GPIO Library
If you're like me, you bought your RPi to "control things" using the I/O pins on the RPi. Using Python and the RPi GPIO library, you can do just that. Python should already be installed on your RPi, and it takes just a minute to install the GPIO library:
- Open the terminal on your RPi. Double-click the icon on the desktop named LXTerminal
- Type the following command to download the GPIO library as a tarball:
wget http://raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.4.1a.tar.gz
- Unzip the tarball:
tar zxvf RPi.GPIO-0.4.1a.tar.gz
- Change to the directory where tarball was inflated:
cd RPi.GPIO-0.4.1a
- Install the GPIO library in Python:
sudo python setup.py install
Important: You must be a superuser to run scripts that control the GPIO pins on your RPi. If you are using the IDLE IDE to write your Python scripts, be sure to launch it as a superuser. Open the terminal and type sudo idle to launch the IDLE IDE as a superuser, otherwise any scripts that utilize the GPIO library will not work.
Time For Your First Project
Now that you have everything setup, you are ready for your first project. Click here to for a very easy tutorial that will teach you how to turn an led on and off using Python.