I have attached an RGB-LED to three output lines of the ESP32 and I'm trying to fade all colors independently. Since it didn't work with my code, I went back to the basic ESP32 ledcWrite_RGB sample program, extracts copied for reference:
uint8_t ledR = 25;
uint8_t ledG = 33;
uint8_t ledB = 32;
void setup()
{
Serial.begin(115200);
ledcAttachPin(ledR, 1); // assign RGB led pins to channels
ledcAttachPin(ledG, 2);
ledcAttachPin(ledB, 3);
// Initialize channels
// channels 0-15, resolution 1-16 bits, freq limits depend on resolution
// ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
ledcSetup(1, 12000, 8); // 12 kHz PWM, 8-bit resolution
ledcSetup(2, 12000, 8);
ledcSetup(3, 12000, 8);
}
void loop()
{
Serial.println("Send all LEDs a 255 and wait 2 seconds.");
ledcWrite(1, 255);
ledcWrite(2, 255);
ledcWrite(3, 255);
delay(2000);
// ... run some color fade tests ...
}
With this code, everything works as expected. Now according to the documentation the resolution (third parameter of ledcSetup) can be changed up to 20 bits. If I change the resolution to 12 bits (and increase the max value to 12^2 - 1 = 4095) it still works fine. But if I change it to 13 bits, all of a sudden, nothing happens any more. The LED stays dark all the time.
Now I know that a PWM resolution of 12 or 13 bits isn't particularly useful for driving a LED, but still I would like to know why this isn't working.
Edit I just found that the kernel dumps this message when it's not working:
[ 3][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
E (31) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=208
[ 33][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed!
E (37) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=208
[ 50][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed!
E (54) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=208
[ 67][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed!
Does this mean that there's a limit imposed by the product of frequency and resolution?