RPi GPIO Interface Circuits

This page shows a number of common circuits used for interfacing the Raspberry Pi's GPIO pins to various other electronic circuits.

Please note: this page is currently under construction

=Input circuits=

Voltage divider


This circuit may be used to connect a digital signal from a 5V device to a GPIO pin on the Pi, which requires a 3.3V input level. The Ov point in the schematic should be connected to one of the Ground lines on the Pi's GPIO connector, and also to the 0v (or Ground) signal on the input device.

The important thing here is the ratio of R1 to R2; R1 should be just over half R2's value to ensure 5V is divided down to 3.3V. The values shown here should be suitable for most uses.

(For more information, see the Wikipedia article on voltage dividers).

=Output circuits=

The GPIO pins are connected directly to the BCM2835 chip at the heart of the Raspberry Pi. These provide only a 3.3V output level, and are not capable of supplying much power. More importantly, if they are damaged through misuse the Pi itself will need to be replaced.

So, if you are connecting anything more than a small LED to the GPIO output, you should use an additional circuit to boost the voltage and/or current.

Using an NPN transistor


This shows a cheap NPN transistor being used to switch a load on and off. When the GPIO is 'high' (logic 1) the load will be turned on; when it is 'low' the load will be off. The circuit shows a 5V power rail, but with the 2N3904 transistor shown, a supply of up to 40V may be used (this is the VCEO value given in the transistor's datasheet). It should be suitable for a load current of up to 100mA.

For bigger loads, you will need a bigger transistor to switch higher voltages (the VCEO value) or currents (the IC value), but note that you will need a transistor with a higher gain (the hFE value) as well. The gain is the ratio of the current in R1 (in this case, about 2.5mA), to the current in the load: a 1A load will need a gain of 400. So-called Darlington transistors, such as the TIP112 (1A, 100V) provide high gain at high currents.

Using a FET


A field-effect transistor is another alternative to the ('bipolar') transistor shown above. Again, when the GPIO is 'high' (or 1), the load will be switched on, and 'low' or 0 switches the load off. Resistor R1 is provided for safety; it ensures that the load is switched off if the GPIO is set to be an input.

As with the bipolar transistor circuit, supply voltages higher than 5V may be used for the load. The 2N7000 device has a maximum (VDS) voltage of 60V; it is suitable for load currents of about 100mA.

For bigger loads, you can choose a bigger FET: the current rating (ID) and voltage rating (VDS) are given in the data sheet. Unlike bipolar transistors, FETs do not have a 'gain' to worry about. Instead you should look for a 'threshold' voltage (the input voltage at which the FET begins to turn on) of 3V or less, so that it is properly 'on' when driven from the GPIO's 3.3 volts. Some datasheets don't give a threshold voltage - instead they give a value RDS(on) measured at 2.5 or 2.7 volts. If the FET isn't suitable for 3.3V logic input, this value won't be given. Fairchild's FQP30N06L has a low threshold voltage (2.5V max) and can switch 32 amps at 60 volts.

Driving a relay


If you are connecting an inductive load (essentially this means anything with windings, such as a relay, a motor, or a solenoid) it is advisable to fit a diode D1 as shown above (note that the cathode is connected to the positive supply rail). When an inductive load is switched off, a voltage spike is produced which can exceed the maximum rating of transistor Q1; the diode 'clamps' this voltage to prevent damage.

=Level Shifters=

Many digital logic circuits (particularly older ones) use a 5 volt power supply. These cannot be connected to the GPIO signals on the Pi, which uses a 3.3V power supply: the Pi GPIO will output +3.3V for a logic 'high', which is less than the 3.5V (= 0.7 * VDD) required by most 5V CMOS logic. Also, a 5V logic output will send an excessively high voltage into a GPIO input, which may damage either or both of them.

The solution is a level shifter circuit. Level shifters are available in IC form or as pre-built modules from many suppliers (e.g. Adafruit, Sparkfun, Cool Components), but it's quite easy to make your own.

Classic MOSFET level shifter


This uses a single MOSFET; when the GPIO is 'low', the MOSFET is turned on (because the gate-source voltage is 3.3V), so the 5V side is connected to the GPIO pin - it is therefore also low. When the GPIO is 'high' the MOSFET is off (the gate-source voltage is near zero), and R2 pulls the 5V side high.

The circuit also works in the other direction, and lets you safely drive a 3.3V GPIO input from a 5V source. Q1 has a (hidden) diode between the source and drain terminals - if the 5V logic side is driven low, this will pull the GPIO low through the diode. When the 5V logic side is high, everything is off and the GPIO will be pulled to 3.3V via R1.

For this reason, the above circuit is often used to convert levels on bidirectional buses such as I2C. Note that for an I2C bus, you will need two copies of the circuit, one for the SDA line, and one for SCL.

Simple diode circuit


This quick-and-dirty circuit has been used successfully to connect the serial port of a 5V ATMega328 processor (as used on many Arduinos) to the Pi's serial port. The 0 and 3.3V logic levels on the Pi's TxD pin are shifted (via D1 and R1) to approximately 0.7V and 4.0V. This is within specification for the ATMega328's RxD pin, so the logic levels are recognised correctly. Note that it is only just within specification; this means electrical noise (interference) can cause errors more easily than when using a 'proper' level shifter.

There is no level conversion between the ATMega328's TxD and the Pi's RxD. Instead, resistor R2 limits the current flowing from one device to the other to a safe value (under 0.5mA).

-

Note: If you wish to produce new or modified schematics matching the ones above, you can use Inkscape using symbols from File:Circuit_Symbols.svg.