Skip to main content
shopping_basket Basket 0
Login

What outputs can I use with my Raspberry Pi? Part 1 - LEDs

DrLucyRogers
1

This series of articles was written by James Macfarlane and Lucy Rogers. This is part one in a series of four articles on outputs. There is also a two-part series on Inputs Part 1 here and Part 2 here.

Outputs

The Inputs articles (Part 1 and Part 2) looked at how your Internet of Things (IoT) project can get information about the world around it using sensors and input devices. In this series of articles, we’ll look at the other side of the equation - output devices.

Many IoT projects will need to “do something” physical to the world, whether that’s lighting an LED, spinning a motor, buzzing a buzzer or turning on a heater. All these “outputs” boil down to the matter of controlling electrical loads. “Load” is the electrical term for something that uses power (and, usually, turns it into useful work.) In our case, that means controlling loads using the Pi, usually via its GPIO pins.

The GPIO pins only work with logic level signals. This means they can only supply 3.3V and a few mA of current from each pin. This is enough to power one or two LEDs but most things need a larger current. To control these things requires additional “driver” circuitry, and sometimes a separate power supply. For some output devices (e.g. motors) there are Raspberry Pi HATs available that include this circuitry. Some items can be connected in other ways. As with inputs, outputs can also be connected to another microcontroller and controlled from the Pi through a serial link, USB, Wifi or Bluetooth.

This series of articles looks at how to use a Pi to control various output devices. It also describes briefly how some of the common output devices work, for example, LEDs and servo motors. Monitors, screens and sound are also outputs but are not covered here.

Ground loops (Earth loops) can be a problem when driving larger loads from a Pi (or any other computer platform.) This article also explains what they are and how to avoid them.

By the end of this series, you will be able to determine which kind of output device is required for your project, how to control it, and how to add an additional power supply safely. You will also be aware of the most suitable nodes in the software Node-RED to use to control your outputs.

Light Emitting Diodes (LEDs)

LED stands for Light Emitting Diode. It emits light if a current is flowing through it. If you connect an LED the wrong way round, it won’t light up. Being a diode, it only lets current flow in one direction.

LEDs are available in many different colours, including white (which is actually a blue LED with a special phosphor coating.) It is also possible to get infra-red LEDs - these are used in TV remote controls. Some LEDs combine red, green and blue diodes in one package known as an RGB LED (or RGBW if it also contains a dedicated LED for white light.)

LEDs come in all shapes and sizes from tiny surface-mount “chip” LEDs a millimetre wide to 100W arrays which can turn night into day and need massive heat sinks. Probably the most familiar size of LED is the 5mm (also called T-1 3/4) size as shown in Figure 1.

green

Figure 1: A 5mm green LED (228-6004) - (Image Credit: RS Components.)

LEDs need a resistor in series with them (or some other way of limiting the current) to avoid letting out the magic smoke. More on this shortly.

How LEDs Work

LEDs, like normal diodes (the non-lighting-up variety), are semiconductor devices. A semiconductor is a material such as silicon which doesn’t let electrons flow through it as easily as a metal would, but doesn’t stop them completely as an insulator would.

The metal wires which go into the diode are connected to a “chip” made of two slightly different semiconductors, called ‘P’ and ‘N’. This ‘PN’ junction has a special property - electrons flowing across it lose some of their energy. In a normal (silicon) diode, this energy is just turned into heat but LEDs are made of semiconductors like gallium arsenide phosphide which have a special property: the energy is lost in the form of photons (packets of light) instead of just heat (although LEDs still get hot, especially big ones!) The “chip” is placed behind a clear or coloured lens to let out the light. Note that it’s not the lens of the LEDs that give it its colour, but the size of the energy gap that the electrons experience when they jump from ‘P’ to ‘N’. Apart from the ones which contain phosphor layers, like white or pink, LEDs only produce light in a narrow band of wavelengths (colour.) This means that, unlike an incandescent lamp, if you put a coloured lens or colour filter in front, you won’t change the colour, only make it darker.

Incandescent Bulb vs LED Incandescent Bulb:

Incandescent Bulb

  1. Emits light by getting hot.
  2. Produce white light made of a broad spread of wavelengths (colours.)
  3. Has a limited life.
  4. Does not matter which way round it’s connected.
  5. Can be connected directly to a power supply.
  6. Uses quite a lot of current so needs additional circuitry to drive it from a GPIO pin.

LED

  1. Emits light by electrons moving in a semiconductor.
  2. Generally produces light of only one colour (wavelength.)
  3. Has an almost unlimited life if treated right.
  4. Only works if connected the right way around.
  5. Requires a current limiting resistor or constant current supply.
  6. Small ones use very little current so can be driven directly from a GPIO pin.

LED Connections

