Saturday, July 22, 2017

Interfacing LEDs with Microcontroller / Arduino

Introduction

This documents describes how to drive an LED or other simple, low current loads with a microcontroller output pin.

LED characteristics

LEDs are Light Emitting Diodes, so they conduct current when they are forward biased, and emit light when current flows through them. The amount of light given off, or its brightness, will be proportional to the current. The more current that is allowed to flow, the brighter it will get.
The data sheet for the LED will generally give typical operating current and maximum ratings for the LED. You can operate the LED at a lower current and get less light or at a higher current and get more light as long as you don't exceed the maximum ratings of the LED. Small LEDs typically run at 20mA.
Another important characteristic of an LED is the Vf, or forward drop voltage, above which diode conducts current and below which conduction stops. As the current through the LED increases, the voltage drop across it will increase only slightly. Vf will be listed at a specific current, usually the same one used for rating the light output.
Forward voltage will usually be around 2 volts for red LEDs, and increase with different colors. Blue or white LEDs are often a bit over 3 volts. Forward voltage is important for setting the proper current level for the LED.
Table below shows typical wavelength and voltage drop for different color LEDs.
ColorWavelength [nm]Voltage drop [ΔV]
Infraredλ > 760ΔV < 1.63
Red610 < λ < 7601.63 < ΔV < 2.03
Orange590 < λ < 6102.03 < ΔV < 2.10
Yellow570 < λ < 5902.10 < ΔV < 2.18
Green500 < λ < 5701.9 < ΔV < 4.0
Blue450 < λ < 5002.48 < ΔV < 3.7
Violet400 < λ < 4502.76 < ΔV < 4.0
PurpleMultiple types2.48 < ΔV < 3.7
Ultravioletλ < 4003 < ΔV < 4.1
PinkMultiple typesΔV ≈3.3
WhiteBroad spectrum2.8 < ΔV < 4.2

LED Design Example

In the example design we select red LED and look up the specs in the data sheet. The recommended current for this type of LED is 16-18mA, and forward voltage is 1.8-2.0V.

Schematics shows the two LEDs being driven from Arduino UNO in different ways. LED1 uses the microcontroller output pin D10 as a sink, and LED2 uses the pin D11 as a source. There is also a resistor in series with the LEDs. If the resistor is omitted and the LED is driven directly with the microcontroller pin, excessive current would flow when the LED is turned on, probably burning out that pin on the microcontroller or maybe the LED.
The question is what value should we use for the resistor?
We will use the LED1-R1 combination to do the calculation. The source voltage is 5V from our arduino board. The voltage dropped across the LED is 1.9V, which we found in the data sheet. Since the voltage around the circuit loop must be zero volts,
Vcc = Vr + Vf
Rearranging the terms gives us
Vr = Vcc - Vf = 5 - 1.9 = 3.1 V
We now know the voltage across the resistor is 3.1V, and we decide that we want to drive the LED at 10mA, so a simple application of Ohm’s Law will tell us what value resistor is needed.
R = V/I = 3.1/0.01 = 310 ohms.
Resistors come in standard values. The resistor values are 300 ohm and 330 ohm. Either would be OK. The 300 ohm would result in a little more than 10mA of current, and the 330 ohm resistor would be less than 10mA. The value for R2 is the same as R1. The only real difference is the microcontroller is supplying 5 volts instead of directly getting it from the power supply.

Sink or source

As we have seen, pins can sink or source current, so which one to use? In most cases it is a matter of personal preference. To turn on LED1 you need to set the pin to zero or LOW. To turn on LED2, you set pin to a logical 1 or HIGH. To many developers, setting a pin to high seems more natural for turning something on, so you might want to use the microcontroller pin as a source.
One case you might need to use one interfacing method over another is when the maximum source and sink currents for the pin are not the same, and only one method will be able to handle the current needed.

Arduino Example

In the following circuit we have two red LEDs connected to Arduino's digital output pins. First LED is connected to digital pin 11 through current limiting resistor to ground. Second LED is connected to 5V through current limiting resistor to digital pin 10. Note that LED anode is longer leg of the LED and needs to be connected to positive side, and LED cathode is shorter leg and needs to be connected to negative side or ground.
In the simple Arduino sketch setup part we are setting both 10 and 11 pins as output pins. In a loop we set both pins HIGH, delay for one seconds, and then set both pins LOW. This effectively makes LEDs on for one second, and then off for one second, but when the first LED is on, the second one is off, and vice versa.
When the digital pin 11 is HIGH, the current flows from the pin, which is sourcing current, through the current limiting resistor and LED to the ground, turning the LED on. When the pin 11 is LOW, no current flows through and the LED is off.
When the digital pin 10 is LOW, the current flows from the 5V supply through the current limiting resistor and LED to the pin 10, which is sinking current, turning the LED on. When the pin 10 is HIGH, no current flows through and the LED is off.
#define LED_SINK_PIN   10 // Use digital pin 10 to drive LED as current sink pin
#define LED_SOURCE_PIN 11 // Use digital pin 11 to drive LED as current source pin

void setup() {
  pinMode(LED_SINK_PIN,   OUTPUT);     // Set LED sink pin as output pin
  pinMode(LED_SOURCE_PIN, OUTPUT);     // Set LED source pin as output pin
}

void loop() {
  digitalWrite(LED_SINK_PIN,   HIGH);  // Set LED sink pin high
  digitalWrite(LED_SOURCE_PIN, HIGH);  // Set LED source pin high
  delay(1000);                         // Delay for 1 second
  digitalWrite(LED_SINK_PIN,   LOW);   // Set LED sink pin low
  digitalWrite(LED_SOURCE_PIN, LOW);   // Set LED source pin low
  delay(1000);                         // Delay for 1 second
}

Summary

Controlling an LED with a microcontroller is a common application and is easy to implement. Make sure that current limits of the microcontroller and LED are not exceeded.

No comments:

Post a Comment