PWM shenanigans

Hello,

I am testing the basics of a PWM output on an esp32 WROOM. If I believe this diagram (https://docs.espressif.com/projects/esp-idf/en/v5.1/esp32/_images/esp32-devkitC-v4-pinout.png) pins 0, 2 and 21 seem to support PWM.

But in practice, the following program only works with pin 0. The others do not produce any signal to the oscilloscope ...


#define pwmChannel 0    
#define frequence 500  
#define resolution 8    

#define BUZZER_PIN 0
//#define BUZZER_PIN 21

void setup() {
    //pinMode(BUZZER_PIN, OUTPUT);

  ledcAttach(BUZZER_PIN, frequence, resolution);
  ledcWrite(pwmChannel, 0);
}

void loop() {
  ledcWrite(
    pwmChannel,
    127
  );

  delay(100);
  ledcWrite(
    pwmChannel,
    0
  );

  delay(100);
}

Do you have any idea why?

Which version of the ESP32 board files are you using ?

I seem to remember changes to the ledc functionality in recent versions, but I could be wrong

esp32 3.0.5

According to this reference that function returns a bool.


It might be worth using Serial.println() to print the return value. Not a solution, but at least you will know whether or not the function thinks it worked okay or not

1 Like

I will try :slight_smile:

You need to use write channel
bool ledcWriteChannel(uint8_t channel, uint32_t duty);
ledcWrite expects a pin number not a channel nomber

1 Like

Bien vu, I will check this too !

This may be significant

https://docs.espressif.com/projects/arduino-esp32/en/latest/migration_guides/2.x_to_3.0.html

it works :slight_smile: thank you

You may also consider using
https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html#ledcattachchannel

It assigns both the channel and pin number

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