Fighting LEDC on ESP32

Hello everyone!

Until today I was absolutely sure that ledcAttach() / ledcWrite() can only be used to set 1 (one) frequency: whenever I tried to enable PWM on pin, say, #0 at 1kHz and then soon after that - 3kHz on pin #2 the frequency on both pins become the same - 3kHz. Then again I try to change frequency on pin #0 to 2kHz and both pins start to generate 2kHz signal.

I looked into Arduino Core's debug output and saw that two different LEDC channels are used: channel#0 and channel#1, but still I get frequency changing on both pins. So I was thinking that this is a hardware limitation: we can only have 1 frequency and 16 different duty cycles (there are 16 LEDC channels)

Today I accidently found (by trying different channels), that channels0 and 1 are grouped together and are only able to generate same frequency.

So I changed the source code for ledcAttach() so it chooses channels #0, #2, #4.. whenever requested frequency is different.

And it worked :). Alternatively one can use ledcAttachChannel( ... , CHANNEL) and use even numbers for channels to get 4 different frequencies (on basic ESP32)

I don't know yet if it is only channels 0 and 1 behave this way or channels 2,3 4,5 ... too.

There's 8 high speed pwm channels and 8 low speed. Each group has 4 timers, so 2ch per timer. So the behavior you found is equal for other channels as well.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.