ESP32 KY-029 Dual Color LED Module
The KY-029 is a dual-color LED module featuring a 3mm LED that emits red and green light. By adjusting the intensity of each color using PWM, various color combinations can be achieved. The module's common cathode design simplifies integration with microcontrollers for visual indicators in projects.
⬇️ Jump to Code Examples
🔗 Quick Links
🛒 KY-029 Price
ℹ️ About KY-029 Dual Color LED Module
The KY-029 Dual Color LED Module features a 3mm LED capable of emitting red and green light. By adjusting the intensity of each color using PWM, you can create various color combinations. The module operates with a common cathode configuration, simplifying control with microcontrollers.⚙️ KY-029 Sensor Technical Specifications
Below you can see the KY-029 Dual 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.
- Protocol: Digital
- Operating Voltage: 2.0V to 2.5V
- Forward Current: 20 mA
- LED Diameter: 3 mm
- LED Colors: Red and Green
- Beam Angle: 150°
🔌 KY-029 Sensor Pinout
Below you can see the pinout for the KY-029 Dual 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 S (Signal):
Connects to the anode of the red or green LED.Pin middle (VCC):
Connects to the common cathode (GND).Pin - (GND):
Not connected.
🧵 KY-029 Wiring with ESP32
Below you can see the wiring for the KY-029 Dual 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.
Pin S:
Connect to a PWM-capable GPIO pin on the ESP32 (e.g.,GPIO16
).Pin middle:
Connect to ESP32GND
.Pin -:
Not connected.
🛠️ KY-029 Dual 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 emit any light.
Solutions:
- Ensure proper wiring connections between the module and the microcontroller.
- Verify that the microcontroller's GPIO pins are correctly configured as outputs.
- Check if appropriate current-limiting resistors are used to prevent LED damage.
🎨 Incorrect Color Display
Issue: The LED displays unexpected colors or brightness levels.
Solutions:
- Confirm that the PWM signals are correctly configured for both red and green LEDs.
- Ensure that the common cathode is properly connected to ground.
- Check for any short circuits or loose connections on the module.
💻 Code Examples
Below you can find code examples of KY-029 Dual Color LED Module with ESP32 in several frameworks:
If you encounter issues while using the KY-029 Dual Color LED Module, check the Common Issues Troubleshooting Guide.

ESP32 KY-029 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the KY-029 Dual Color LED Module:
int led_red = 11; // Pin for red
int led_green = 10; // Pin for green
void setup() {
// Initialization of output pins for the LEDs
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
}
void loop() {
digitalWrite(led_red, HIGH); // Red LED is switched on
digitalWrite(led_green, LOW); // Green LED is switched off
delay(3000); // Wait for 3 seconds
digitalWrite(led_red, LOW); // Red LED is switched off
digitalWrite(led_green, HIGH); // Green LED is switched on
delay(3000); // Wait for a further 3 seconds in which the LEDs are then switched over
}
This Arduino code alternates between turning on the red and green LEDs every 3 seconds. Pins 11 and 10 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.

ESP32 KY-029 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the KY-029 Dual 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/ledc.h"
#include "driver/gpio.h"
#define LED_RED_PIN GPIO_NUM_16
#define LED_GREEN_PIN GPIO_NUM_17
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define LEDC_CHANNEL_RED LEDC_CHANNEL_0
#define LEDC_CHANNEL_GREEN LEDC_CHANNEL_1
#define LEDC_DUTY_RES LEDC_TIMER_8_BIT
#define LEDC_FREQUENCY 5000
void app_main(void) {
// Configure timer for LEDC
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.duty_resolution = LEDC_DUTY_RES,
.timer_num = LEDC_TIMER,
.freq_hz = LEDC_FREQUENCY,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
// Configure LEDC channel for red LED
ledc_channel_config_t ledc_channel_red = {
.speed_mode = LEDC_MODE,
.channel = LEDC_CHANNEL_RED,
.timer_sel = LEDC_TIMER,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LED_RED_PIN,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel_red);
// Configure LEDC channel for green LED
ledc_channel_config_t ledc_channel_green = {
.speed_mode = LEDC_MODE,
.channel = LEDC_CHANNEL_GREEN,
.timer_sel = LEDC_TIMER,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LED_GREEN_PIN,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel_green);
while (1) {
printf("Red LED ON\n");
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_RED, 255);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_RED);
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_GREEN, 0);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_GREEN);
vTaskDelay(pdMS_TO_TICKS(3000));
printf("Green LED ON\n");
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_RED, 0);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_RED);
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_GREEN, 255);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_GREEN);
vTaskDelay(pdMS_TO_TICKS(3000));
}
}
This ESP-IDF code configures GPIO16 and GPIO17 as PWM outputs using the LEDC peripheral. It alternates between turning on the red and green LEDs every 3 seconds, simulating a simple traffic light effect.
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.

ESP32 KY-029 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the KY-029 Dual Color LED Module
light:
- platform: monochromatic
name: "KY-029 Red LED"
output: led_red
- platform: monochromatic
name: "KY-029 Green LED"
output: led_green
output:
- platform: gpio
pin: GPIO16
id: led_red
- platform: gpio
pin: GPIO17
id: led_green
This ESPHome configuration sets up two monochromatic light entities for the red and green LEDs on GPIO16 and GPIO17. They can be controlled separately in Home Assistant to create different lighting effects.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.

ESP32 KY-029 PlatformIO Code Example
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-029 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the KY-029 Dual Color LED Module:
#include <Arduino.h>
#define LED_RED_PIN 16
#define LED_GREEN_PIN 17
void setup() {
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-029 Dual Color LED Module Test");
}
void loop() {
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_GREEN_PIN, LOW);
Serial.println("Red LED ON");
delay(3000);
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_GREEN_PIN, HIGH);
Serial.println("Green LED ON");
delay(3000);
}
This PlatformIO code alternates between turning on the red and green LEDs every 3 seconds, with messages printed to the serial monitor.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.

ESP32 KY-029 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the KY-029 Dual Color LED Module with your ESP32.
import machine
import time
LED_RED_PIN = machine.Pin(16, machine.Pin.OUT)
LED_GREEN_PIN = machine.Pin(17, machine.Pin.OUT)
while True:
LED_RED_PIN.on()
LED_GREEN_PIN.off()
print("Red LED ON")
time.sleep(3)
LED_RED_PIN.off()
LED_GREEN_PIN.on()
print("Green LED ON")
time.sleep(3)
This MicroPython script toggles the KY-029 red and green LEDs every 3 seconds using GPIO16 and GPIO17.
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-029 Dual Color LED Module, its pinout, connection with ESP32 and KY-029 Dual Color LED Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.