PWM Signal problems

hdsjulian2
Posts: 15
Joined: Wed Apr 10, 2024 8:08 am

PWM Signal problems

Postby hdsjulian2 » Sun Apr 21, 2024 7:26 pm

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?

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: PWM Signal problems

Postby liaifat85 » Tue Apr 23, 2024 7:25 am

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

Who is online

Users browsing this forum: No registered users and 120 guests