RPi Tutorial EGHS:LED output

Warnings
While most of these circuits may interface directly to the RPi, the use of a buffered interface (such as the one supplied by the Gertboard) is recommended which will help protect against damage. Alternatively, experiment with one of the Alternative Test Platforms.

'''Extreme caution should be exercised when interfacing hardware at a low level, you may damage your RPi, your equipment and potentially yourself and others. Doing so is at your own risk!'''

Aims
The purpose of this guide is to enable control of an LED via the GPIO pins of the RPi.

This is the embedded version of writing a program to display "Hello World" and is the first step in getting started.

The first stage will be to build the hardware we are going to use, and then we shall look at the software which will drive it.

Note: Until RPi devices are available, I can not confirm this will work on a real RPi. For now, I shall be using the TI LaunchPad (see Alternative Test Platforms for details) to test the hardware on (as it is cheap and the logic levels similar).

Theory
This is only a brief and rough overview, since the basics are covered in a lot more detail in many other places (see below).

The GPIO pins on the RPi when defined as an Output is able to cause the voltage on the pin to go HIGH (source) or LOW (sink). This allows signals to be sent to other processors and devices like LEDs. However it is important to remember that the pin will only be able to Source or Sink very small currents, so higher powered devices (such as motors) can not be driven directly from a GPIO pin.

NOTE: Depending on the specification of the RPi GPIO pins, the current SOURCE ability may be better, than the SINK (or vice-a-versa). i.e. If the RPi is able to SINK more current than it can SOURCE, then any driving circuit should be between the RPi 3.3V pin and the GPIO pin (rather than GPIO pin and GND).

For additional detail see [Introduction To Embedded Programming - GPIO Output]

Circuit 1 - Basic LED Driving Circuit
The resistor R1 is used to limit the current going through the LED (which has hardly any resistance), without the resistor, the LED will draw as much current as it can until it burns out (or burns out your GPIO pin).

The value you select for R1 will depend on the current required by the LED (upto 20mA depending on the LED used - check the datasheet) and the source current limit of the GPIO (launchpad is ~20mA), the RPi has a 50mA limit for the 3.3V supply line.

We also need to know the forward voltage required by the LED to light, typically around 2V-3.5V depending on colour.

Finally, the output voltage of the RPi (and LaunchPad) GPIO is 3.3V output level.

Vout = 3.3V Vled = 2V (I'm using RED) Iled = 5mA = 0.005A

R1 = (Vout – Vled)/Iled = (3.3 - 2)/0.005   = 260ohms (so 270ohms is closest preferred value)

If in doubt, use a bigger resistor (=less current & less brightness) and test if good enough by connecting across the 3.3V and ground pins (if you are just experimenting you are unlikely to need LEDs shining at their full brightness anyway).

For instance, one of my test circuits uses 470ohms (which only gives 2.7mA on 3.3V, but the same circuit can be connected to a 12V supply without blowing the LED - rated @20mA).

Basic LEDx8 Test Module
Basic LEDx8 Test Module (Control pins at top, GND connection at bottom-right).

The above test module has been built to allow easy testing of GPIO outputs by driving up to 8 LEDs. The resistor value 330 ohms is used (keeps the current draw fairly low).

Circuit 2 - Higher Power LED Driving Circuit


This section is rough outline of ideas

In order to drive a slightly higher current, the use of a transistor circuit will be required. Since all the driving current will be drawn through Vcc and through the transistor, the RPi 5volt line can be used for Vcc (this will limit the available current to 1Amp total draw from the USB supply itself - including the RPi draw). The current limit will the nominal current the transistor can handle.

The value of R1 is similar to before, but since the driving voltage is higher, less current is needed for the same brightness. Detail calc here.

The use of a transistor, allows the bulk of the driving current to pass through the transistor to ground, with only a small switching current required to be driven from the GPIO pin.

The value of R2 unlabled!!! is determined by this switching current, as follows: detail calc here!

Even transistors have a limited amount of current handling ability, which can be improved by coupling together as a Darlington {ref to wiki} pair (often available in a single package). Also higher powered switches such as mosfets, and even relays can be driven for higher power requirements.

The Software
While the RPi is not available, I can only confirm the TI LaunchPad code works for me.

TI LaunchPad
Sample test code for Basic LEDx8 Test Module (tested on TI MSP430G2553 device).

Basic LEDx8 Test Module input pins 0-7 wired to device Port1:0 to Port1:7, plus GND connection.

Code:
 * hwmap_ledx8module.h - Hardware Mapping File (LED0 to LED7 mapped to GPIO Port1:0 to Port1:7)


 * main.c - Main calling functions


 * ledx8module.c - Sample routines


 * ledx8module.h - Mapping LED types etc

RPi
The above circuits should work with code similar to that given in (RPi Low-level peripherals) section.