PWM & audio (tone or .wav) has noise?

Hey guys..

I am curious as to the nature/relationship between PWM and audio output.

I have played around with several projects/circuits.. and have tried to do simple PWM on ANY PWM enabled pins..and can always 'hear' it through the speaker over the music..

I have played with:

Arduino & Waveshield.. trying to PWM leds..and can 'hear' the PWM through the speaker..matching the led fade..etc

PWM output (using the SimpleSDAudio lib).. and when I also try to PWM a few leds while playing a quick audio clip.. you can hear the PWM fade as noise over the clip/speaker..

Tone output using a small amp to boot the volume a bit... fade an array of leds through a transistor..(powered from separate pack, and GNDS tied together..etc)....and again, you can HEAR the pwm/fade.. when it stops the buzz/noise/static over the clip stops as well..

so Im trying to get a base/foundation of why this stuff happens..

(hopefully not a bunch of equations and links to filters/calculators...etc, looking for a more general understanding to build from) why Im posting hte gneral section.. mot audio.. to keep in the beginner arena. :slight_smile:

obviously some kind of noise due to the PWM'ing...

but how does one go about fixing this?
is this where isolation/opto-isolators come in? (which is all new to me still)

I have a project that i want to get back to soon..

more or less Arduino + Adafruit Waveshield.. but I need to make a custom PCB for some MAX7221 chips to contol a MATRIX of leds (2 x MAX chips, 60 leds on each chip).. so a voltage regulator, iSet resistor for the current..... some pads for the battery pack and Arduino 'control' line/pin...etc..

so I wanted to talk it out in hopes of learning what I can do to ensure better audio output..


The Offspring have an appropriate song: Come Out and Play with an emphasis on the subtitle: Keep 'em Separated. Keep digital lines (i.e. wires / traces carrying PWM signals) physically separated from analog lines (i.e. wires / traces carrying sound signals).

Every integrated circuit gets at least one capacitor: De-coupling

Humans Hearing range:20 Hz to 20 kHz
Dogs Hearing range: 40 Hz to 60 kHz

make PWM frequency higher than that will do the trick.

When you use pwm to dim led ;-


High frequency pwm (500 KHz) is used to control the current and keep it constant (300 mA).
Low frequency dimming (400 Hz, implemented with override feature, and high enough to eliminate blink) controls the light intensity, We use 12.5 Hz which will be blink. That is what we want.


Low frequency dimming might be fixed to low frequency, follow Coding Badly's recommendation.

is this where isolation/opto-isolators come in?

Yes it will help, use linear opto-isolators for audio and opto-isolators for pwm. plus use isolated power supply independent power linear/audio and logic/pwm cuicirt if you could.


not one email notification... out of curiosity I searched out the thread..

@CodingBadly -

I guess Im not fully clear.. :slight_smile:

arent the lines 'separated' already? from pin on chip to I/O pad... ?

lets use the Adafruit Waveshield for examples.. just a stacked shield..

what could be 'connecting' the lines like you are referring to?

(the comment you made seems pretty straight forward... I must be missing something?)

or another example..

I have a barebones/breadboard Arduino set-up in front of me..
328P-PU (DIP)
set to run @ +3.3v @8MHz internal clock
SD card connected.
no vReg (runs of direct +3.7v li-po battery)

(has resistor & cap for RESET & DTR break-out)
(coupling cap on the battery 'input')

this set-up is running/using the SimpleSDAudio library.. (so there is a DC blocking cap on D10 of audio output pin as well)

the only other components on this test set-up is
some leds: 5 x green, 3 x red (each with their own resistor of course) :slight_smile:

a switch.

I get the same type of result from this set up that I do from the Adafruit Waveshield, Custom Arduino/Waveshield merged boards, Tone gen, that I am getting with this SimpleSDAudio lib tests..

whenever I add in some sort of PWM.. I can always 'hear it' through the speaker..

on most of the other projects I have coupling caps all over on most of the power vregs..etc..


thanks for the post.. its a bit over my head at this point in time though. :frowning: Im not even sure how I would go about even messing with type of stuff?

using someones else's library for the audio..

and a simple loop/analogWrite() to fade out some leds..

on other examples.. Im using a MAX chip.. (doesnt even support PWM, but has a 'brightness' option of 15 steps of something.. and you can hear the 'stepping/change' through the speaker too)..

figured I'd start the process of trying to understand this better..(baby steps!) was hoping maybe some BOB in between board & speaker or something?

Alternately.. heres a 3rd project.. that is TONE gen based.. w/amp..etc

on top side.. there are some 5050 leds..

I play tone through speaker.. and try to PWM the 5050 leds (through transistor)... again.. you can HEAR it being PWM'd?


Plan A;-

Noise cancellation, basic pick up out of phase PWM signal feed into Waveshield to cancel PWM. YMMV

Plan B;-

Move Adafruit Waveshield into second Arduino, and power second Arduino with second power supply if you could. connect Arduinos with opto-isolators.

Plan C;-

Use linear LED driver to dim the LED, now frequency is 0 Hz (DC) and out of hearing range.

The 3 key methods;

  • cancellation.
  • isolate, isolate input or power supply.
  • make frequency out of range, either lower or higher than 20 Hz to 20 kHz.

whenever I add in some sort of PWM.. I can always 'hear it' through the speaker..

That is because PWM is a rapid digital signal with a vertical edges. This contains lots of harmonics that radiate. This is being picked up by your audio amplifier and so you can hear it.

There are three things you can do,

  1. reduce the intensity of the source of interference
  2. reduce the sensitivity of the amplifier to pickup
  3. reduce the coupling of the two signals.

for 1) you could add very small capacitors to the signal to slow down the edges, and increase decoupling on the LED supply.
for 2) more decoupling on the supply, and a small capacitor across the input but that will reduce the frequency response of the amplifier.
for 3) as mentioned separate as much as possible and possibly run the amplifier of a separate supply or a well decoupled one.

@sonnyyu - let's try and be practical with any advice given theirs a good chap.

for 1) you could add very small capacitors to the signal to slow down the edges, and increase decoupling on the LED supply.

1 kHz PWM, the base frequency is 1 kHz, then 2nd harmonic is 2 kHz ... 10th harmonic is 10 kHz...20th harmonic is 20 kHz. they are all in humans hearing range ! add very small capacitors will be no help.

and a small capacitor across the input but that will reduce the frequency response of the amplifier

a small capacitor across the input is high pass filter, we have a lot nth harmonic. No work.

diagram of noise cancellation (Plan A)

Plan D;-

make sure use PWM above 20 kHz.

Diagram for Plan C


2. use JFET current sink.

change Q1 and Q2 for support high current, and put Q2 on heatsink. connect Vin to output of Arduino DAC.

TI App note