Simple Raspberry Pi GPIO Circuits

Interacting with electronic circuits is one of the great things you can do with the Raspberry Pi, utilizing it’s General Purpose Input Output (GPIO) ports. If you are new to making electronic circuits then you do need to be careful as it is possible to damage your Pi by applying too much voltage to or drawing too much current from the pins. There are many Raspberry Pi beginner projects and here I will show you how to safely connect an LED and button to the GPIO pins and then control the LED using the button. While these circuits and the python code are quite simple controlling inputs and outputs is a great place to start before tackling more complicated projects.

Materials

In this tutorial I will be using the following materials:

Electronics

Connecting A LED to a Raspberry Pi

Connecting a Light Emitting Diode (LED) is one of the things most people try when first building a circuit on the Pi. LED’s are like other diodes in that they will only allow current to flow in one direction and require a minimum voltage to “turn on”, once the LED is turned on the flowing current is converted to light and this is what we see. They come in several different colors, the most common being red, orange, yellow and green which all require a different amount of voltage to turn on. When connecting an LED to your Pi it is important to ensure you always have a resistor in series to prevent large currents flowing as this will very quickly burn out your LED.

In the following examples, I will use the Broadcom pin numbering convention as shown below, as that is what is shown on the T-Cobbler.

Most common LED’s require a forward (turn on) voltage of between 1.2 – 3.6V depending on the color and a current of 10 to 30mA with 12 to 20mA being common. So to calculate the resistor value that we require we use the simple Voltage = Current x Resistance (V=IR) formula or in this case R=V/I. So assuming that we are using a Red LED that requires a forward voltage of 1.8V and that we want a maximum current of 10mA.

Resistance = (Source Voltage – LED Forward Voltage) / Max Current

Resistance = (5V – 1.8V) ⁄ 0.01A

Resistance = 320Ω

If you wanted a maximum current of 20mA then you would replace 0.01 with 0.02 and end up with a resistor value of 160Ω, these are minimum values so you would select the next highest standard resistor value. It should be noted that the Pi’s 5V power supply can provide approximately 200 – 300mA, this should be considered when adding multiple LED’s to a circuit.

Increasing the size of the resistor would drop the current and dim the LED, this, however, is not recommended and you should probably never drop the current below 5mA as the LED may turn off completely. To dim an LED properly you should use the PWM facilities of the Pi but that is beyond what I am going to discuss in this article.

To start, shutdown your Pi, the simplest circuit we can then make is to connect the 5V pin of the Pi to a 330 Ohm resistor and then through the LED and back to Ground (GND). As mentioned earlier the LED will only pass current in one direction so it needs to be placed in the circuit the right way round. Current passes from positive to negative through the LED, the shorter leg of the LED is the negative one if you have cut the legs down to the same length the other way of identifying the polarity is that the resin body itself will have a notch or flat spot on the negative side.

Once you have built and double-checked your circuit restart your Pi and the LED will illuminate.

Now we have a working LED circuit we can add some control with the GPIO’s. A GPIO pin on the Pi can safely provide (source) a maximum 16mA of current. As you can see above if we choose an appropriate resistor value then an LED could be connected directly to a GPIO pin without sourcing too much current.

The Pi 3.3V supply was designed to supply a current of 3mA per GPIO pin. If you load each pin with 16mA the total current sourced on a Pi 3 would be 416mA and the 3.3V supply could fail under those conditions. To ensure we do not damage the Pi we should try to keep the current as low as possible (1-3mA).







Controlling the LED

A better solution is to use a Field Effect Transistor (FET) as a switch to turn on the LED. We can take the existing circuit and add the FET by connecting the Drain to the -ve leg of the LED, connect the Gate to GPIO pin 17 and connect the source back to Ground. This will allow us to apply a voltage to the gate of the FET which will close the connection between the Drain and Source and allow current to flow through the LED. The advantage of this is that the FET draws almost no current from the Pi’s GPIO pin to achieve this.

The following python code will blink the led every 0.5 seconds (2Hz), I am using the RPi.GPIO python library to control the GPIO, another good option is WiringPi but it does not come standard on the Pi and you will need to import it.