Page 1 of 1

PWM for low-cost ClassD audio amplifier

Posted: Sat May 11, 2024 2:23 pm
by dj.petr
Hi, everyone!
I´m new in ESP and I need to control duty of PWM by analogue signal connected to the internal ADC. I have this code:

Code: Select all

const int analogInputPin = 36; 
const int pwmOutputPin = 2;   

void setup() {

  pinMode(analogInputPin, INPUT);
  ledcSetup(0, 78000, 10); // freq of PWM 78kHz, 10bit
  ledcAttachPin(pwmOutputPin, 0); 
}

void loop() {
  int analogValue = analogRead(analogInputPin);
  ledcWrite(0, analogValue);
}
I watch the output of ClassD amplifier on a scope and theres a problem with aliasing on sinewave frequencies higher than cca 1kHz (input is sinewave with amplitude 1-4V with ofset voltage). So i have question, how to solve this, how to optimize code to run on a higher frequencies than 1kHz..

Thanks! ;-)

Re: PWM for low-cost ClassD audio amplifier

Posted: Sun May 12, 2024 1:57 pm
by liaifat85
Implement an anti-aliasing filter on the PWM output signal to remove high-frequency components before it reaches the amplifier. This can help in reducing aliasing effects in the output signal.