Index
Introduction
In this tutorial, we’ll create a smooth color-fading effect using an RGB LED.
You’ll see the LED gradually transition through red, green, blue, and other colors.
This helps you understand how to mix colors using simple code.
It’s a great project for beginners to start working with RGB LEDs.
Required Components
- ESP32-S3 Board
- KY-016 RGB LED module
- Jumper wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- RGB (GND) → ESP32-S3 GND
- RGB (RED) → ESP32-S3 4
- RGB (GREEN) → ESP32-S3 5
- RGB (BLUE) → ESP32-S3 6

Code / Programming
/*
Filename: esp32-S3_rgb_led_analogwrite.ino
Description: RGB LED fade using analogWrite()
Author: www.oceanlabz.in
Modification Date: 1/4/2025
*/
const int redPin = 4;
const int greenPin = 5;
const int bluePin = 6;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
fadeToColor(255, 0, 0); // Red
fadeToColor(0, 255, 0); // Green
fadeToColor(0, 0, 255); // Blue
fadeToColor(255, 255, 0); // Yellow
fadeToColor(0, 255, 255); // Cyan
fadeToColor(255, 0, 255); // Magenta
fadeToColor(255, 255, 255);// White
fadeToColor(0, 0, 0); // Off
}
void fadeToColor(int r, int g, int b) {
for (int i = 0; i <= 255; i++) {
analogWrite(redPin, r * i / 255);
analogWrite(greenPin, g * i / 255);
analogWrite(bluePin, b * i / 255);
delay(5);
}
}
Explanation
- This code smoothly fades an RGB LED through various colors using
analogWrite()
on the ESP32-S3. - Each
fadeToColor()
call gradually increases brightness from 0 to the target RGB values. analogWrite()
directly controls LED brightness using PWM, compatible with ESP32 Arduino Core 3.3.0.
Troubleshooting
- Ensure you are using the ESP32 Board version 3.3.0 or newer where
analogWrite()
is supported. - Double-check that the RGB pins (4, 5, 6) are PWM-capable and not used by other hardware functions.
- If colors don’t appear correctly, verify the common cathode/anode type of your RGB LED and adjust logic accordingly.