Even if you’ve never connected external devices to your Raspberry Pi, you can’t fail to have noticed the double row of 40 pins at the edge of the board (26 pins on the early Pis).
This is the GPIO header, otherwise known as the general-purpose input/ output connector, and it provides a means of interfacing to real-world devices. Before starting to
think about designing circuits to interface to the Pi, therefore, it’s important to understand the basics of the GPIO hardware.
Power and ground pins
Although referred to as the GPIO header, not all the pins connect to the GPIO hardware. Some of the other pins provide power and ground connections that are also used by hardware that’s connected to the header.
The Pi’s GPIO header has eight ground pins (GND), which you can identify from the
documentation. Ground is equivalent to the negative side of the power supply and is often
referred to as 0V (ie zero volts).
The GPIO header also has four power supply pins: two that provide +3.3V and two that provide +5V. Using the ground and power supply pins allow you to obtain power from the Pi for your external interface circuitry so you don’t need a separate power supply.
With two exceptions, the remainder of the pins on the GPIO header are GPIO pins although some also have secondary functions that we’re not going to get embroiled in here.
As the phrase ‘generalpurpose input/output’ suggests, these pins can be configured in
the software to act either as inputs or outputs. When programmed as inputs, these
pins could be connected to a switch, for example, and the software would be able to read
whether the switch was open or closed, i.e. on or off.
Alternatively, when programmed as outputs, these pins could be connected to an LED, and the software would be able to turn it on or off.
GPIO PIN NUMBERING
There are two numbering schemes for GPIO pins. First, there’s the physical numbering. This reflects each pin’s position on the header, so it runs from 1 and 2 at one end to 39 and 40 at the other. Then, for the actual GPIO pins (as opposed to power supplies), there are GPIO numbers. You can choose to use either scheme in the software.
The Raspberry Pi’s GPIO operates from a supply of 3.3 volts, so you should never, ever present a higher voltage to any of the pins. Doing so will probably destroy your Raspberry Pi. However, there are ways of interfacing to devices that require higher voltages.
In fact, there are even ways of interfacing to mains-powered equipment and this is also
something we’ll discuss. The maximum voltage isn’t the only way of exceeding the GPIO’s maximum rating; you should also adhere to its maximum current of 16mA (and a total of 50mA for all GPIO pins). In practice, this means that you’ll easily be able to drive an LED,
which doesn’t require much current, but driving a higher-powered device such as an electric motor requires a bit more attention.
Exceed limits safely
Interfacing an output device such as an LED to a GPIO pin requires that the device doesn’t require more than 3.3V and it doesn’t draw more than a GPIO pin’s maximum 16mA current. Devices such as electric motors and blue or white LEDs, that
require a higher voltage supply and/or draw a higher current, need special treatment.
The solution is to use a transistor, which can be thought of as an electronic switch. A small current from a GPIO pin turns the transistor on or off, thereby turning on or off a separate circuit involving a higher voltage and often a higher current than a GPIO pin can supply. This secondary circuit might use the 5V on the GPIO header as its supply but, if a higher voltage is required or the current will be higher than the GPIO header can supply, an external power supply will be needed.
The circuit diagram shows the general configuration. When 3.3V is applied to the
transistor’s base via the resistor, it will turn on, a condition that you can think of as the transistor’s emitter being connected directly to its collector.
This means that a current can flow from the high voltage supply, through the load (ie motor, blue LED etc) to 0V and so the motor will spin or the LED will illuminate.
Choosing the type of transistor and the value of the resistor is quite an involved process, especially since there is such a staggering choice of different transistors. While we can’t fully cover this topic here, we can give some pointers. First of all, in the circuit shown, the transistor must be of the type referred to as an NPN bipolar transistor.
Transistors of this type will also be defined by their gain and the maximum current and voltage you can use on the emitter-collector circuit. Here is where it starts to get involved, but just let’s say that for voltages up to 24V and currents up to 250mA (conservative limits), a BC337 would be ideal. With this type of transistor and for this
current, a 1k resistor would be suitable.
Finally, if your load is a motor or a relay, it is important to wire a diode in parallel with the load (cathode to the supply) to suppress reverse currents which these components can generate and which could destroy the transistor. A 1N4148 would be suitable.
You may also like to read these articles