Getting Started with the Pi's GPIO Port

The Raspberry Pi's General Purpose Input/Output port is the 26 pin connector labelled P1 at the top left of the board. We use a GPIO breakout board, which enables easy attachment of wires to labelled paddle terminals. Our rainbow ribbon cable connecting P1 to the breakout board is not colour coded in the conventional way (with the end brown wire connected to pin 1). Presumably this is because of the unconventional orientation of P1 on the Pi. GPIO Electrical Specifications provides this warning: "Note that the orientation of P1 differs from the convention used in most connector placement. On most computer boards the header would be rotated 180°, so that the odd-numbered row would lie alongside the board edge, instead of the even-numbered row. That way, the wires or ribbon cables running from the edge of the board are numbered from left to right. The Raspberry Pi board violates the convention, placing its even-numbered rows along the board's edge. So be careful in making your connections."

The demonstrations on the following pages are based on those in the Free Pascal wiki, with some additional detail provided to help school students to get started. We use the pin terminology recommended in RPi Low-level peripherals to distinguish the numbers on the Broadcom chip (e.g. GPIO17) from those on the GPIO port (e.g. P1-11). The programs use Linux system file commands (from the Unix and BaseUnix units) to set up the pins for access and to change or read their on-off state. You could type these commands (prefixed by "sudo") at the command prompt to achieve the same result. All of the Pascal programs must be run with SuperUser permissions. To do this for the program gpio_input:
  1. Type Ctrl+F9 to build the project without running the executable gpio_input.
  2. Change the current directory to that of gpio_input using the cd command.
  3. Type at the command prompt sudo ./gpio_input.

In the simple circuits described below there is a wide range of component properties that will work. For example, you could use a 200 ohm (instead of 1 kilohm) resistor for output to an LED and a 2.2 (instead of 7.5) kilohm resistor to connect to the base of the transistor in the motor circuit.

Output to LED

We used a 3mm 2mA LED in series with a 1 kilohm resistor. Make the connections as follows.

  1. Connect P1-11 (GPIO17) to one end of the resistor.
  2. Connect the other end of the resistor to the positive terminal (longer wire) of the LED.
  3. Connect the negative terminal of the LED to P1-6 (GRD).

See the code for control by a toggle button on a form and by cycling an on-off sequence.

Output to DC Motor

You can use in this circuit a cheap 5V DC motor, a transistor such as Fairchild's BC33825TA or equivalent, a diode such as 1N4001 and a 7K5 (7.5 kilohm) resistor. Use the instructions and/or circuit diagram below when making the connections.

  1. Connect P1-11 (GPIO17) to one end of the resistor.
  2. Connect the other end of the resistor to the base (middle wire) of the transistor.
  3. Connect P1-02 (+5v) to one terminal of the DC motor.
  4. Connect the other terminal of the motor to the collector of the transistor. See the transistor's data sheet to find out which terminal is the collector.
  5. Connect P1-06 (ground) to the emitter of the transistor.
  6. Connect a diode in parallel with the motor for the protection of the transistor. (See an equivalent Arduino example with an explanation). The cathode of the diode (marked with a band on the 1N4001 diode) must be connected to P1-02. Connection the other way round would cause a damaging short-circuit.
Circuit diagram for output to a DC motor

Circuit diagram for output to a DC motor

See the code for control by a toggle button on a form and by cycling an on-off sequence.

A stepper or servo motor should be used for precise control. This requires more complex programming, which we intend to cover in a tutorial.

Input from Switch

For the input program, which records the on-off state of the switch in a toggle box and memo, make the circuit as follows.
  1. ConnectP1-12 (GPIO18) to one terminal of a 7.5 kilohm resistor.
  2. Connect the other end of the resistor to one terminal of a simple push switch.
  3. Connect the other terminal of the switch to P1-6 (ground).


Follow these links to the demonstrations.

Programming - a skill for life!

Setting up the Raspberry Pi, installing Lazarus, use of the GPIO port (with and without PiFace), web servers and remote control