Noise through a class D amplifier...

Hello everyone

Im using a class D amp from Sparkfun through which I want to play some audio files. I have a RC filter with 10kohms r and 30 nF cap. From the output of the amplifier I hear a very noisy sound. I hear the audio but with it is a lot of high frequency sound. How do I reduce the noise?

When I attach the speaker directly to pin 9 there is no noise but its just not loud enough..any suggestions please! Really need help here

When I attach the speaker directly to pin 9 there is no noise but its just not loud enough.

DON'T DO THAT! Anything lower than 125 Ohms will draw more than 40mA and can damage your Arduino. (And, the noise is probably there but too quiet to hear.) You can put a resistor in series with the speaker to make it safe (for the Arduino) but it will be even quieter.

Do you have an audio shield or an Arduino DUE, or something with a true analog output? If the sound is PWM (from a "regular" Arduino) it's going to be noisy.

If you want to test the amplifier, try it with another known-good audio source (your computer, or a CD/DVD player, etc.). Of course, disconnect the amp from the Arduino when you do that.

DVDdoug:
Do you have an audio shield or an Arduino DUE, or something with a true analog output? If the sound is PWM (from a "regular" Arduino) it's going to be noisy.

I have an Arduino UNO, so the output is PWM from pin 9. Why does PWM have a noisy output? Should I usee a DAC in this case then?

My purpose is to get clear and loud audio I won't use the amp if i dont need to...but im confused as to what the best way would be to get a clean and loud output..

It depends on what frequency your PWM is set to. The filter might be at too high a frequency or a simple RC filter may be insufficient.

The other thing it could be is your amplifier reacting badly to the high frequency content.

Grumpy_Mike:
It depends on what frequency your PWM is set to.

I havent changed the frequency so it should be the standard arduino 490Hz for PWM. My audio file is 16kHz sample rate. Do i take this into account for my filter calculations?

When I use a transister to amplify from pin 9 to speaker its very very little noise but it isnt loud. With the amp its louder but extremely noisy..how do i determine an optimal solution...should i move to lc or second order filters? I don't know a lot of the math behind those.

How can I make the output from the transistor louder? Supply more voltage to power it?

Why does PWM have a noisy output?

[u]PWM[/u] is not analog and the default PWM switching-frequency is smack in the middle of the audio band.

Also, at 8-bits (with PWM or regular PCM) you get audible quantization noise which is most noticeable at low levels (when only a few bits are being used for the audio).

Class D amplifiers actually use PWM, but at very high frequency (probably MHz, depending on the amplifier) and the pulse width is analog so there's no defined bit depth.

Should I use a DAC in this case then?

Probably. I'd probably use an audio shield (depending on your requirements). An audio shield has it's own sample-rate clock as well as a DAC which makes the Arduino code a lot simpler. And, frequently they have a memory slot for storing the audio.

When I use a transister to amplify from pin 9 to speaker its very very little noise but it isnt loud. With the amp its louder but extremely noisy..how do i determine an optimal solution..

The signal-to-noise ratio won't change with amplification... If you amplify the signal, you amplify the noise.

or second order filters?

If you filter-out 490Hz and above, the audio will be very muffled.

No your PWM is much higher than 490Hz otherwise it would not work. So give more sped tailed about what you are doing. Post your code and use code tags.

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10  //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>//  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch
char mychar;

void setup(){

  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  tmrpcm.play("chicken.wav"); 
}

void loop(){  

  if(Serial.available()){   
    mychar = Serial.read();

    if(mychar == 'a'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("A 1.wav");
    } else if(mychar == 'b'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("B.wav");
    }
  }

}

I'm using the tmrpcm library and literally just trying to play a wav file thru SD card reader. I havent adjusted the PWM frequency. Also for the amplifier chip, it says the input voltage range is 2.5-5.5v, when i feed it 5v it doesnt work at all but works with 3.3v but very noisy as I mentioned. Any guidance on why that could be when it clearly states voltage upto 5.5v is good?

I havent adjusted the PWM frequency.

No but the TMRpcm library has.

Have you normalised the waveform sample before you used it. As the PWM is only 8 bits you will rapidly loose quality if your sound sample doesn't cover the full available dynamic range.

Grumpy_Mike:
Have you normalised the waveform sample before you used it. As the PWM is only 8 bits you will rapidly loose quality if your sound sample doesn't cover the full available dynamic range.

I am not sure what normaliisng the waveform means. Could you explain please? Also the output PWM frequency of my class D amp is 250 kHz...what does the tmrPCM library change the arduino PWM frequnecy to? How do you know it changes it?

eengineer_8:
I am not sure what normaliisng the waveform means. Could you explain please?

Your audio files might be encoded to 16 bits per sample. Since PWM only can be controlled by unsigned 8-bit values, then those samples should be encoded likewise.

what does the tmrPCM library change the arduino PWM frequnecy to?

I don't know exactly how much, but it's boosted up to around 33 KHz for a Timer1-controlled pin (aka pin 9 and 10).

How do you know it changes it?

The library automatically does something like this: Arduino Playground - HomePage

I am not sure what normaliisng the waveform means. Could you explain please?

When you have a recorder sample the signal will vary between the maximum you recorded and the minimum. This depends on things like the volume of the sound and the gain in the system.
The maximum and minimum peak you recorded is not necessarily the maximum that your system can cope with so the sample is normalized. This involves scanning the whole sample and finding these values and then working out a multiplication factor that when applied to the recorded maximum will produce a maximum that your system is capable of producing. This multiplication factor is then applied to all samples so it covers all the dynamic range the system is capable of. This process is known as normalization and is normally done at the click of a mouse.
Doing this will tend to minimise any noise in the system.

In case your problem still persists, then try this:
https://developer.mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/

I used the first approach, a simple resistor (920 ohm) and a npn transistor (BC something) and it worked great on my 12 ohm speakers with 5V vcc and pwm as input generated from Arduino. Hope this helps.