K
K
Kirill2019-11-05 18:01:15
Arduino
Kirill, 2019-11-05 18:01:15

How to beat PWM hum on ESP?

Hey! NodeMCU is connected to PWM with LED strip, PSU with a margin, nothing gets hot but buzzes when PWM != 0 || != 100
Googled and it seems like they say that it helps to increase the frequency on arduino, but how to do it for ESP? google doesn't say :(

#define TABLE_LIGHT 12
pinMode (TABLE_LIGHT, OUTPUT);

from MQTT comes as a percentage 0-100%
String strPayload = String((char*)payload);  //считываем значение топика
    int val = strPayload.toInt(); //конвертируем для шим
    int stled = map(val, 0, 100, 0, 1024); //приводим значение 0-100 в значение 1024-0
    set_pwm_smooth(stled);

and then implemented like this:
void set_pwm_smooth(int new_pwm) {
    static const int pwm_step = 1; // шаг изменения ШИМ
    static const int pwm_step_time = 3; // время одного шага изменения ШИМ
    static int stled;
    int pwm = stled;
      while (pwm != new_pwm) {
          int next_pwm = pwm + (pwm < new_pwm ? pwm_step : -pwm_step);
          if ((pwm < new_pwm && next_pwm > new_pwm) ||
              (pwm > new_pwm && next_pwm < new_pwm))
              pwm = new_pwm;
          else
          pwm = next_pwm;
          analogWrite(TABLE_LIGHT, pwm);
          delay(pwm_step_time);
      }
      stled = new_pwm;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Grigory Boev, 2019-11-05
@ProgrammerForever

1) Decrease pwm_step_time;
2) Try to put the LC filter on the output of the block. Capacitor (Electrolyte+ceramic in parallel) in parallel with the supply, and inductor in series. Check all winding parts in the PSU - chokes and transformers. Coat with varnish if necessary.

K
Kirill, 2019-12-03
@KirillSPB777

analogWriteFreq(150); no more hum

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question