Connecting to the GPIO pins of the Raspberry Pi

Raspberry Pi - Connecting to GPIO
One of the most exciting features of the Raspberry Pi is the presence of the GPIO pins, which makes it possible to connect the Pi to some custom hardware which could otherwise not be able to communicate with it on standard ports. They are basically some general purpose pins, which you can control from software arbitrarily. They can be used as input, to read information from other devices, such as sensors, for example, or they can be used as output to send signals to external hardware, such as LEDs, motors, speakers, etc. Some of the GPIO pins also have dedicated functions, allowing the Raspberry Pi to communicate with other hardware through some standard protocols (serial communication for example; they can be used as SPI or I2C lines).
In order to be able to connect to the GPIO pins of the Raspberry Pi, we’ll need to clarify where those pins are exactly. We will also have to find a way to unambiguously identify the pins, so we will assign numbers to them and we will refer to them using these numbers. Both (A and B) original models have a total of 26 GPIO pins, organized into one pin header, named the P1 header. The newer Raspberry Pi (model B revision 2) adds 8 more GPIO pins in a new pin header called P5, but they are not the same male pins sticking out of the board that we can find on the P1 header, instead they are some little rings to which one can solder wires, so they target more advanced users, with more serious hardware and soldering skills.
Raspberry Pi Model B Revision 2 GPIO Headers

Raspberry Pi Model B Revision 2 GPIO Headers

Not all the GPIO pins are programmable. Some of them are 5.0 VDC or 3.3 VDC positive power pins, some of them are negative ground pins and a few of them are marked DNC (do not connect). The P1 header has 17 programmable pins and the P5 header adds 4 more.
The following two images show how the pins are organized on the P1 and P5 headers and the numbering of the programmable pins according to the WiringPi numbering scheme (Wiring Pi is a project which makes it possible to program the Raspberry Pi‘s GPIO pins in C/C++ in a style similar to the that of the Wiring programming language used with Arduino. The author of WiringPi, Gordon Henderson, has come up with an abstract numbering of the GPIO pins which makes it easier to identify them. This is called the WiringPi numbering scheme). We will use this numbering scheme to refer to the GPIO pins. The below two images were kindly provided by Pi4J.com (Pi4J is a project based on WiringPi, which aims to offer a nice way to program the Raspberry Pi‘s GPIO pins in Java).
Raspberry Pi P1 Header

Raspberry Pi P1 Header – Image courtesy of Pi4J.com

Raspberry Pi P5 Header

Raspberry Pi P5 Header – Image courtesy of Pi4J.com

 

A more advanced description of the Raspberry Pi’s GPIO pins and of their usage can be found here.

 

Limitations

The maximum amount of current that can be safely drawn from a Raspberry Pi GPIO pin is 16 mA, but the total current drawn from all the pins may not exceed 51 mA. If these limits are exceeded, the GPIO pins may burn out or even the whole Pi may become damaged!

 

Example

So let’s see a simple example: How to connect an LED to a programmable GPIO pin on the Raspberry Pi i order to be able to control the LED from software running on the Pi.

We will use a standard red LED, which has a forward voltage of 2 V and we will connect it to the 3.3 V power pin of the Raspberry Pi. To protect the LED, we will include a 330 Ohm resistor in series with it. As you can calculate it using the LED calculator, this will result in a 4 mA current going through the LED. You may safely allow up to 16 mA of current to go through the LED, so you can use resistors down to 82 Ohms. For non-red LEDs which may have a different forward voltage rating, you may have to use resistors with different values.

The programmable GPIO pin 0 (according to WiringPi numbering) will be set to output mode. Whenever the pin is set HIGH from software, the LED will light, whenever the pin will be set LOW, the LED will stay off.

Raspberry Pi model B controlling one LED

Raspberry Pi model B controlling one LED

The above illustration was created with a great tool named Fritzing.

 


Comments

Connecting to the GPIO pins of the Raspberry Pi — 4 Comments

  1. I think you got the pin numbering for P5 wrong.
    The pin numbering for P5 has to be seen from the other side of the board.

Leave a Reply

Your email address will not be published. Required fields are marked *