LED Tutorial for Arduino, ESP8266 and ESP32

LED Tutorial for Arduino, ESP8266 and ESP32

In this tutorial you learn how to control different LEDs with your Arduino, ESP8266 or ESP32 based micrcontroller.

The table of contents shows the different types of LEDs that are included in this tutorial.

Light in the dark

Table of Contents

In a lot of my blog posts I use some kind of a LED to show what is going on in the sketch, for example when a certain level of a sensor is reached, a LED will go on. Because there are more lights and different kinds of LED available, we cover all these different devices in this tutorial. With this knowledge you can make your next project more special and awesome.

In this tutorial we often use a PWM signal to change the colors of the LEDs. If you do not know what a PWM signal is or find out more about PWM here is a separate article only about PWM.

In the following examples we will use the Pulse Width Modulation (PWM) function of the microcontrollers.

If you do not know what PWM is, you find here an article about every thing you have to know about PMW. Make sure that you only use PWM pins if you need this functionality. For the ESP8266 based boards, all digital I/O pins are PWM ready. For the Arduino boards you find an overview of the different pinouts in the following articles: Arduino Nano, Arduino Uno, Arduino Mega.

The following table gives you an overview of all components and parts that I used for this tutorial. If you want to support my work, you can buy something from the following links and I will earn a small commission. This does not affect the price you pay for the products.

If you are interested in components and parts that I used in other tutorials, visit the components and parts page.

 Arduino UnoAmazonBanggoodAliExpress
ORESP8266 NodeMCUAmazonBanggoodAliExpress
ORESP32 NodeMCUAmazonBanggoodAliExpress
ANDTwo-color LED (KY-011), SMD RGB (KY-009), RGB LED (KY-016), 7 Color Flash LED (KY-034) and Light Blocking (KY-010) are all part of a Sensor PackAmazonBanggoodAliExpress

Two-color LED (KY-011)

Two-color LED

The Two-color LED (KY-011) module is a bi-color LED because the module emits red and green light. Using PWM you can adjust the amount of each color. Therefore the LED can be completely red or green and all combinations between depending on the inputs for the pins. This defines also the number of pins the KY-011 has:

  • One pin for the red light input
  • One pin for the green light input
  • Ground pin to close the circuit

The operation voltage of the two-color LED KY-011 is between 2V and 2.5V. Therefore we need a resistor in series for the red and green light input to reduce the output voltage on our micocontroller. We will use 330Ω resistors for the Arduino with an operation voltage of 5V and 100Ω resistors for ESP8266 based microcontrollers with an operation voltage of 3.3V, that you can also see in the following Fritzing sketch.

We want to make a short example sketch for the Two-color LED which alternate the color of the LED between red and green. Therefore we use PWM pins to loop between the green and the red color. If you are not familiar with Pulse Width Modulation (PWM) you find here a detailed article about PWM. It is also possible to only use the full red and green color if you want to make an example for a battery charging project.

  • Battery fully charged: Green LED
  • Battery empty: Red LED
  • Battery is getting charged: Both LED on, so yellow.
int Led_Red = 10;
int Led_Green = 11;
 
int val;
 
void setup () {
  // Output pin initialization for the LEDs
  pinMode (Led_Red, OUTPUT); 
  pinMode (Led_Green, OUTPUT); 
}
void loop () {
   // In this for loop, the two LEDs will get different PWM-Values. 
   // Via mixing the brightness of the different LEDs, you will get different colors. 
   for (val = 255; val> 0; val--)
      {
      analogWrite (Led_Green, val);
      analogWrite (Led_Red, 255-val);
      delay (15);
   }
   // You will go backwards through the color range in this second loop.
   for (val = 0; val <255; val++)
      {
      analogWrite (Led_Green, val);
      analogWrite (Led_Red, 255-val);
      delay (15);
   }
}
LED Tutorial for Arduino and ESP8266 - Two color LED

SMD RGB (KY-009)

SMD RGB

The SMD RGB (KY-009) is like the Two-color LED (KY-011) but is able to emit red, green and blue instead of only red and green. Therefor all RGB full colors can be emitted by the SMD RGB module due to the mix of red, green and blue. Like the Two-color LED (KY-011) also the SMD RGB (KY-009) use PWM to adjust the amount of each color. The module has 4 pins to connect to your microcontroller. The following pictures shows you how to connect the KY-011 to your Arduino or ESP8266 based microcontroller.

Important: The LED SMD Module consists of a 5050 SMD LED which has to prevent against burnout with limiting resistors. The following table shows you the operating voltage and the used resistors to prevent the burnout.

