5mm LED Basics and The Arduino: Tutorial

A Supercharged Blink

5mm LED‘Blink’ is likely the first Arduino sketch you uploaded and it was likely where you first got hooked.  Then you sped off to more exciting things.   As you rushed off to those other things, you likely didn’t spend a lot of time understanding what it is you will need to know later on as you run off to other things.

This tutorial helps will help you put and LED to use in a project of your own.

 

Key 5mm LED Specifications

There are a few key specifications that will be of interest to you when selecting an LED and when designing a circuit that puts it to use.

Forward Voltage – VF  This is the amount of voltage that is dropped across the LED when it’s forward biased and the Forward Current is limited to a specified value.   The remainder of the voltage will be dropped by other components in series with the LED.   In fact, these other components are what often limits the current.

Forward Current – IF – The amount of current flowing through the LED that has been forward biased.   Most 5mm LEDs perform best when their Forward Current has been limited to 20 mA.

Maximum DC  Forward Current – The absolute maximum amount of current that the LED can handle without causing appreciable damage.

 KEY POINT –  Different Color LEDs have different Forward Voltages.

 

5mm LED Pin Outs

As previously discussed, and LED is a diode.   That means it is a polarized device.  Therefore you must pay attention to the polarity of the LED when making connections.

Fortunately, 5mm LED are generally of a standardized design.  If you study the image below,  you will see that there are three ways to identify the polarity.

 

LED Pin Outs

 

Controlling Current in an LED

When using an LED, it is not enough to pay attention to the polarity.   You must also design the circuit so that amount of current flowing through the LED is controlled.   Too much current will damage the LED and cause a premature failure.  Too little current will mean little or no illumination.

The most common way to control current is to place a resistor in series with the LED.

The value of the resistor is determined by applying Ohms Law to the LED specifications.    The process for determining the value of that resistor uses the following steps.

  1.  Determine the Forward Voltage of the LED (VF) – You may want to find the LED spec sheet.   The red LED I have has forward voltage of 1.8 volts.  That’s pretty common for red 5mm LEDs.
  2. Determine the circuit Supply Voltage –  In the case of an Arduino, it will normally be 5 Vdc.
  3. Subtract Forward Voltage from Supply Voltage to retermine Resistor Voltage – Using my red LED,  I yield 3.2 Vdc
  4. Determine the Forward Current (IF) – This is also found in an LED spec sheet.   Mine is 20mA  (0.020 A) and that is very common with this type of LED.
  5. Divide the Resistor Voltage by Forward Current to get the Resistor Value –  Using my LED with an Arduino calls for a resistor of 160 Ohms.   Because I only have a 150 Ohm resistor, I will use it.   Its safe to do so because the current that will flow through the LED will be limited to a value that is below the maximum value for my LED.  ( 3.2V / 150 Ohms = 21.22 mA )

The graphic below illustrates this process.

LED Resistor Calculation

Arduino 5mm LED Tutorial

Connect your Arduino to the 5mm LED

We’ve been discussing the resistor a lot and you are going to need one.   I used a 150Ω,  but anything that get you to between 150 and 200 ohms should work.

Arduino LED TutorialCopy, Paste, Upload. and Run the 5mm Arduino Sketch

 

// Henry's Bench
// 5mm LED Tutorial

int ledPin = 10;
boolean bLedOn = true;

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

}

void loop() {
  digitalWrite(ledPin, bLedOn);
  bLedOn = !bLedOn;
  delay(1000);
}

Experiment with the Delay

When you ran the sketch for the first time, you should have seen the LED alternate between being on for a second and then off for a second.   Now try changing that delay to read delay(10);

Now it should look as though your LED is on all the time.  However it is not.  Instead, its turning on and off at a rate that is too fast for your eye to detect.   If you pay close attention,  it should appear dimmer as well.

In the next sketch, you will use analogWrite() to pulse width modulated the LED to control it’s intensity.

Load and Run the 5mm LED PWM Sketch

In the following sketch we use the analogWrite() command.  When we use that command,  we tell the Arduino to output a Pulse Width Modulated (PWM) signal to a particular pin and we tell it how long (in a 255 unit time period) to send that output high.

A value of zero, will mean we’re alway keeping the out put low ( since we need a low signal to turn on the LED, the LED is always on).

A value of 255 means that the output will always remain high.

A value of 128 means you’re low half the time and high half the time.

When you run the following sketch, you will see the LED light at two different intensities.

// Henry's Bench
// LED PWM Instensity

int ledPin = 10;    

void setup() {
  // nothing 
}

void loop() {
    analogWrite(ledPin, 0);  
    delay(500); 
    analogWrite(ledPin, 240);    
    delay(1000);
 
}