In the previous tutorial, we discussed the basics about Arduino sketches, with a a quick Arduino language reference. Now, it’s time to get our hands dirty. Interfacing LED and driving digital output from a source is the “Hello World” of embedded systems.
As discussed in the previous tutorial, a microcontroller interfaces and interacts with other electronic devices in five ways:
1. Digital Output
2. Digital Input
3. Analog Output
4. Analog Input
5. Serial Communication.
In this tutorial, we’ll go into more detail about the first one of these methods: digital output. By driving digital output from Arduino UNO’s GPIO, we will build an LED driver.
Light-emitting diodes
Light-emitting diodes, or LEDs, are semiconductor diodes that emit a narrow bandwidth of electromagnetic waves (either visible light, infrared light, or laser light) in forward bias condition. LEDs are similar to PN junction diodes. For example, when forward voltage is applied, a current will pass through LEDs. And, when reverse voltage is applied, the current will be blocked.
These diodes are made up of a very thin layer of heavily doped semiconductor material. Much like PN junction diodes, these are two-terminal devices. The P-type semiconductor material forms the anode and the N-type semiconductor material forms the cathode.
When a forward bias voltage is applied (meaning, a positive potential at the anode and a negative potential at the cathode of the diode), electrons from the N-type semiconductor are exited and combine with holes from the P-type semiconductor — releasing energy in the form of monochromatic light.
As the layer of semiconductor material in LEDs is extremely thin, the photons can escape through the junction and produce visible or infrared light. Therefore, in the forward bias condition, the LEDs radiate electrical energy in the form of light energy.
In the reverse bias condition (when there’s a negative potential at the anode and a positive potential at the cathode of the diode), the holes in the P-type semiconductor are attracted to the negative potential. Also, the electrons in the N-type semiconductor are attracted to a positive potential widening or the depletion zone between the P-type and N-type semiconductors. As a result, no current flows through the diode and there’s zero light radiated from it.
LEDs are constructed differently than typical signal diodes. The PN junction of an LED is enclosed in a transparent epoxy-resin hard-plastic shell, which can be hemispherical, cylindrical, or rectangular. However, the junction does not produce much light. Rather, the emitted light is reflected from the surrounding substrate base, such that when it leaves the diode it appears as a focused beam of monochromatic light. It’s the reason that light emitted from LED is brightest at the top of it.
As LEDs resemble a monochromatic type of light, they have an extremely low heat emission that manifolds their efficiency in comparison to non-semiconductor light sources. Typically, the cathode of LED is indicated by a notch in the shell and/or by shorter lead at the cathode terminal.
Types of LEDs
LEDs can be distinguished by the color they emit. Unlike normal signal diodes that are made by doping Germanium or Silicon semiconductors, LEDs are made by doping semiconductor compounds — such as Gallium Arsenide (GaAs), Silicon Carbide (SiC), Gallium Phosphide (GaP), Gallium Indium Nitride (GaInN), Gallium Arsenide Phosphide (GaAsP), and Aluminium Gallium Phosphide (AlGaP). Different ratios of these semiconductor compounds can produce different bandwidths of light.
The main P-type dopant used in LEDs is Gallium (Ga) and the main N-type dopant used in LEDs is Arsenic (As).
Gallium Arsenide is the most common semiconductor material used for manufacturing LEDs. Gallium Arsenide produces infrared light, which has a wavelength of between 850 and 940 nm. It is compounded with other elements, such as Phosphorous, to reduce the resultant wavelength.
The actual monochromatic light produced from an LED depends on the choice and ratio of semiconductor compounds, and the level of their doping.
Here are some of the more commonly used semiconductor compounds in making LEDs.
The forward voltage required to light up the LED depends on its color and wavelength. The forward operating voltage typically ranges from 1.2 to 3.6 V, with a forward current from 12 to 30 mA.
LED characteristics
Similar to signal diodes, LEDs are also current-controlled devices. This means their characteristics depend on the input current. “Higher” is the current flowing through it, and “Greater” is the intensity of output light. However, this does not mean that the maximum current must be passed through the LED.
LEDs have an extremely thin layer of semiconductor material and an excessive current can easily damage or destroy it. So, LEDs typically need to be current-limited, which can be done by connecting a series resistor. However, an LED should never be connected directly across a battery or power source. They should also be current-limited by 5mA for normal brightness to 30mA for higher brightness.
As the required forward voltage depends on the color and wavelength of the LED light, the current rating can be obtained by investigating the voltage-current characteristics.
Here’s a comparison of the VI characteristics for different types of LEDs.
Current limiting resistor
Generally, LEDs are powered through a low-voltage DC supply. A series resistor is connected with LED to limit the current through it. The value of this resistor can be calculated by using Ohms Law.
If an LED with a forward voltage VF is supplied a DC voltage VS, the current through it can be limited to IF by connecting a series resistor with a value derived by this equation:
RS = (VS – VF)/IF
LEDs in series
It’s possible to drive multiple LEDs together from the same voltage source by connecting them in a series. When series-connected, the overall voltage drop is the sum of the forward voltage of all of the LEDs. Also, the current through all the LEDs will remain the same.
As the voltage drops across an LED series increases, the value of the current-limiting resistor will need to be reconsidered. If three LEDs are connected in a series with forward voltages VF1, VF2 and VF3, the value of the current limiting resistor can be derived by this equation:
RS = (VS – (VF1 + VF2 + VF3)/ IF
LED driver circuit
LEDs are used for decorative purpose or as indicators in electronic devices. LEDs can be used to indicate mutually exclusive conditions, such as if:
- A device is switched on or off
- A feature or option is selected or not selected
- A device is working or not working
In some situations, progress of an action is indicated by blinking LEDs.
A circuit that switches the LED ON and OFF is called the LED driver. For driving an LED, the circuit must control the flow of current through it.
Generally, an LED is hard-wired in the circuit so that a current can be sinked or sourced through it. For switching ON the LED, its anode must have a positive potential in comparison to its cathode. If the LED has to be switched ON by sourcing its current, this means the cathode is connected to the ground and the anode is connected to the current source. When the source provides a positive potential that’s greater than its forward voltage, current will flow through the LED so it starts glowing. In the absence of a positive voltage that’s greater than the forward voltage, the LED will stay switched OFF.
If the LED has to be switched ON by sinking a current through it, this means its anode is connected to a positive DC voltage and the cathode is connected to the sink. When the voltage at the sink drops, the current will flow through the LED so that it starts glowing. When the sink maintains an equal or higher voltage, the LED remains switched OFF.
This image shows the two methods to drive an LED.
Arduino GPIO & Digital Input/Output
Most of the Arduino boards have low-power CMOS 8-bit microcontrollers based on AVR enhanced RISC architecture. The GPIO pins of these controllers operate at CMOS logic levels. A CMOS gate operating at 5V accepts voltage ranging from 0 to 1.5V as logical LOW and 3.5 to 5V as logical HIGH.
The acceptable output signal voltage from a CMOS gate ranges from 0 to 0.05V for logical LOW and 4.95 to 5V for logical HIGH.
The Arduino pins can be configured as INPUT, OUTPUT, or INPUT_PULLUP. When a pin is configured as INPUT, a voltage greater than 3V and a voltage greater than 2V is read as logical HIGH in 5 and 3.3V boards, respectively. A voltage less than 1.5V and a voltage less than 1V is read as logical LOW in 5 and 3.3V boards, respectively.
A pin configured as INPUT is in a high-impedance state (for example, such that it’s connected in a series to a 100-Mega Ohms resistor). In such a state, the pin would have an extremely low demand from any external circuits. This means that if it’s unconnected, it would go into an unpredictable floating state.
Therefore, a pin configured as INPUT must be connected to a pull-up or pull-down resistor. A pull-up resistor hardwires the pin to a DC supply voltage and a pull-down resistor hardwires the pin to the ground.
When a pin is configured as INPUT_PULLUP, it accesses logical levels through the internal pull-up resistors. This means there’s no reason to connect external pull-up resistors to avoid a floating state. When a pin is configured as INPUT or INPUT_PULLUP — if the applied voltage is negative or greater than supply rail voltage (5 or 3V) — the pin can get damaged or destroyed.
When a pin is configured as OUTPUT, a voltage of 5V and a voltage of 3.3V is sourced by the pin for logical HIGH in 5 and 3.3V boards, respectively. A voltage of 0V is sourced as a logical LOW in 5 and 3.3V boards.
The Arduino pins can source or sink up to 40 mA of current to external circuits. If any load requires more than 40 mA of current, it must be interfaced via a transistor or other similar circuitry. As LEDs typically require a current ranging from 12 to 20 mA, they can be directly interfaced and driven by the Arduino pins.
Arduino as LED driver
LEDs can be directly interfaced with the Arduino pins. Arduino GPIO can drive an LED by either sourcing or sinking current through it. However, as the LED must be controlled by the Arduino pin, the pin must be configured as a digital output.
If the anode of the LED is connected to the Arduino pin, its cathode must be grounded so that the pin can serve as the current source. In such configuration, when the pin outputs a logical HIGH, the LED glows. When the pin outputs a logical LOW, the LED is switched OFF.
If the cathode of the LED is connected to the Arduino pin, its anode must be connected to a DC supply voltage. The supply voltage must be less than the positive power rail of Arduino. In such configuration, the pin serves as the current sink. When the pin outputs a logical HIGH, the LED remains switched OFF because of a lack of the required forward voltage (the potential difference across two terminals of the LED). When the pin outputs a logical LOW, the LED glows as the required potential difference (the forward voltage) across its terminals.
So, LEDs can be hard-wired in these two ways, and they can be switched ON and OFF by simply toggling the digital output. More than one LED can be driven by the same pin by connecting them in series, provided their forward voltage is low. As the forward voltage is dependent on the color of the LED, it may be possible to drive two red, green, yellow. or orange LEDs from the same pin. But, two white LEDs may not drive from the same pin as the sum of their forward voltage will exceed even the power rail voltage of Arduino.
Generally, a single LED is driven from one Arduino pin. The LED must remain connected to the Arduino pin via the current-limiting resistor as the output current can shoot up to 40 mA, which may damage the LED.
Arduino LED driver recipe
In this recipe, we will build an LED driver using Arduino UNO. We will switch an LED ON and OFF regularly, blinking the LED from one of the digital input/output pins on the Arduino board.
The required components…
1. Arduino UNO x1
2. LED x1
3. 330 Ohms Resistor x1
4. Breadboard x1
5. Male-to-Male Jumper Wires
The circuit connections…
- Connect the digital I/O pin 2 from Arduino UNO with the anode of the LED.
- Connect the cathode of the LED with a series resistor of 330 Ohms and ground the other terminal of the resistor.
- The DC supply voltage and ground can be given to the circuit from the 5V power pin and one of the ground pins of the Arduino UNO, respectively.
Circuit drawing
Programming guide
For a blinking LED, the pin 2 where the LED is connected must be configured to a digital output. This is done in the setup() function by calling pinMode() function as:
void setup() {
pinMode(2, OUTPUT);
}
The LED must be blinked infinitely at an interval of one second. The code to blink LED is written in loop() function and it keeps iterating infinitely.
The pin is set to output a logical HIGH using digitalWrite() function and delay() function, which is called passing 1000 milliseconds as an argument to provide a delay of one second. As per the circuit connections, the LED is switched ON when a logical HIGH is an output from the pin.
After the one-second delay, the pin is set to output a logical LOW using digitalWrite() function. Again a delay of one second is passed using the delay() function. As the code in loop() function iterates infinitely, the LED is switched ON and OFF at a regular interval of one second.
void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
The LED is connected with the Arduino pin such that the pin acts as a current source for the LED. When the pin is set to logical HIGH, the LED is “forward biased” and it starts glowing. When the pin is set to logical LOW, the LED is deprived of the forward voltage and it will stop glowing.
An infinite sequence of logical HIGH and LOW is the output from the pin at an interval of one second so as to blink the LED. As the microcontroller code keeps iterating infinitely until the Arduino is shut down, the LED also will keep blinking.
Do-it-yourself
Try driving a pair of series-connected LEDs from the Arduino pin. Will it impact the brightness of LEDs? Will you need to change the current-limiting resistor? What if two LEDs are connected in parallel to the Arduino pin. What if two LEDs are connected in parallel to the Arduino pin such that one LED requires pin as current source to switch ON and the other requires pin as current sink to switch ON?
In the next tutorial, we will discuss reading digital input on Arduino and will use it to detect the status of a switch.
Demonstration video:
You may also like:
Filed Under: Arduino Projects, Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.