Change LEDC frequency and pwm on the fly without glitches

fnoguez
Posts: 6
Joined: Mon Sep 07, 2020 2:03 am

Change LEDC frequency and pwm on the fly without glitches

Postby fnoguez » Mon Nov 30, 2020 10:59 pm

Hello to every one.

I'm trying set LEDC frequency and PWM on the fly and the code works but not the way I'm expecting, there are bumps or glitches every time de frequency gets changed and also the PWM has glitches but is a little less notorious.

For both Frequency and PWM I need a glitch free transitions and frequencyresolution should be at least 0.1 Hz.

I'm using arduino-esp32 esp32-hal-ledc.h, esp32-hal-ledc.c

I've read on ESP32_TECHNICAL_REFERENCE_manual that using LEDC High Speed Channels transitions are glitch free.

But I don't know how to achieve it primarily because I'm not a C/C++ programmer and I'm very new to ESP32.

Below is a basic code I'm using.
main.cpp
(1.35 KiB) Downloaded 553 times
Any advice will be very wellcome

Best regards

Fernando

fnoguez
Posts: 6
Joined: Mon Sep 07, 2020 2:03 am

Re: Change LEDC frequency and pwm on the fly without glitches

Postby fnoguez » Wed Dec 02, 2020 3:46 am

Or a more defined question:
How can I integrate ledc_intr_type_t with the code attached in my last post?
So that it only update pwm frequency when LEDC Counter reaches max count, LEDC_INTR_MAX?

rowifi
Posts: 3
Joined: Thu Aug 13, 2020 3:42 pm

Re: Change LEDC frequency and pwm on the fly without glitches

Postby rowifi » Fri Jan 22, 2021 3:53 pm

I'm sort of doing the same - and having similar problems..
ESP32 ledc PWM features and functions.
The Arduino framework seems to allow only a subset of the Ledc PWM features.
The ESP has auto fading functionality but the Arduino framework does not appear to support these.
Also there appears not to be a method to change the duty cycle without glitches.. The ESP framework has an update_duty function which may accomplish this.
Has anyone successfully used ledc in Arduino to get glitchless live adjustments of the ledc PWM output?
Perhaps accessing the Espressif framework at a lower level may achieve that (I've tried but it didn't work ).
Which begs another question, since I can access the espressif functions, why is there an Arduino layer on top?

zimgir
Posts: 1
Joined: Fri Jun 10, 2022 3:31 pm

Re: Change LEDC frequency and pwm on the fly without glitches

Postby zimgir » Fri Jun 10, 2022 3:40 pm

Hi,

I had the same problem.

I dug in to ESP IDF source code to figure out what the problem is.

I found out that calling "ledc_set_freq" to change the frequency always resets the timer which is the source of the glitches.

Basically this means that even for high speed timer which is glitch free you will see glitches because of this reset.

I didn't find another API to set frequency on the fly so to fix the issue I copied and modified the ESP IDF code to make it possible to set frequency without glitches with my own "ledc_set_freq_no_reset" function.

You can find reference for my fix here:

https://github.com/zimgir/esp-sstc-cont ... dc_fix.cpp

Please note that I use Arduino framework with PlatformIO in VSCode so it allows me to call ESP IDF functions directly and bypass Arduino layer if I need to mess with low level stuff while still having the options to stay at Arduino level if I want to.

cybermaus
Posts: 1
Joined: Mon Dec 18, 2023 9:19 pm

Re: Change LEDC frequency and pwm on the fly without glitches

Postby cybermaus » Mon Dec 18, 2023 9:23 pm

Hi @zimgir

Like me, you have a single post, so you probably do not read this, but I want to thank you for your workaround.
I borrowed the code from your GIT and got it to work. Nice, glitch-free freq and duty adjustments from within the Arduino framework on VStudio/PlatformIO

Thanks!

Protonx80
Posts: 1
Joined: Mon Sep 16, 2024 7:53 am

Re: Change LEDC frequency and pwm on the fly without glitches

Postby Protonx80 » Mon Sep 16, 2024 7:56 am

A big thank you from me too the code worked beautifully without any "glitches" ;)

Who is online

Users browsing this forum: MicroController and 91 guests