Diodes have two terminals called the anode (A) and the cathode (K). LEDs are the same. The anode needs to be more positive (higher voltage) than the cathode in order for current to flow. Different LED packages have different ways of indicating which connection is which. On the common 3mm and 5mm LEDs, this is usually done by making the connections different lengths. This can be seen in Figure 1. The anode (+) is the longer of the two leads.

LED symbol

Figure 2: LED Symbol

On the schematic symbol (see Figure 2) the current flows in the direction of the “arrow”.

Understanding LED Data Sheets

The voltage across an LED, when it is connected the right way around, is called the Forward Voltage or VF.

For the LED to start emitting, a minimum VF is needed and this figure tends to be higher for LEDs which have shorter (more blue) wavelengths. E.g. a red LED might have a VF of 2.1V, whereas a blue one might need over 3V to start working. This means that on a 3.3V supply, some blue LEDs might not be able to attain their full brightness.

If connected backwards, the voltage across them is called VR (reversed.) Once a certain value of VR is reached, LEDs stop behaving like diodes, and, if enough current is allowed to flow, they can be damaged. The maximum VR might only be around 5V.

The current flowing through an LED (when connected the right way around) is called IF - the forward current. The manufacturer will give a recommended maximum value for IF (e.g. 20mA) - it’s fine to operate the LED at this current (or less) all day long. They also give an “absolute maximum” value of IF (e.g. 30mA) - if you exceed this a little bit, you’ll reduce the life of the LED. If you exceed it a lot, you might make the LED go “pop” (possibly literally!)

The datasheet should also tell you the value of VF to expect when IF is at the recommended maximum. This is really helpful to know when calculating the value of the current limit resistor (more on this in a moment.)

LED data sheets will also tell you all kinds of other useful stuff like the luminous intensity (measured in millicandela, or mcd - one candela is roughly the brightness of a candle flame,) the viewing angle and the peak wavelength (in nanometres, nm).

How to select a resistor for an LED

There are three main methods for selecting a current-limiting resistor for an LED.

  1. Guess
    2. Use maths
    3. Use an online calculator to do the maths

The first can be useful in prototyping - just try different resistors, starting with something cautious. If using 3.3V or 5V, start with 470Ω and make it bigger if the LED is too bright, smaller if too dim.

The next two methods are more accurate and reliable - they use maths.

These methods are calculation by hand and using an online calculator. For both these methods, you will need to know:

  • Supply voltage of the circuit (VS)
  • LED Current (IF)
  • LED Forward Voltage (VF)

The first is up to you (or maybe fixed by the application - e.g. 3.3V or 5V on the Pi.) The IF you can choose (up to the LED’s maximum value or the limit of your circuit, e.g. 16mA for GPIO pins.) VF will be listed in the LED’s datasheet.

To calculate the resistor value, we can use Ohm’s Law:

V = IR

where:

  • V is the voltage drop across the resistor,
  • I is the current through it,
  • R is the resistance.

but we need to hack it a bit. We want to find R, not V, so first, we re-arrange it:

R = VI

In our LED circuit, the voltage across the resistor is the difference between the supply voltage and the LED forward voltage, so we substitute VS − VF for V. Also, the current through the resistor is the same as the current through the LED, so we can put in IF for I.

Now we have our final LED resistor formula:

R = (VS − VF) / IF

If IF is in amps, then R will be in ohms. If IF is in mA, R will come out in KΩ.

*******************************************************************************************************

R vs Ω

On circuit diagrams, in computer code, and sometimes on resistors themselves, the letter ‘R’ is often used instead of the Greek letter Ω (capital omega) because there isn’t always a symbol font available and it can be confused with a ‘0’ (zero.) The ‘R’, or another multiplier like ‘K’, also sometimes doubles as a decimal point. E.g. 4K7 = 4.7K = 4700Ω.

*******************************************************************************************************

Figure 3: LED and Current Limit Resistor

Figure 3: LED and Current Limit Resistor

Now an example. Say you want to run a 5mm green LED from a GPIO pin on the Pi (see Figure 3) and choose the L-35GD by Kingbright (228-6004) . You grab the datasheet for the LED from your favourite electronics supplier (e.g. L-53GD, Green T-1 3/4 (5mm) Solid State Lamp.) This LED is designed to run at 20mA (absolute maximum IF is 25mA.) 20mA is a bit much for the GPIO pin to drive. So you decide to run it at 5mA instead. The datasheet says the typical VF is 2.2V, measured at an IF =20mA. We could just use this number but it would be nice to know the value at 5mA. Fortunately, there is a little graph on page 2 which shows the relationship between VF and IF. If you look up 5mA, you get a VF of 2.0V. Putting all this into our equation:

R = (VS − VF ) / IF

= (3.3−2.0) / 0.005

= 1.3 / 0.005

=260Ω

