Objective

Use basic digital I/O on a Raspberry Pi from the command line and Pd.

This exercise assumes you have the basic Raspberry Pi materials as specified in 1.c.iv.1, as well as the following materials:

  1. switch
  2. 10K resistor
  3. incandescent bulb
  4. 2N3904 transistor

Overview

The Raspberry Pi B+ we have in stock include 40-pin header with a number of digital I/O pins, including I2C and SPI busses. One pin (GPIO18, pin number 12) can generate high-frequency PWM signals. Analog to digital conversion can be performed by attaching an external ADC via either SPI or I2C.

We are using the wiringPi interface library for convenient access to this hardware interface. The gpio command-line program is a quick way to inspect and configure the state of the interface. We also provide a pdwiringPi external for Pure Data for basic access from Pd patches. For more advanced usage, we provide the WiringPi2-Python module for hardware access from Python scripts. It is also possible to write C/C++ programs which directly use libwiringPi.

Steps and observations

The gpio command. The simplest way way to see what the gpio command can do is just run it; it will print out a list of options. The gpio readall command will show a table with the current state of all the interface pins.

Pin numbering. There are three different ways that the pins are numbered, which can be confusing. The physical pins numbers are defined by the 40-pin header in the conventional order. The CPU defines a numbering based on the internal GPIO device registers; these are referred to as the Broadcom or BCM numbers by wiringPi, but are shown as GPIO on the Raspberry Pi diagram.

Confusingly enough, the wiringPi library and gpio command define another pin numbering scheme, show in the readall display as the wPi columns, next to the BCM columns showing the Broadcom hardware scheme, and the Physical columns at the center with the the physical scheme.

Reading an input. Set up the circuit in the associated diagram with a switch and pullup resistor attached to physical pin 11 and 3.3V. Note that the Raspberry Pi is a 3.3V device, so digital inputs should not exceed the 3.3V supply voltage.

Then try the following:

  1. Configure WiringPi pin 0 (BCM 17, physical pin 11) as an input:
    gpio mode 0 input
  2. Read the state of pin 0 as follows, which should change with the state of the button:
    gpio read 0

Configuring a digital output. The following steps will control a lightbulb on physical pin 12/BCM 18/wPi 1. The Pi doesn't provide enough current or voltage for the lightbulb, so you will need to use an external power source with a 2N3904 transistor. Connect the base pin of the transistor to physical pin 12 from the Pi. Then try the following:

  1. Configure WiringPi pin 1 (BCM 18, physical pin 12) as an output:
    gpio mode 1 output
  2. Write the state of wPi 1 as follows, which should turn the light on and off:
    gpio write 1 1
    gpio write 1 0

Configuring a PWM output. Using the same circuit, this will configure the pin for PWM output for variable brightness:

  1. Configure WiringPi pin 1 (BCM 18, physical pin 12) for PWM output:
    gpio mode 1 pwm
  2. Set the lightbulb to half-brightness:
    gpio pwm 1 512

Using wiringPi from Pd. Find the associated Pd patch and run it as follows: sudo pd 1.c.iv.2_rpi-pdwiringPi.pd. Pure Data needs to be launched with root privileges for fast hardware access. The text within the patch explains the next few steps, which replicate the previous steps within Pd.