Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PWM and music playback

Not open for further replies.


Junior Member level 1
Jun 27, 2011
Reaction score
Trophy points
Activity points
I have the .wav samples (unsigned 8-bit PCM) of a music file that I want to play using the AVR microcontroller. I have enough flash to store these samples and would like to know if it would be easier to play it directly from the flash or should I store them on the RAM and then play the samples using PWM.

Also I would like to know how PWM works in such cases i.e. in playback of an audio signal. These samples are all 8-bit unsigned samples and the sampling frequency is 16kHz. How do these samples decide the duty cycle of the waveform i.e the pulse train of the PWM.

Your duty cycle register can be directly fed with the PCM bytes - assuming you are using an 8-bit pwm channel. Should be easy enough from flash itself - no need for RAM.

However the PWM frequency needs to be much higher than that of the audio content, which i suppose is < 8KHz. A 10x factor is OK. Higher is better. However with the standard 8-bit AVR's the fast PWM can only do around 78KHz @20Mhz. If you are using an AVR with internal PLL then you can go higher.

The trick is to change to the next PCM byte/ PWM duty cycle after exactly 62.5uS or at the same rate as the original sampling of 16KHz. This can be easily done by using a 2nd timer.

Your output needs to be LPF'd, and usually a simple RC is enough. Since the audio is < 8Khz, and PWM is ~78Khz this should be enough.
This output will be centered at approx Vcc/2 ~ 2.5v typically.

Which AVR are you planning on using ? Note that each 1 second of your wav file will occupy 16Kbytes, and your code needs to be there too !!

thanks for your valuable response, good sir!!! :)

Not open for further replies.

Part and Inventory Search

Welcome to