Page 1 of 1

PWM Signal problems

Posted: Sun Apr 21, 2024 7:26 pm
by hdsjulian2
On my ESP32 S2 (self created hardware design, schematic can be found here or embedded at the bottom:
https://preview.redd.it/esp32s2-wont-sh ... e38d7165c4
I have, however, tested the hardware thoroguhly with a multimeter. this part is fine.

I have defined:

Code: Select all

  
  #define LEDC_TIMER_8_BIT  8
   #define LEDC_BASE_FREQ     5000
    const int ledPinBlue1 = 18; 
    const int ledPinRed1 = 38;
    const int ledPinGreen1 = 8;
    const int ledPinGreen2 = 3;
    const int ledPinRed2 = 9;
    const int ledPinBlue2 = 37;
according to my hardware schematic.

in my setup() i have

Code: Select all

  ledcAttach(ledPinRed1, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT);
  ledcAttach(ledPinGreen1, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT);
  ledcAttach(ledPinBlue1, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT);
  ledcAttach(ledPinRed2, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT);
  ledcAttach(ledPinGreen2, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT);
  ledcAttach(ledPinBlue2, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT);
(now I am aware that IO18 is a special pin and shouldn't be used for PWM output, but that is not my problem here. )

now, so far all is fine, BUT:

i then created a class ledHandler {}
and moved the ledcAttach() parts in a setup() method which i called from the global setup()
with ledHandler.setup().

and then, when I use

Code: Select all

ledcWrite(ledPinRed1, 128);
ledcWrite(ledPinGreen1, 128);
ledcWrite(ledPinBlue1, 128);
ledcWrite(ledPinRed2, 128);
ledcWrite(ledPinGreen2, 128);
ledcWrite(ledPinBlue2, 128);
green and blue work just fine. Red does not.

I mean, i fixed the bug by moving the ledcAttach() parts back to global setup() but does anyone have a n explanation for this?

Re: PWM Signal problems

Posted: Tue Apr 23, 2024 7:25 am
by liaifat85
hdsjulian2 wrote:
Sun Apr 21, 2024 7:26 pm
I mean, i fixed the bug by moving the ledcAttach() parts back to global setup() but does anyone have an explanation for this?
If the ledHandler class or its setup() method has different scope or lifetime compared to the global setup, it could potentially lead to unexpected behaviour. It is more like to create problem especially if there are interactions with other parts of your code that affect the PWM operation.
If you want to generate PWM signal with ESP32 using Arduino IDE, you can check this tutorial out: https://www.theengineeringprojects.com/ ... 2-pwm.html