ESP32 Varying PWM Frequency

I am trying to change the frequency of the PWM on the ESP32.

The frequency must be varied between 100Hz to 3KHz.
Screenshot 2021-05-24 at 21.20.19

This will be used to dim lights in various rooms around the house.

I only found this library for Arduino.

Any advice on how to vary the frequency?

you don't need that library. Use what the ESP provides ( ESP32 LED PWM Controller with LEDC)

you have higher level functions:

    ledcSetup(pwmChannel, frequency, resolution);
    ledcAttachPin(pwmPin, pwmChannel);
    ledcWrite(pwmChannel, duty);

Thanks for the reply.

Please correct me if I am wrong, but the function

ledcSetup(pwmChannel, frequency, resolution);

only allows the frequency to be set once.

Will I have to call this function repeatedly to change the frequency?

yes, change it when needed

1 Like
 // Send a GET request to <ESP_IP>/slider?value=<inputMessage>
  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    // GET input1 value on <ESP_IP>/slider?value=<inputMessage>
    if (request->hasParam(PARAM_INPUT)) {
      inputMessage = request->getParam(PARAM_INPUT)->value();
      sliderValue = inputMessage;
      ledcSetup(ledChannel, (sliderValue.toInt()), resolution);
      ledcWrite(ledChannel, 127);
    else {
      inputMessage = "No message sent";
    request->send(200, "text/plain", "OK");
  // Start server

Changing the frequency does not change the brightness of my connected LED.

Any other suggestions?

you need to attach the pin into the channel with ledcAttachPin()

the brightness is typically not done by changing the frequency, just change the duty cycle..

you could use this analogWrite() for that

1 Like

I'm using a Meanwell ELG-75-C500B, which needs a PWM Frequency of 100Hz - 3KHz to dim the connected LED's hence the reason why I need the frequency to change or am I misunderstanding the diagram from the datasheet above?

Just hooked it up to my Oscilloscope - can see the frequency changing.

I would understand it as ' you can use any frequency between 100Hz and 3KHz'. But a PWM signal defines the brightness of the LED by its duty cycle, not by the frequency.

1 Like

PWM, Pulse Width Modulation.

A pulse of 10 Hz can have a duration of, say 50% of the duty cycle or 1% of the duty cycle. The longer time the pulse is high represents a longer on time and a higher brightness.

The ESP32 LEDC API is a very good API to use for LED's. Motors are better suited to the ESP32's MCPWM.

A link the the ESp3e2 API

1 Like

Thanks all for the replies.

same here.

1 Like

This driver LED allows any frequency between 100Hz and 3KHz.
But the adjustment is based on the duty cycle, as shown in the graph on the right.

RV mineirin

1 Like

Thank you very much.

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