A light-emitting diode, or LED, is a component capable of emitting light when an electric current flows through it.
Today, there are LEDs in different colors (red, green, blue, yellow, white) and even LEDs that can display several colors: red, blue and green, such as RGB LEDs.
The LED only allows current to flow in one direction (from the anode to the cathode). The other direction is called the blocking direction.
There are different types of LEDS:
The LED has a very particular symbol in an electronic circuit. It is symbolized by a triangle and a line. The line shows that the current only passes in one direction in the diode: from the anode to the cathode.
The light-emitting diode is a very particular component: There is a blocking direction and a passing direction. The LED will only work if you connect the + voltage to the Anode and the – voltage to the Cathode.
Do not connect the LED without resistance!
As you can see from the voltage threshold table, the values are sometimes different from those that an Arduino card can deliver. As the Arduino card can only deliver 3.3V or 5V, it will be necessary to limit the current entering the LED in order to protect it. For this we will use a resistor. You have a demonstration of how to calculate its value in a circuit on our Resistance course.
However, if you want to save yourself the details of the calculations, here are the resistors to choose in relation to the input voltage:
We will now see how to connect an LED. The circuit opposite will not allow you to control the LED (make it flash), but just to understand how to turn it on:
In this part we will control the LED in two forms: first by turning it on and off, then by gradually turning it on.
Here is the circuit you will need for both parts:
Here is the code to turn an LED on and off. You can change the pin depending on where you plugged your LED into on the Arduino board:
int led_Pin = 11; // We assign the LED to pin 11.
void setup() {
pinMode(led_Pin, OUTPUT); // We assign the LED as output
}
void loop() {
digitalWrite(led_Pin,HIGH); // We turn on the LED
delay(1000); // We pause to see the LED on before turning it off
digitalWrite(led_Pin,LOW); // We turn off the LED
delay(1000);
}
We will now see how to vary the brightness of the LED with the Arduino card. To do this, you will need to connect the LED to one of the PWM pins on the Arduino board. The Pulse Width Modulation pins allow you to “vary” the voltage of the arduino board with pulses. They are represented by “~”. Here is the diagram for varying the intensity of the LED:
int led_Pin = 11; // We assign the LED to pin 11.
float value; // Brightness value in percentage.
void setup() {
pinMode(led_Pin, OUTPUT); // We assign the LED as output
}
void loop() {
for (int amount_counter = 0; amount_counter < 255; amount_counter++){
analogWrite(led_Pin, counter_amount); // We send the value to the LED
delay(10);
}
for (int descending_counter = 255; descending_counter > 0; descending_counter--){
analogWrite(led_Pin, counter_down); // We send the value to the LED
delay(10);
}
}