We want to make a short example sketch for the SMD RGB (KY-009) which cycle through various colors by changing the PWM value on each of the three primary colors.

int Led_Red = 10;
int Led_Green = 11;
int Led_Blue = 9;
 
int val;
 
void setup () {
  // Output pin initialization for the LEDs
  pinMode (Led_Red, OUTPUT); 
  pinMode (Led_Green, OUTPUT); 
  pinMode (Led_Blue, OUTPUT); 
}
void loop () {
   // In this for loop, the two LEDs will get different PWM-Values. 
   // Via mixing the brightness of the different LEDs, you will get different colors. 
   for (val = 255; val> 0; val--)
      {
      analogWrite (Led_Red, val);
      analogWrite (Led_Blue, 255-val);
      analogWrite (Led_Green, 128-val);
      delay (15);
   }
   // You will go backwards through the color range in this second loop.
   for (val = 0; val <255; val++)
      {
      analogWrite (Led_Red, val);
      analogWrite (Led_Blue, 255-val);
      analogWrite (Led_Green, 128-val);
      delay (15);
   }
}
LED Tutorial for Arduino and ESP8266 - SMD RGB

RGB LED (KY-016)

RGB LED

The RGB LED module is the same like the SMD RGB (KY-009). It only has a slightly different operating voltage, see the table below. But because the operating voltage differs only a little we use the same resistors to prevent the burnout of the module.

For the RGB LED we use the same example sketch like for the SMD RGB (KY-009) which cycle through various colors by changing the PWM value on each of the three primary colors.
int Led_Red = 10;
int Led_Green = 11;
int Led_Blue = 9;
 
int val;
 
void setup () {
  // Output pin initialization for the LEDs
  pinMode (Led_Red, OUTPUT); 
  pinMode (Led_Green, OUTPUT); 
  pinMode (Led_Blue, OUTPUT); 
}
void loop () {
   // In this for loop, the two LEDs will get different PWM-Values. 
   // Via mixing the brightness of the different LEDs, you will get different colors. 
   for (val = 255; val> 0; val--)
      {
      analogWrite (Led_Red, val);
      analogWrite (Led_Blue, 255-val);
      analogWrite (Led_Green, 128-val);
      delay (15);
   }
   // You will go backwards through the color range in this second loop.
   for (val = 0; val <255; val++)
      {
      analogWrite (Led_Red, val);
      analogWrite (Led_Blue, 255-val);
      analogWrite (Led_Green, 128-val);
      delay (15);
   }
}
LED Tutorial for Arduino and ESP8266 - RGB LED

7 Color Flash LED (KY-034)

7 Color Flash LED

The 7 color flash LED changes its color every 2-3 seconds automatically and includes 7 colors in total. Therefore the module has only 2 pins for the signal and the ground. The operation voltage is between 3.3V and 5V, so no resistor is needed.

In the following sketch we turn on the LED for 20 seconds and then start the sketch all over again.

void setup () {
  // Initialize the digital pin as an output.
  pinMode (11, OUTPUT);
}

void loop () {
  digitalWrite (11, HIGH); // set the LED on
  delay (20000); // wait for a second
}
LED Tutorial for Arduino and ESP8266 - 7 Color Flash LED

Light Blocking (KY-010)

Light Blocking

The KY-010 photo interrupter module consists of an optical emitter/detector in the front and two resistors (1kΩ and 33Ω) in the back. The sensor uses a beam of light between the emitter and the detector to check if the path between both is being blocked by an opaque object.
The operation voltage is between 3.3V and 5V, therefore no resistor in series is needed to prevent burnout.

In the following example, the microcontroller will detect if the sensor beam is blocked. If this is true, a LED should be turned on as long as the beam is blocked.

int Led = 7;           // define LED pin
int LightBlocker = 11; // define photo interrupter signal pin
int val;               //define a numeric variable

void setup()
{
  pinMode(Led, OUTPUT); // LED pin as output
  pinMode(LightBlocker, INPUT); //photo interrupter pin as input
}

void loop()
{
  val=digitalRead(LightBlocker); //read the value of the sensor 
  if(val == HIGH) // turn on LED when sensor is blocked 
  {
    digitalWrite(Led,HIGH);
  }
  else
  {
    digitalWrite(Led,LOW);
  }
}
LED Tutorial for Arduino and ESP8266 - Light Blocking

Conclusion

I hope that you now know the differences between the different LED modules that are available on the market. Because there are some major differences, you should know what LED fits your project right. If you have any questions about this article, please the use comment section below to ask some questions. And I would be really happe if you share this article with your friends.

Leave A Comment