This value, although accurate, does not need to be exactly adhered to. Resistors come in standard sizes (called preferred values) which may not be the exact value you have calculated. Use the nearest one - a smaller value resistor will give a brighter LED, a larger one a dimmer LED. In this case, the nearest value is 270R.

You can then “tweak” your resistor choice (within reason) to get the brightness you want. If you have LEDs of several colours and you are trying to match the brightnesses, you might find you don’t want them all to run at the same IF.

If you can’t find the value for VF, you can try a sensible guess (e.g. 3V for a blue or white LED, 2V for any other) to get your circuit working then measure the actual value with a multimeter.

Finally, if you would rather use an online calculator, a quick web search should yield a bunch of options. These won’t help you look up things in datasheets but they will usually pick a preferred resistor for you. Sometimes, they’ll even give you the colour code for the resistor.

If an online calculator gives you a zero value for the resistor, it is because VF is the same as, or bigger than, VS. This might happen if you were trying to run a short-wavelength blue LED off a 3.3V supply. In this case, it is good practice to still use a small resistor since things like VF can change with temperature. For a 3.3V source voltage, 47R would be a sensible choice. Note, though, that the LED might not even light at all, even for R=0.

Many of the online calculators will also figure out the power rating of the resistor that you need but that’s not hard to do yourself. There’s really no way to run into problems with burning out resistors at the currents and voltages available from the GPIO pins but let’s calculate it for our example above, just for fun (or in case you end up designing a project with big LEDs running off higher voltages.)

The power dissipated in the resistor is

P = IF2 R


where: P is in watts, IF is in amps and R is in ohms.

(If IF is in mA and R in KΩ then P comes out in mW.)

In our example, this is

(0.005)2 × 270 = 0.00675W or 6.75mW.

This is a tiny amount of power. Most small through-hole (axial-leaded) resistors start at a power rating of 1/8W and are commonly 1/4W, 1/2W or 0.6W, so there’s no way you’ll burn anything out in this case.

NeoPixels and other addressable LEDs

It can get tricky to control more than a handful of LEDs from the Pi. There are only so many GPIO pins and they can only source a small current each.

Dedicated LED driver chips are a great option if you want to drive a lot of LEDs - they can handle more power and offer more control (such as PWM - more on this later.) They are controlled via a serial protocol and can usually be daisy-chained. This can save a lot of GPIO pins.

You can also get LEDs with driver chips built into one package, called addressable LEDs. These usually combine red, green and blue (and sometimes white) LEDs together with a driver chip into a small surface-mount package and include the WS2801, WS2811, WS2812 and SK6812. You might hear these called NeoPixels. This is the term Adafruit Industries use for the WS2811-based addressable LEDs. Addressable LEDs can be purchased individually or daisy-chained together into long (usually flexible) strings.

NeoPixels (and similar) are connected using three wires: 5V, ground and a data line. Others, like the WS2801, also require a clock signal but have the advantage of being less fussy about the timing of the signals.

The Raspberry Pi GPIO pins have 3.3V logic levels but it turns out that most NeoPixels don’t seem to mind this and can be directly controlled from a GPIO pin. If problems occur, you can convert the GPIO’s 3V3 logic levels up to 5V by using level converter chip, such as the 74AHCT125 (798-8941) . There are HATs and add-on boards, such as the Unicorn HAT (146-2648) , that incorporates a level converter and allow a string of NeoPixels to be connected.

Pins 2 and 4 on the GPIO connector are connected to the Pi’s 5V rail and can be used to provide the power supply for NeoPixels or other LEDs. Note that these pins are not current limited like the data GPIO pins are.

For more information on NeoPixels check out the Adafruit blog post.

NeoPixel’s data line has very strict timing requirements. It is reported the Pi can’t always achieve this. If you have problems, this may be the cause. The alternative is to “outsource” control of the LED strip to a microcontroller, or choose addressable LEDs with a clock line.

Connecting LEDs to the GPIO pins of the Pi is relatively simple but the pins are limited to about 16mA. Even a small load, such as a 1.5V motor, requires about ten times this. Connecting such loads straight to the GPIO pins can result in a dead Pi.

Part 2 of this series looks at loads larger than an LED.

I am an inventor, engineer, writer and presenter. Other stuff: Royal Academy of Engineering Visiting Professor of Engineering: Creativity and Communication at Brunel University London; Fellow of the Institution of Mechanical Engineers and have a PhD in bubbles; Judge on BBC Robot Wars.

Recommended Articles

Comments

March 1, 2021 13:42

Hello. For my part when I tinker with Arduino, I use LEDs with integrated resistor like 860-9712 - yellow - which allows for testing without having to wire an additional resistor (false contacts, increased risk of short circuit ...). To be checked if it lights sufficiently under 3.3V. Bernarduino

0 Votes
DesignSpark Electrical Logolinkedin