/*
ledcWrite_RGB.ino
Runs through the full 255 color spectrum for an rgb led
Demonstrate ledcWrite functionality for driving leds with PWM on ESP32
This example code is in the public domain.
Some basic modifications were made by vseven, mostly commenting.
*/
// Set up the rgb led names
uint8_t ledR = 25;
uint8_t ledG = 26;
uint8_t ledB = 27;
uint8_t incremento = 5;
uint32_t dutyR, dutyG, dutyB;
const boolean invert = true; // set true if common anode, false if common cathode
uint8_t color = 0; // a value from 0 to 255 representing the hue
uint32_t R, G, B; // the Red Green and Blue color components
uint8_t brightness = 255; // 255 is maximum brightness, but can be changed. Might need 256 for common anode to fully turn off.
// the setup routine runs once when you press reset:
void setup()
{
Serial.begin(115200);
delay(10);
// Initialize pins as LEDC channels
// resolution 1-16 bits, freq limits depend on resolution
ledcAttach(ledR, 5000, 8); // 12 kHz PWM, 8-bit resolution
ledcAttach(ledG, 5000, 8);
ledcAttach(ledB, 5000, 8);
}
// void loop runs over and over again
void loop()
{
while (1)
dutyR = (dutyR + incremento * 2) % 256;
dutyG = (dutyG + incremento) % 256;
dutyB = (dutyB + incremento * 3) % 256;
// write the RGB values to the pins
ledcWrite(ledR, dutyR); // write red component to channel 1, etc.
ledcWrite(ledG, dutyG);
ledcWrite(ledB, dutyB);
printf("Duty Cycle - R: %d, G: %d, B: %d\n", dutyR, dutyG, dutyB);
delay(100); // full cycle of rgb over 256 colors takes 26 seconds
}