ledcAttach() on ESP32 for different frequencies

Tried to enable couple of PWM outputs on my ESP32 and found that if I change the frequency then all the outputs change their frequency. Is this intended behaviour?

I read somewhere that by using different timers one can generate few frequencies of different pins at time; looks like ledcXxxx() functions are all using the same timer

Are you using version 3 of the Arduino ESP32 core?

If so, you may wish to use ledcAttachChannel.

Not tried it yet but what I see from the debug output is that two different channels are autoselected:

Enable PWM of 40kHz on pin2:

[2554638][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x4011fbf0
[2554650][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 2 successfully set to type GPIO (1) with bus 0x3
[2554660][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type LEDC (12) successfully set to 0x400db2d0
[2554672][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 2 successfully set to type INIT (0) with bus 0x0
[2554682][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 2 successfully set to type LEDC (12) with bus 0x3ffb8c04
[2554692][I][esp32-hal-ledc.c:166] ledcAttachChannel(): LEDC attached to pin 2 (channel 0, resolution 10)

Measure frequency on pin4 which is connected to pin2 with piece of wire:

40013 pulses in 1.000 seconds (40013.0 Hz, 2 interrupts)

So far so good. Create second PWM channel of 60kHz:

[2575686][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x4011fbf0
[2575698][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 22 successfully set to type GPIO (1) with bus 0x17
[2575708][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type LEDC (12) successfully set to 0x400db2d0
[2575720][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 22 successfully set to type INIT (0) with bus 0x0
[2575730][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 22 successfully set to type LEDC (12) with bus 0x3ffb8e04
[2575740][I][esp32-hal-ledc.c:166] ledcAttachChannel(): LEDC attached to pin 22 (channel 1, resolution 10)

Note that Ch1 is used this time (last line in a codeblock above)

Now checking the frequency of both pins (pin23(counter) is connected to pin22(generator)):

Pin4:

% 60119 pulses in 1.000 seconds (60119.0 Hz, 3 interrupts)

Pin23:

% 60091 pulses in 1.000 seconds (60091.0 Hz, 3 interrupts)

Frequency has changed (60kHz for both). Now if I change frequency on pin2 then again both PWM generator will change frequency :-/

Suspecting that my frequency measurement might be incorrect I connected an LED to pin4. And I see that when I change PWM frequency at pin22 my LED changes its frequency too. It happens on all frequencies, even 1 hz.

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