In this entry, we will see how to turn on an LED using Arduino outputs. For this, we will look at the operating principle and the necessary electrical diagram.
Of course, we can also use the content of this entry to turn on an LED with any other automaton, or by directly connecting it to voltage with a power supply or battery. (The electrical diagram is the same, just replace the Arduino output with your power supply)
In general, LEDs should always be connected through a resistor. In fact, almost every electronic element you connect should go through a resistor.
To understand the importance and role of this resistor, and to calculate its value, it is necessary to understand how an LED works.
In some books or websites, you will see that they sometimes connect the LED directly to a digital or analog output of Arduino. Although this works (we will see why later) it is a bad practice.
What is an LED?
An LED is a light-emitting diode. That is, a particular type of diode that emits light when an electric current passes through it. Diodes (light-emitting or not) are one of the fundamental electronic devices.
Remember that we differentiate between electrical and electronic devices.
- Electrical devices include resistors, capacitors, and coils, and integrate the field of electricity.
- Electronic devices arise from the use of semiconductor materials, leading to the field of electronics.
A diode is a junction of two semiconductor materials with different doping. This difference in doping (without going into details) creates a potential barrier. This prevents current from passing in one direction.
Here we have the first characteristic of diodes; they have polarity, meaning they only allow current to pass in one direction. Therefore, we must connect the voltage correctly to the device.
- The longer leg must be connected to the positive voltage (anode)
- The shorter leg to the negative voltage (cathode)
Mnemonic rule:
- The “longer” leg is positive
- The “shorter” leg is negative
The other consequence of the potential barrier is that, even when connecting the device with the correct polarity, at low voltage the electrons still cannot pass through the device.
This happens until a certain voltage value is reached, which we call forward bias voltage (Vd), depending on the type of diode.
From this voltage onward, we say that the diode is forward-biased, and current can pass through it freely with almost zero resistance.
The voltage that is actually powering the diode is the difference between the applied voltage and the forward bias voltage of the diode.
Why do we need the resistor?
As we see, the moment we exceed the forward bias voltage, and given that the resistance of the diode is very small, a large current is generated that will destroy the diode.
For that reason, we need a resistor to limit the amount of current flowing through the diode.
In other words, if we do not put a resistor, the system only has two states.
- If we power it with a voltage lower than Vd, the LED does not light up ❌
- If we power it with a voltage higher than Vd, the LED breaks 💥
Neither of the options is very good 😅. In any case, we will not be able to make the LED light without using a suitably valued resistor.
Why does it work when connecting to an Arduino output?
As we mentioned earlier, sometimes you will see in tutorials online that some connect an LED directly to an Arduino output, without using a resistor. Indeed, this works, and the LED lights up without breaking. How can this be possible?
This works because Arduino has a limitation of 20mA on its outputs. This limitation prevents the LED from burning out (although it is actually behaving as if it were a short circuit). Arduino simply cannot provide more current, more “juice”.
However, this is a practice that is highly discouraged for several reasons:
- Firstly, because it unnecessarily forces the Arduino output, which can shorten its lifespan in the long term.
- Secondly, because 20mA is generally too high of a current for an LED.
- But above all, because it is a hack and a total lack of electronic hygiene 😆.
What types of LEDs will we use in electronics?
There is a wide range of LEDs available, from the usual low-power LEDs to the high-power LEDs used in lighting. The latter require additional power stages (drivers) to be turned on from an automaton.
Among the low-power LEDs, which are the ones we will use most frequently, the most common are traditional 3mm or 5mm encapsulated LEDs.
You can also find opaque (diffused) LEDs or clear LEDs. Opaque LEDs are designed to “light up” themselves (for example, to create a control panel). On the contrary, clear LEDs are designed to illuminate an area, but not the LED itself.
Additionally, we will find LEDs with different angles. LEDs with a smaller illumination angle have a tighter beam, thereby concentrating the light in a narrow area. Conversely, LEDs with wider angles concentrate less light forward and illuminate a larger area in return.
Finally, you will see that some LEDs have a colored encapsulation. This color is simply to identify the color of the light emitted by the LED without having to turn it on, but it has no influence on the color of the emitted light, which only depends on the internal construction of the LED (personally, I prefer them with uncolored encapsulation).
How much does an LED cost?
The cost of LEDs is very similar regardless of their type and size (except, of course, for high-power LEDs), although some colors may be slightly more expensive.
In general, they are really cheap devices. We can find LEDs for 1 cent euro, from international sellers on eBay or AliExpress.
Finally, note that we also have a wide variety of accessories available for LEDs, such as supports, threaded bases, decorative plastic caps, etc., and they are also very cheap, around one cent euro each.
Calculate the value of the resistor
We have said that the main thing to make an LED work is to calculate the value of the necessary resistor. To calculate the required voltage value to power an LED, we need to connect 3 parameters:
- The supply voltage (Vcc)
- The forward bias voltage of the LED (Vd)
- The nominal current of the LED (In)
Calculating the value of the resistor is straightforward. As we said, the voltage that the LED withstands is the difference between the applied voltage and the forward bias voltage of the LED.
Applying Ohm’s law, with the nominal current value of the LED:
So the value of the resistor results in:
Since commercial resistors have standardized values, you will not find a resistor with the exact value you calculated. In this case, we will choose the immediately higher standardized resistor value than the calculated value to ensure that the current is below the nominal.
The supply voltage Vcc is known to us. In the case of applying a power supply or a battery, Vcc is its nominal voltage. In the case of a digital or analog Arduino output, Vcc will depend on the model we are using (5V or 3.3V) but is also known.
Remember that even if you use a PWM analog output, the voltage delivered to the load is always Vcc. Check the entry PWM Analog Outputs in Arduino if you have any doubts about this.
As for the forward bias voltage and nominal current, they depend on the materials and internal constitution of the diode. In the case of conventional 3mm and 5mm LED diodes, they mainly depend on color and brightness.
However, in most cases, the seller provides these values in the listing. In case of doubt, we should refer to the LED’s Datasheet to check the nominal values.
In the following table, we provide general values of the typical forward bias voltage Vd for each color. I will also provide the necessary resistor value in Ohms for different supply voltage values Vcc.
Color | Vdd | Ω (3.3V) | Ω (5V) | Ω (9V) | Ω (12V) |
---|---|---|---|---|---|
Infrared | 1.4V | 150 | 270 | 510 | 680 |
Red | 1.8V | 100 | 220 | 470 | 680 |
Orange | 2.1V | 100 | 200 | 470 | 680 |
Yellow | 2.2V | 100 | 200 | 470 | 680 |
Green | 3.2V | 10 | 150 | 330 | 560 |
Blue | 3.5V | - | 100 | 330 | 560 |
Violet | 3.6V | - | 100 | 330 | 560 |
White | 3.8V | - | 100 | 330 | 560 |
Electrical connection
The electrical connection is really simple. We simply place the previously calculated resistor in series with the LED.
The assembly on a breadboard would look like this.
Code examples
Next are some codes to try to turn on LEDs with our Arduinos, and the indicated assembly.
The codes are similar to those we have seen previously in the various blog entries, but using an external LED instead of the integrated LED on the board.
For this, we just need to replace the PIN number 13, corresponding to the integrated LED, with the output PIN we are going to use.
Blink an LED
Thus, the first code is used to turn an LED on and off, as we saw in the entry digital outputs in Arduino.
const int ledPIN = 9;
void setup() {
Serial.begin(9600); // start serial port
pinMode(ledPIN , OUTPUT); // define pin as output
}
void loop(){
digitalWrite(ledPIN , HIGH); // set the Pin to HIGH
delay(1000); // wait one second
digitalWrite(ledPIN , LOW); // set the Pin to LOW
delay(1000); // wait one second
}
Turn on N times via Serial
The next code uses digital outputs and serial port communication to make the LED blink the number of times we send through the serial port, as we saw in the entry serial communication in Arduino.
const int ledPIN = 9;
int option;
void setup(){
Serial.begin(9600);
pinMode(ledPIN , OUTPUT);
}
void loop(){
//if there is pending information
if (Serial.available()>0){
//read the option
char option = Serial.read();
//if the option is between '1' and '9'
if (option >= '1' && option <= '9')
{
//subtract the value '0' to get the sent number
option -= '0';
for(int i=0;i<option;i++){
digitalWrite(ledPIN , HIGH);
delay(100);
digitalWrite(ledPIN , LOW);
delay(200);
}
}
}
}
Adjustable via PWM
Finally, the next code uses a PWM output to vary the intensity of the LED, as we saw in the entry analog outputs in Arduino.
const int ledPIN = 5;
byte outputValue = 0;
void setup()
{
Serial.begin(9600); // Start serial port
pinMode(ledPIN , OUTPUT);
}
void loop()
{
if (Serial.available()>0) // If there are available data
{
outputValue = Serial.read(); // Read the option
if(outputValue >= '0' && outputValue <= '9')
{
outputValue -= '0'; // Subtract '0' to convert to a number
outputValue *= 25; // Multiply by 25 to scale from 0 to 250
analogWrite(ledPIN , outputValue);
}
}
}
This concludes the tutorial on using LEDs. It seems unbelievable how much has been written about such a small device! We hope it is clear how to use LEDs with or without Arduino, and we encourage you to use and test them in your experiments.
Download the code
All the code from this entry is available for download on Github.