NAKED LED
Context
● You are developing an embedded application using one or more members of the 8051 family of microcontrollers.
● You are designing an appropriate hardware foundation for your application.
Problem
What is the cheapest way of driving a small number of LEDs with a microcontroller?
Background
Even in the most basic application, the presence of a constant red or green output from a light-emitting diode (LED) is reassuring, implying that the application is at least powered. In other applications requiring numerical outputs, LEDs are grouped into seven- or eight-segment combinations12 to display, for example, the current time, the voltage or the number of calls received by a telephone answering machine (‘voice mail’) system. Overall, LEDs are the most widely used components in user interfaces for embedded systems.
Given the name it is hardly surprising that, electrically, an individual LED operates as a diode. LEDs have a forward voltage drop of about 2V, and typically require a cur- rent of around 5–15 mA for a bright display (Figure 7.1). Note that the forward voltage required to ‘switch on’ a conventional (silicon) diode is around 0.7 V: the dif- ference arises because LEDs are generally manufactured from gallium arsenide phosphide (see Horowitz and Hill, 1989, for further details).
Solution
The focus in this pattern is on low-cost techniques for driving LEDs.
12. Multi-segment LEDs are considered in MX LED DISPLAY [page 450].
Driving single LEDs
It is possible to drive small single LEDs directly from the port pins, as illustrated in Figure 7.2. Note that we usually need a resistor in series with the LED, between the supply and the port pin. This resistor is required to limit the current flow to the port when the LED is ‘switched on’.
To understand why this resistor is necessary, you need to remember that – as we discussed in ‘Background’ – the voltage drop across the LED will be around 2V. Assuming Vcc is 5V, then the voltage drop across the resistor and diode, when the port pin is at 0V, will be around 5V. Thus, the voltage drop across the resistor needs to be 3V. If there is no resistor, then we need to drop 3V across a stretch of wire: this can cause a very strong current to flow (limited only by the supply capacity), which will almost instantly destroy the port, the LED and, possibly, the whole microcontroller.
The equation in Figure 7.2 (essentially Ohm’s law) shows how to calculate the required resistor (R) value. Typical values of the required parameters are as follows:
● Supply voltage, Vcc = 5V
● LED forward voltage, Vdiode = 2V
● Required diode current, Idiode = 10 mA (note that the data sheet for your chosen LED will provide this information) This gives a required R value of 300Ω.
Use of pull-up resistors
Throughout this chapter, we will present examples of driver circuits like that shown in Figure 7.2.
This circuit will only work on port pins where the microcontroller has an internal pull-up resistor: this applies to most ports on the 8051 family, with the exception of Port 0. In addition, some other members of the 8051 family – notably the Atmel 89Cx051 devices – have small numbers of pins without internal pull-ups.
[Note: When calculating the required value of Rled, the resistance values are in ohms, voltages in volts and current in amps.]
To adapt circuits such as that shown in Figure 7.2 for use on pins without internal pull-up resistors is straightforward: you simply need to add an external pull-up resis- tor, as illustrated in Figure 7.3. The value of the pull-up resistor should be between 1K and 10K. This requirement applies to all of the examples in this book.
In the unlikely event that you do not know whether a drive circuit will be used on a port with pull-up resistors, the best solution is to include 10K resistors in your cir- cuit. If the port pin used already contains pull-ups, the extra resistor will have no discernible impact on the operation of the circuit.
Hardware resource implications
Every implementation of this pattern uses at least one port pin.
Reliability and safety implications
There are several reliability implications involved in the use of this pattern.
Connecting up LEDs
If you connect ordinary LEDs to a port, do not omit the resistor! The resulting high current flows may not cause the system to fail immediately, but will greatly reduce the life of the port pin and, potentially, the whole processor.
Note: There are ‘5V’ LEDs available, at higher cost: these include series resistors. Provided they have suitable current requirements, they may be safely connected directly to the port pins.
Should you use a buffer?
Where more than two LEDs are connected to a single port, buffering is almost always essential, because – while the limit for an individual port pin may be (say) 10 mA – the port as a whole may have a limit of 20 mA or less. This issue is discussed in IC BUFFER [page 118].
Use of LEDs as warning devices
LEDs (particularly flashing LEDs) are frequently used as warning devices. Bear in mind that in bright sunlight, such warnings will be barely visible and that blind or partially sighted people will never be able to see them. Adding an additional or alternative audible output may be appropriate in some systems.
General use of LEDs
LEDs consume large amounts of power (compared, for example, to liquid crystal displays) and need to be used with care in many battery-powered designs.
Portability
The circuits presented here can be used with almost all microcontrollers and micro- processors. Please note, however, that most of the circuits we present for driving DC and AC loads involve some form of current ‘sink’, as in Figure 7.2. Here, the current flows ‘in to’ the processor port. This is despite the fact that most microcontroller ports, manufactured using some form of MOS technology, can also ‘source’ current, so that the load could be arranged as illustrated in Figure 7.4.
However, some of the other circuits used as buffers or drivers may use different manufacturing processes, including TTL technology. TTL devices can sink current in much the same way as MOS devices, but are poor current sources. As a result, hard- ware designs based on current sinking are generally more portable (across different logic families) than designs based on current sourcing and, for this reason, will be used throughout this pattern collection.
[Note: Here the port acts as a current source. See text for details and for a discussion of the drawbacks of this approach.]
Overall strengths and weaknesses
This pattern allows small numbers of LEDs to be driven from a microcon- troller port with a minimum of external hardware.
This is a low-cost solution.
Only applicable for small numbers of LEDs (typically two per port), otherwise a buffer will be required: see IC BUFFER [page 118].
Related patterns and alternative solutions
See PORT I / O [page 174] for software details.
See the remaining patterns in this chapter for techniques suitable for driving higher powered loads.
Example: Using low-current LEDs
To emphasize that all rules can be broken, we will consider in this example how you can connect eight ‘naked’ LEDs to the port of an 8051 microcontroller without using a buffer (see Figure 7.5).
Of course, for the reasons outlined in this pattern, it is not possible to connect up ‘normal’ (~10 mA) LEDs in this way without exceeding the current capacity of the port: however, if we use low-current (2 mA) LEDs, then – with most 8051 microcon- trollers – this is possible.
Further reading
Horowitz, P. and Hill, W. (1989) The Art of Electronics, 2nd edn, Cambridge University Press, Cambridge, UK.