Search code examples
esp32arduino-esp32

Esp32 frequency error after reinstall arduino ide


Last week for some reason I reinstalled my lap and also arduino ide. The issue I have is that on a code which worked fine on some esp's written before reinstall, now when i deploy it I get this error.

This is the piece of code :

const int PinCh1 = 13;
const int PinCh2 = 15;
const int freq = 5000;
const int Channel1 = 0;
const int Channel2 = 1;
const int resolution = 16;

ledcSetup(Channel1, freq, resolution);
ledcSetup(Channel2, freq, resolution);

This is the error:

E (76891) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=63

First time I assumed that is something wrong with the new esp's I bough, but I took one of the old one and I just wrote that code again on it and got same error. So I am almost sure that is something in this last version which I installed on arduino ide...I have no idea what I had before ..

Any idea on this ?

Thanks


Solution

  • Yes, because the resolution depends on the frequency you want to generate. For higher frequencies, you are limited to lower resolutions.

    You will have to change this -

    const int resolution = 13;

    As, 5 kHz can have a maximum duty resolution of 13 bits.

    This table lists some commonly used frequencies along with their maximum resolution.

    https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf#table.14.1

    Note that the max resolution and frequency also depend on the timer you are using. In the Arduino API for LEDC, the selection of timer and other timer configs are taken care of in the background. However, there are 3 timers available to you, each with its own frequency.

    There is a formula mentioned just above the table, you can use that formula to calculate the maximum available resolution for your desired frequency.