ESP32 - Generate 4 variable frequency dynamic signals without glitches

[en] Good morning all !
I come to the forum because I can't find a solution to my problem, I hope you can help me!
I'm using an esp32 with the Arduino IDE to generate 4 variable frequency signals (frequency adjusted every second).
It works except that each time I want to change the frequency (using the ledcSetup function), the counter resets, which creates a period on the signal with a duty cycle of 0% ( Like on the photo ), the next period being correct ( duty cycle and frequency)
It's quite annoying for my project which uses strip led, because this problem generates flashing.
I tried other functions (like ledc_set_freq) but this one doesn't seem to work (or I didn't understand how to use it).
If I don't change the frequency, the signals are correct and error free.
Do you have an idea on why I can't change the frequency on the fly?
A huge thank you in advance!

[fr] bonjour à tous !
je viens sur le forum car je ne trouve pas de solution à mon problème, j'espère que vous pourrez m'aider !
J'utilise un esp32 avec l'IDE Arduino pour générer 4 signaux de fréquence variable (fréquence ajustée toutes les secondes).
Ca fonctionne sauf que à chaque fois que je veux changer la fréquence (en utilisant la fonction ledcSetup), le compteur se réinitialise, ce qui créer sur le signal une période avec un rapport cyclique de 0% ( comme sur la photo ), la période d'après étant correct (rapport cyclique et fréquence)
C'est assez gênant pour mon projet qui utilise du strip led, car ce problème génère un clignotement.
J'ai essayé d'autres fonctions (comme ledc_set_freq) mais celle ci ne semble pas fonctionner (ou je n'ai pas compris comment m'en servir).
Si je ne modifie pas la frequence, les signaux sont correct et sans erreur.
Auriez vous une idée, une piste, sur pourquoi je ne peux pas changer la fréquence à la volée ?
Un immense merci d'avance !

Mon code :

void loop() 
{
  valPotar = analogRead(potar);
  valPotar = map (valPotar, 0, 4096, 10, 100);
  frequenceR  = 50 ;
  frequenceG  = 100 ;
  frequenceB  = 150 ;
  frequenceUV = 200 ;

  ledcSetup(1, frequenceR, 8);
  ledcSetup(2, frequenceG, 8);
  ledcSetup(4, frequenceB, 8);
  ledcSetup(8, frequenceUV, 8);

  ledcWrite(1, alphaR);
  ledcWrite(2, alphaG);   
  ledcWrite(4, alphaB);
  ledcWrite(8, alphaUV);
  
  delay(1000);
}

Docs for the LEDC API,

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html

I tend to use the MCPWM to generate PWM instead of the LEDC API. The LEDC API uses the hardware timers to generate PWM which will need to be reset when changing frequencies. When I used the LEDC API I found it was best to designate a hardware timer for each LEDC channel instead of allowing the ESP32's OS to control the hardware timers.

The MCPWM uses its own internal timers, Motor Control Pulse Width Modulator (MCPWM) - ESP32 - — ESP-IDF Programming Guide latest documentation and may not suffer from the same issues the LEDC has.

good luck.

Great, thank you !
I will look at it.

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