Language: EN


Turn on an LED with Arduino

In this post we will see how to turn on an LED using Arduino outputs. To do this, we will see the principle of operation and the necessary electrical schematic.

Of course, we can also use the content of this post to turn on an LED with any other automaton, or by directly connecting it to a power source or battery. The electrical schematic is the same, just replace the Arduino output with your voltage source.

In some books or web pages, you will see that sometimes they directly connect the LED to a digital or analog output of Arduino. Although this works (we will see why later) it is a bad practice. In general, LEDs should always be connected through a resistor.

To understand the importance and role of this resistance, and to be able to calculate its value, it is necessary to understand how an LED works.

What is an LED?

An LED is a light-emitting diode, that is, a particular type of diode that emits light when crossed by an electric current. Diodes (light-emitting or not) are some of the fundamental electronic devices.

Remember that we differentiate between electrical and electronic devices. Electrical devices include resistors, capacitors, and coils, and are part of the field of electricity. Electronic devices arise from the use of semiconductor materials and give rise to the field of electronics.

A diode is a junction of two semiconductor materials with different dopings. Without going into details, this difference in doping causes it to generate a potential barrier, which as a first consequence makes the passage of current in one direction not possible.

Here we have the first characteristic of diodes, they have polarity, that is, they only allow current to pass in one direction. Therefore, we have to connect the voltage to the device correctly.

The long pin must be connected to the positive voltage (anode), and the short one to the negative voltage (cathode).


Mnemonic rule: - The “longer” pin is the positive one. - The “shorter” pin is the negative one.

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 reaching a certain value of voltage that we call the forward voltage (Vd), which depends on the type of diode.

From this voltage, we say that the diode is polarized and the current can pass through it freely with an almost zero resistance. The voltage that is really feeding the diode is the difference between the applied voltage and the forward voltage of the diode.

As we can see, the moment we exceed the forward 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 resistance that limits the amount of current that flows through the diode.

In summary, if we do not put a resistance, the system only has two states.

  • If we feed at a voltage lower than Vd, the LED does not light up.
  • If we feed at a voltage higher than Vd, the LED breaks.

In any case, we will not be able to make the LED light up without using an appropriate resistance.

Why does it work by connecting directly to an Arduino output?

As we have mentioned before, sometimes you will see in tutorials on the Internet 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 in its outputs. This limitation means that the LED does not burn out, although it is actually behaving as if it were a short circuit. Simply put, Arduino cannot provide more current.

However, this is a practice that is totally discouraged for several reasons. First, because it unnecessarily forces the Arduino output, which can shorten its life in the long run. On the other hand, because 20mA is, in general, a current that is too high for an LED. But above all, because it is a makeshift 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 high-power LEDs used in lighting. The latter require additional power stages (drivers) to be able to turn them on from an automaton.

Among low-power LEDs, which are the ones we will use most frequently, the most common are the traditional 3mm or 5mm LED packages.


We can also find opaque LEDs (diffused) or transparent LEDs (clear). Opaque LEDs are designed to “light up” themselves (for example, to make a control panel). On the other hand, transparent 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 narrower beam, so they concentrate the light in a narrow area. On the other hand, LEDs with wider angles concentrate a lower amount of light forward, and in return, illuminate a larger area.

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 depends only on the internal construction of the LED (personally, I prefer them with the uncolored encapsulation).


How much does an LED cost?

The cost of LEDs is very similar regardless of their type and size (except, logically, high-power LEDs), although some colors can be a little more expensive.

In general, they are really cheap devices. We can find LEDs for 1 euro cent, in international sellers on eBay or AliExpress.


Finally, we also have a large number of accessories available for LEDs, such as supports, threaded supports, decorative plastic caps, etc. ..and they are also very cheap, around one euro cent each.


Calculate the value of the resistance

We have said that the main thing to make an LED work is to calculate the value of the necessary resistance. To calculate the value of the voltage necessary to power an LED, we need to connect 3 parameters.

  • The supply voltage (Vcc)
  • The forward voltage of the LED (Vd)
  • The nominal current of the LED (In)

Calculating the value of the resistance is simple. As we said, the voltage that the LED supports is the difference between the applied voltage and the forward voltage of the LED.

Applying Ohm’s law, with the value of the nominal intensity of the LED

So the value of the resistance is

Since commercial resistors have standardized values, you will not find a resistor with the exact value you have calculated. In this case, we will choose the immediately higher standardized resistance value to guarantee that the current is lower than the nominal.

The supply voltage Vcc is known to us. If applying a power source or a battery, Vcc is the nominal voltage of the same. In the case of a digital or analog output of Arduino, 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. Consult the entry PWM analog outputs in Arduino if you have doubts about this.

Regarding the forward voltage and the nominal current 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 advertisement. In case of doubt, we should consult the LED Datasheet to check the nominal values.

In the following table, we attach some general values of the typical forward voltage Vd for each color. It also shows the value of the necessary resistance, in ohms, for different values of supply voltage Vcc.

ColorVddResistance (Ohms)

Electrical Connection

The electrical connection is really simple. We simply place the previously calculated resistance in series with the LED.


The assembly on a breadboard would look like this.


Code examples

Here are some of the codes to test turning on LEDs with our Arduinos, and the indicated assembly. The codes are similar to those we saw previously in the various blog entries, but using an external LED instead of the integrated LED on the board. To do this, we just have to replace the PIN number 13, corresponding to the integrated LED, with the PIN of the output we are going to use.

So, the first code is used to turn on and off an LED, 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 for a second
  digitalWrite(ledPIN , LOW);    // set the Pin to LOW
  delay(1000);                   // wait for a second

Try it online

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 port communication in Arduino.

const int ledPIN = 9;

int option;

void setup(){
  pinMode(ledPIN , OUTPUT); 

void loop(){
  //if there is pending information
  if (Serial.available()>0){
    //read the option
    char option =;
    //if the option is between '1' and '9'
    if (option >= '1' && option <= '9')
      //subtract the value '0' to obtain the number sent
      option -= '0';
      for(int i=0;i<option;i++){
         digitalWrite(ledPIN , HIGH);
         digitalWrite(ledPIN , LOW);

Try it online

Finally, the following 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 is data available
      outputValue =;  // Read the option
      if(outputValue >= '0' && outputValue <= '9')
         outputValue -= '0';    // Subtract '0' to convert to a number
         outputValue *= 25;    // Multiply by 25 to go to a scale 0 to 250
         analogWrite(ledPIN , outputValue);

Try it online

That’s all for the LED usage tutorial. It’s amazing how much has been written for such a small device! We hope that it is clear how to use LEDs with or without Arduino, and we encourage you to use them and try them out in your experiments.

Download the code

All the code from this post is available for download on Github. github-full