If you look at a Raspberry Pi, it has this odd set of Headers in the back right corner, these pins are called the GPIO pins. GPIO stands for ‘General Purpose Input/Output’, and we can use these to interface with off-board components. This header I speak of, it has 26 pins, just like and old serial cable, and it is difficult to breadboard with, but a great NYC company has created a small item to solve that problem.
Adafruit has created the Pi Cobbler Breakout. It has a 26 pin socket on the top, and on the bottom, nicely spaced headers with labels for quick and easy breadboarding. This kit does require some assembly, but it is very simple, and should take less than 15 minutes for people just getting started with soldering. The kit laso includes a 6 inch GPIO cable, and all the necessary parts, at the time of writing, $8 is a great price for such a convienent utility.
Adafruit Pi Cobbler Breakout
The Raspberry Pi Community has created, and is still developing a very nice GPIO Python library. To use this library, we must use and install python 2.x
First we need to install the Python-Developer-Kit and the Python Package Manager, PIP.Next, we need to update our Python distribution using the following command.
Finally, we need to install the RPi.GPIO library.
In this first example, we will simply blink an led, without using any non-essential libraries. The code is as follows:
Use your favorite text editor on the Pi, to copy and paste the code. next make it executable by using this command. Remembering to insert the name of your file.
The sudo is necessary, because you are want to access low-level components on the Pi.
In this example we want to do something more complex, we want to read the analog value of a potentiometer. But Wait, you will say, the Raspberry Pi doesn’t have any Analog inputs. Right you are, but this is the electronics world, there is an ic for everything.
MCP makes a ADC -Analog to Digital Converter, the 3008 Chip, Adafruit caries it for $4 and it can be found here. The chip uses SPI to communicate with the Pi.
First we need to un-blacklist the SPI driver. So go to /etc/modprobe.d/raspi-blacklist.conf and comment out both lines, which allows the use of SPI and I2C, which we will use later. Reboot the Pi, and then do aYou should see something like this: [box]/dev/spidev0.0 /dev/spidev0.1[/box] Once SPI has been enabled, we need to be able to use it in our Python program, to do this we are going to use a library called [spidev](https://github.com/doceme/py-spidev). To do this we will first need to install git on the Pi, using apt-get.
Next, we need to cd into out home directory.Then we will clone the spidev git repository using git clone.
After the Pi has finished cloning the repository we will ‘cd’ into the new directory.Once in the directory, we will execute the install script.
Wiring It Up
Connect the Cobbler and the MCP3008 as follows:
|MISO-Master In Slave Out||Dout|
|MOSI-Master Out Slave In||Din|
If you have wired everything up correctly, your Pi now has 8 analog inputs which you can pull via SPI. To check that everything works correctly, attach a potentiometer to port 0 on the ADC, and provide it with 3.3v and ground. Copy/paste the example code below into text editor on the Pi, and make it executable.You should see a new value between 0 and 1023 appear every 1 second, that correlates with the position of the pot.
Going Further/Example 3
Instead of overwhelming your terminal screen, let’s use a display to present our analog values. Adafruit carries a very nice display, pictured below, and they are available in a variety of colors. The best thing is, they come as a kit, with the display, and a I2C backpack, which lets us control the entire display, with only 2 wires, and 2 additional wires for power.
Adafruit 4-Digit 7-Segment Display in White
To use I2C on our Pi, we need to add a few modules to our Pi. First we need to install a few packages to allow is to use I2C in python, and discover I2C devices.
Next, we need to add the following code to **/etc/modules **
Now, Reboot. and when your Pi boots up again, it will initiate I2C. enter ls /dev/i2c* and you should see:
Adafruit has a Repository on GitHub with all their libraries, but I have created some libraries, based on the adafruit code, but they include some user improvements. The demo code calls for:
- 3 leds- a green, yellow, and green
- photo-cell with 10k? resistor
- 3 buttons- I like these Colorful Square Tactile Button
- 3 10k? or higher resistors
- Adafruit 7-Segment Display with I2C Backpack
- Pi Cobbler
- (Optional) Multimeter -But Very Helpful
Connect the components as follows:
|Green Led||GPIO Port 22|
|Yellow Led||GPIO Port 21|
|Red Led||GPIO Port 4|
|Potentiometer||MCP3008 Port 7|
|Light Sensor/PhotoCell||MCP3008 Port 6 and 3.3V|
|10KΩ Resistor||PhotoCell and Ground|
|Green Button||GPIO Port 25|
|10KΩ or More Resistor||Green Button and 3.3V|
|Yellow Button||GPIO Port 24|
|10KΩ or More Resistor||Yellow Button and 3.3V|
|Red Button||GPIO Port 23|
|10KΩ or More Resistor||Red Button and 3.3V|
|7-Segment||5V, Ground, SCL, SDA|
My Breadboard, with a few extra components
After you have double checked your wiring, clone the git repository containing the necessary libraries, and example code using the following command:
Ard.py, is the resource library with all the simplified functions, and App.py, is the designated executable. Press the Green Button for the Pot value to be displayed, press the yellow button to display the light sensor reading. The red button kills the application.
The Red led blinks every time a reading is taken, the Yellow and Green leds indicate which mode the app is in, they match with the button colors.
If something doesn’t work, double check your wiring, and that all previous steps were completed correctly.