Using the Raspberry Pi GPIO with Python
So you got your Raspberry Pi, installed an OS and using it just like your computer. Great! Now what? You didn’t buy a Raspberry Pi just to replace your computer, did you? Well, Raspberry Pi can do a lot more… much more than your computer can do!
Did you notice that there are lots of tiny little pins on one corner of your RPi? These are called “General Purpose Input Output” pins (or GPIO pins). These pins allow your RPi to be connected to the external world. Raspberry Pi Models A and B have 26 pins (17 GPIO) whereas the new model B+ comes with 40 pins (26 GPIO). The model B+ is pin compatible with models A and B.
The easiest way to control these pins is to use the RPi.GPIO Python library. The library comes pre-installed with the latest Raspbian OS. In any case, we’ll learn how to install the library, just in case you find it missing. After this post, you should be able to perform simple I/O operations using your RPi’s GPIO pins.
- Raspberry Pi Pin Configuration
- The RPi.GPIO Python Library
- Hello World with Raspberry Pi – LED Blinky
- A Level-up: Reading Inputs
- Test: Design a Counter
- What now?
Raspberry Pi Pin Configuration
As mentioned earlier, RPi models A/B and B+ are pin compatible. The following image (source raspberrypi-spy.co.uk) shows the pin layout of models A/B (rev 2) and B+.
Model B had two revisions in which the pin configuration changed a little. The RPi model B consists of pins 1 through 26, of which only 17 of them can be used as GPIO. The remaining pins consist of power supply (5v and 3.3v), ground and serial pins. The RPi model B+ consists of all the pins shown above, out of which 26 can be used as GPIO.
The labels mentioned on the left and right of the pins refer to the pin numbers on the BCM2835 CPU. For instance, pin GPIO16 of BCM2835 CPU is connected to pin 36 of RPi B+. Makes sense? What difference does it make to you – we’ll see in a while! 🙂
The RPi.GPIO Python Library
Now let’s get to the point. In order to control the GPIO pins of the RPi, we’ll use the RPi.GPIO Python library. Starting version 0.5.6, the library has support for RPi model B+ as well. While the library is the best way to access and control the GPIO pins, it still lacks support for SPI, I2C, hardware PWM and serial functionality, which are planned to be added (this is with respect to version 0.5.6. In future, this might change).
This is a really simple library which allows you to read to and write from any GPIO pin by various means (like polling, triggers, events, etc). In this post, we’ll discuss the polling method (don’t worry about the name, we’ll deal with it later. The concept is rather simple), whereas I’ll deal with the other methods in my next post.
Method 1 – Install from Repositories
The RPi.GPIO library comes pre-installed with the latest version of Raspbian. In case it doesn’t, all you need to do is to install the latest version from the repositories by running the following in the terminal (holds good for Raspbian only)–
That’s all you need to do! Simple, eh?! 🙂
Method 2 – Build from Source
If you want to build from the source, first download the library from here. Once downloaded, you need to extract it. Open up the terminal, browse to the directory where you downloaded the library and type in the following to extract (replace the name of the file with the one that you downloaded)–
Then enter into the extracted folder.
Before we proceed towards installation, make sure Python is installed in your system (which should be since it comes pre-installed in all OS images). In case there’s no Python installed, type the following to install–
And then, install the package by typing–
That should be it! You’re all set to blink some LEDs! 🙂
Hello World with Raspberry Pi – LED Blinky
A “Hello world” program is a typically one of the simplest programs that one can possibly write to illustrate the basic functionality of any device. In case of Raspberry Pi, it is, like any other embedded system, blinking an LED. This not only gives beginners an idea of how things generally work, but also makes sure that all the tools and devices are properly set up and working properly.
In this post, we’ll program in Python. It can be programmed in a ton other languages as well, but we’ll stick to Python for now because of its simplicity. I’ll dedicate another post on how to do the same using C, and may be another post on how to write your own Linux kernel modules to control the GPIO. 🙂
A little background
For those who are not familiar with circuits, this might seem a challenging task. So here’s a little background on the concept of blinking LEDs. If you are not already familiar, LEDs are little devices that glow and emit light of some color (red, blue, green, yellow, white, etc) upon providing a positive voltage across its pins (forward bias). If you reverse the polarity, the LED does not glow. In the adjoining figure of an LED, you must make sure that the voltage drop across the LED (from +ve to -ve pin) is positive to make it glow.
If you connect a 5 volts supply across an LED directly, it’ll simply blow up since it exceeds the voltage rating of the LED. To prevent that, we usually connect a current limiting resistor in series with the LED to divide the voltage drop, so that your LED is safe and keeps on glowing. Usually any resistor having value in between 100 ohms and 1kohms will work good for this. But don’t connect a resistor having too high resistance. This will not leave enough voltage across the LED to make it glow.
The GPIO pins of your RPi are capable of generating these different voltage levels. This is why you program the RPi – to generate a particular voltage level on a particular pin.
Alright, now let’s get to the circuit diagram.
Disclaimer: We ABSOLUTELY DO NOT take any responsibility of you messing things up. The circuit shown below is tested and works just fine, however if you make wrong connections and/or short the board and damage your Raspberry Pi, it’s all on you. In short, PROCEED AT YOUR OWN RISK. Also, beware of ELECTROSTATIC DISCHARGE. The GPIO pins of your Raspberry Pi are connected directly to the BCM2835 CPU’s pins. There is no protection. So don’t mess around. If you don’t feel confident, try using something like a Gertboard as an intermediate interface.
We’ll be using a solderless breadboard to make the circuit. If you don’t know how to make circuits on a breadboard, try watching this video first. Things that we’ll need are–
- Raspberry Pi and accessories
- F/M jumper wires (a combination of F/F and M/M will also work)
- An LED (3mm or 5mm)
- Two 330 ohms resistor
- One push button
Check out the following circuit. We’ll implement the region marked in red for now, which is the LED blinky. Notice that pin 7 of RPi is connected to the positive end of the circuit and pin 6 (which is ground) is connected to the negative end of the circuit.
This means that when pin 7 is driven high, a high voltage level (usually 5v) is generated at pin 7, which glows the LED. And when pin 7 is driven low, a low voltage level (usually 0v) is generated at pin 7, which turns off the LED.
Cool! Once you’re done making the circuit in the red region, proceed to the next section where we learn how to program your RPi using the RPi.GPIO library.
Using RPi.GPIO Library
The RPi.GPIO package allows us to control the GPIO pins by means of classes. We start by importing the modules by typing–
This allows us to refer the module by simply mentioning GPIO instead of its full name RPi.GPIO.
Specify Mode of Operation
The next step is to specify the mode in which we’ll be using the module. Remember that we mentioned that RPi (BOARD) and the CPU (BCM) have different pin numbers? This is where this matters. You need to choose between the two systems. You can do so by typing–
Set up a Channel
Next we need to set up the desired channel as either input or output. This can be done by typing–
For instance, for the LED blinky, we connected pin 7 on the board to the LED. This means that we must configure that pin as an output channel. Also, recall that that pin 7 on board is mapped to GPIO4 of the CPU. So this is how you do–
It is recommended that you follow the BOARD numbering system since it is easier to follow. In the end it doesn’t matter which one you follow, just choose one and stick to it.
Drive a Channel
Next step is to drive the channel high or low. This can be done by typing–
For instance, to drive pin 7 high, we type any of the following–
To drive the same pin 7 low, we type–
Read a Channel
In order to read the value of any GPIO pin, simply type–
We’ll learn how to use in later in this tutorial. We don’t need it for LED blinky since all our pins are output pins.
For more detail: Using the Raspberry Pi GPIO with Python