Index
Introduction
The KY-016 RGB module allows you to produce a variety of colors by mixing red, green, and blue light.
It has built-in resistors for each color, making it easy to connect directly to the ESP32.
By using PWM on three GPIO pins, the ESP32 can control brightness levels and create smooth color transitions.
Required Components
- ESP32 Board
- KY-016 RGB LED module
- Jumper wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- RGB (GND) → ESP32 GND
- RGB (RED) → ESP32 25
- RGB (GREEN) → ESP32 26
- RGB (BLUE) → ESP32 27

Code / Programming
// Define RGB pins
const int redPin = 25;
const int greenPin = 26;
const int bluePin = 27;
// Define PWM channels
const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;
void setup() {
// Setup PWM channels: channel, frequency, resolution (8-bit)
ledcSetup(redChannel, 5000, 8);
ledcSetup(greenChannel, 5000, 8);
ledcSetup(blueChannel, 5000, 8);
// Attach channels to GPIOs
ledcAttachPin(redPin, redChannel);
ledcAttachPin(greenPin, greenChannel);
ledcAttachPin(bluePin, blueChannel);
}
void loop() {
// Red
setColor(255, 0, 0);
delay(1000);
// Green
setColor(0, 255, 0);
delay(1000);
// Blue
setColor(0, 0, 255);
delay(1000);
// Yellow
setColor(255, 255, 0);
delay(1000);
// Cyan
setColor(0, 255, 255);
delay(1000);
// Magenta
setColor(255, 0, 255);
delay(1000);
// White
setColor(255, 255, 255);
delay(1000);
// Off
setColor(0, 0, 0);
delay(1000);
}
// Set RGB color
void setColor(int red, int green, int blue) {
ledcWrite(redChannel, red);
ledcWrite(greenChannel, green);
ledcWrite(blueChannel, blue);
}
Explanation
- The KY-016 RGB module has three color channels (Red, Green, Blue) that can be controlled using PWM signals.
- The ESP32 uses
ledcWrite()
on PWM-capable GPIO pins to mix colors and create visual effects. - This setup allows you to display various colors by adjusting brightness levels for each LED component.
Troubleshooting
- If colors appear incorrect, check the RGB pin connections (R, G, B may be mislabeled or reversed).
- Make sure you’re using PWM-capable pins and have correctly configured
ledcSetup()
andledcAttachPin()
. - For common anode modules, reverse the logic (use
255 - value
) insetColor()
to get correct brightness.