Skip to main content
ESPBoards

ESP32 KY-011 Two-Color LED Module

The KY-011 is a two-color LED module featuring a common cathode 3mm LED capable of emitting red and green light. By adjusting the intensity of each color using PWM, various colors can be achieved. It's ideal for visual feedback in electronic projects.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-011 Two-Color LED Module Datasheet ButtonKY-011 Two-Color LED Module Specs ButtonKY-011 Two-Color LED Module Specs ButtonKY-011 Two-Color LED Module Specs Button

🛒 KY-011 Price

KY-011 Two-Color LED Module
Normally, the KY-011 Two-Color LED Module costs around 1$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ About KY-011 Two-Color LED Module

The KY-011 Two-Color LED Module features a 3mm common cathode LED capable of emitting red and green light. By adjusting the intensity of each color using PWM, various colors can be achieved. The module operates at a forward voltage between 2.0V and 2.5V with a typical forward current of 20mA. It's compatible with microcontrollers like Arduino and ESP32, making it suitable for visual feedback in projects.

⚙️ KY-011 Sensor Technical Specifications

Below you can see the KY-011 Two-Color LED Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Type: module
  • Protocol: Digital
  • Forward Voltage: 2.0V - 2.5V
  • Forward Current: 20mA
  • LED Diameter: 3mm
  • Colors: Red and Green
  • Beam Angle: 150°
  • Wavelength: Red: 644nm, Green: 571nm
  • Luminous Intensity: Red: 40-80 mcd, Green: 20-40 mcd

🔌 KY-011 Sensor Pinout

Below you can see the pinout for the KY-011 Two-Color LED Module. The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32!

  • Pin (G): Ground (GND).
  • Pin (R): Red LED anode.
  • Pin (Y): Green LED anode.

🧵 KY-011 Wiring with ESP32

Below you can see the wiring for the KY-011 Two-Color LED Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.

  • KY-011 Pin (G): Connect to ESP32 GND.
  • KY-011 Pin (R): Connect to ESP32 GPIO (e.g., GPIO18) through a current-limiting resistor.
  • KY-011 Pin (Y): Connect to ESP32 GPIO (e.g., GPIO19) through a current-limiting resistor.

🛠️ KY-011 Two-Color LED Module Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.

❌ LED Not Lighting Up

Issue: The LED does not illuminate when expected.

Solutions:

  • Ensure the module is properly connected to the microcontroller.
  • Verify that appropriate current-limiting resistors are used to prevent damage to the LEDs.
  • Check the code to ensure the correct GPIO pins are being controlled.

🎨 Incorrect Color Displayed

Issue: The LED displays an unexpected color.

Solutions:

  • Confirm that the correct GPIO pins are connected to the respective LED anodes.
  • Adjust the PWM values in the code to achieve the desired color mix.
  • Ensure that the common cathode is connected to ground.

💻 Code Examples

Below you can find code examples of KY-011 Two-Color LED Module with ESP32 in several frameworks:

If you encounter issues while using the KY-011 Two-Color LED Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-011 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-011 Two-Color LED Module:

int redPin = 10; // Pin for red LED
int greenPin = 9; // Pin for green LED

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}

void loop() {
digitalWrite(redPin, HIGH); // Red LED on
digitalWrite(greenPin, LOW); // Green LED off
delay(3000); // Wait for 3 seconds
digitalWrite(redPin, LOW); // Red LED off
digitalWrite(greenPin, HIGH); // Green LED on
delay(3000); // Wait for 3 seconds
}

This Arduino code alternates the KY-011 module's red and green LEDs every 3 seconds. Pins 10 and 9 are configured as outputs for the red and green LEDs, respectively.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 KY-011 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-011 Two-Color LED Module, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/ledc.h"

#define RED_PIN GPIO_NUM_18
#define GREEN_PIN GPIO_NUM_19
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_HIGH_SPEED_MODE
#define LEDC_CHANNEL_R LEDC_CHANNEL_0
#define LEDC_CHANNEL_G LEDC_CHANNEL_1

void configure_led_pwm(gpio_num_t pin, ledc_channel_t channel) {
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.timer_num = LEDC_TIMER,
.duty_resolution = LEDC_TIMER_8_BIT,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);

ledc_channel_config_t ledc_channel = {
.gpio_num = pin,
.speed_mode = LEDC_MODE,
.channel = channel,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}

void set_led_color(uint32_t red, uint32_t green) {
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_R, red);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_R);
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_G, green);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_G);
}

void app_main(void) {
configure_led_pwm(RED_PIN, LEDC_CHANNEL_R);
configure_led_pwm(GREEN_PIN, LEDC_CHANNEL_G);

while (1) {
set_led_color(255, 0); // Red on, Green off
vTaskDelay(pdMS_TO_TICKS(3000));
set_led_color(0, 255); // Red off, Green on
vTaskDelay(pdMS_TO_TICKS(3000));
}
}

This ESP-IDF code configures the KY-011 two-color LED module using the ESP32's LEDC PWM hardware. GPIO18 controls the red LED, and GPIO19 controls the green LED. The configure_led_pwm() function initializes PWM for each LED channel, allowing brightness control. The LEDs alternate between red and green every 3 seconds.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 KY-011 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-011 Two-Color LED Module

output:
- platform: ledc
pin: GPIO18
id: red_led
- platform: ledc
pin: GPIO19
id: green_led

light:
- platform: monochromatic
name: "KY-011 Red LED"
output: red_led
gamma_correct: 2.8
- platform: monochromatic
name: "KY-011 Green LED"
output: green_led
gamma_correct: 2.8

This ESPHome configuration sets up the KY-011 two-color LED module with LEDC PWM on GPIO18 for red and GPIO19 for green. The LEDs can be controlled individually to create different color effects.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-011 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

ESP32 KY-011 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-011 Two-Color LED Module:

#include <Arduino.h>

#define RED_PIN 18
#define GREEN_PIN 19

void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
}

void loop() {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
delay(3000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
delay(3000);
}

This PlatformIO code sets up GPIO18 and GPIO19 as outputs to control the KY-011 module. The red and green LEDs alternate every 3 seconds.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-011 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-011 Two-Color LED Module with your ESP32.

import machine
import time

RED_PIN = machine.Pin(18, machine.Pin.OUT)
GREEN_PIN = machine.Pin(19, machine.Pin.OUT)

while True:
RED_PIN.value(1)
GREEN_PIN.value(0)
time.sleep(3)
RED_PIN.value(0)
GREEN_PIN.value(1)
time.sleep(3)

This MicroPython script configures GPIO18 and GPIO19 as outputs for the KY-011 module. It alternates the red and green LEDs every 3 seconds.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of KY-011 Two-Color LED Module, its pinout, connection with ESP32 and KY-011 Two-Color LED Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.