Getting Started with Raspberry Pi Part 3

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 BreakoutAdafruit Pi Cobbler Breakout

Using GPIO

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

Installing Python

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.

Example 1

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.

Now, you can finally execute your program, and because of your “Hash Bang” which looks like this(#!/usr/bin/python) you don’t need to have the preceding “python” in the terminal when you want to execute. Simply enter:

The sudo is necessary, because you are want to access low-level components on the Pi.

Example 2

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.

MCP3008 Chip MCP3008 Chip

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.

Using SPI

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 a

You 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

MCP3008 Pin-Out MCP3008 Pin-Out

Connect the Cobbler and the MCP3008 as follows:

**Raspberry Pi****MCP3008**
3.3vVDD
3.3vVref
GroundAGND
CLKCLK
MISO-Master In Slave OutDout
MOSI-Master Out Slave InDin
CE0CS/SHDN-Chip Select
GroundDGND
 

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 WhiteAdafruit 4-Digit 7-Segment Display in White

I2C

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:

/dev/i2c-0 /dev/i2c-1

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
  • Potentiometer
  • 3 buttons- I like these Colorful Square Tactile Button
  • 3 10k? or higher resistors
  • Adafruit 7-Segment Display with I2C Backpack
  • Pi Cobbler
  • Breadboard
  • MCP3008
  • (Optional) Multimeter -But Very Helpful

Connect the components as follows:

**Component****Connects to**
Green LedGPIO Port 22
Yellow LedGPIO Port 21
Red LedGPIO Port 4
PotentiometerMCP3008 Port 7
Light Sensor/PhotoCellMCP3008 Port 6 and 3.3V
10KΩ ResistorPhotoCell and Ground
Green ButtonGPIO Port 25
10KΩ or More ResistorGreen Button and 3.3V
Yellow ButtonGPIO Port 24
10KΩ or More ResistorYellow Button and 3.3V
Red ButtonGPIO Port 23
10KΩ or More ResistorRed Button and 3.3V
7-Segment5V, Ground, SCL, SDA

My Breadboard, with a few extra components 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.