0

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?

4
  • it can only turn the pin on and off so many times per second. When you use more bits, it uses more ons/offs, heh; that's what a bit is. Think of a human analogy; you can recite more 5 digit codes in one minute than you can 10 digit codes... There's a tradeoff between resolution and frequency as you've noticed. There's nothing you can do to "fix" this hardware limit. Commented Dec 20, 2022 at 23:43
  • and if you don't have audio feedback issues with your hardware (the leds or driver buzzing), there's little human advantages to anything over about 4khz, and as you increase frequency, your switching costs will rise, generating more heat and increasing the effects of any capacitance in the wiring or strips, which (ironically) further reduces the practical resolution (number of dim levels achievable). Commented Dec 20, 2022 at 23:47
  • @dandavis I'm seeing that it has to do with the internal oscillator required, but the frequency of the (theoretical) PWM signal doesn't change when the resolution is increased (only the duty cycle can be set more accurately). What I'm actually looking for is a documentation about this. How is the limit of frequency x resolution defined? Commented Dec 21, 2022 at 7:10
  • If you use, say, 16b instead of 8b, or go slower in freq, then the equivalent "010" will become "001100" or "000111000" as needed, so while the apparent output won't change, the internals that encode the duty cycle will. You can look at the freq of a 2 bit or the rez of a max freq to get an idea of the over-all bandwidth available. iirc, it's about 80mbps/2^bits. Commented Dec 21, 2022 at 17:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.