Skip to content


PWM Sound Generation

SoundChipMichael Smith just posted up a great project where he uses PWM to play back sampled sound on the Arduino. His code is posted up at the Arduino Playground here.

Michael hijacked two timers, one for PWM generation and the other for the playback sample rate. His sample rate is 8KHz and the PWM runs at about 60KHz which is easy to filter. In fact, you don’t even need to filter it for most applications. I had no trouble loading up his project and playing the sound though my PC as described in a previous article here.

I plan to use Michael’s code my next article about Digital to Analog Converters (DACs) for audio. Michael uses the PWM function to act as a DAC for PCM audio and I can adapt his code to work with several types of DACs. The sound sample he uses is somewhat broad spectrum which makes it ideal for testing various DAC configurations and playback techniques. Great work Michael!

Posted in Arduino, Microcontroller, Projects.


5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Berni says

    I looked around microchips data sheets and seams there is no waiting for reading it just for writing.So i guess the limit for read speed is the serial bus clock i can be 1Mhz for I2C and 20Mhz for SPI so its relay fast.So real time recording is not possible but playback probably is.(with a dsPIC probably capable of 48Khz 16Bit)

  2. Steve says

    I don’t know if I can get 8kB/s into and out of the EEPROM (It’s some Microchip serial EEPROM I added to my sample order a few months ago to pad it out a little 😉 because I have to find it and look at the spec sheet still.

    If it’s not possible to stream the audio onto or off of the EEPROM in realtime, then I’ll look at copying it to program space and playing from there. I suspect that 8kB/sec isn’t prohibitively fast, however.

  3. Berni says

    A EEPROM is not a bad idea, but is it fast enough to play sound in real time? Can a small serial EEPROM do 8KB/s ?

    Well i have some 4MB RAM chips,but the thing is huge since its parallel and a backup battery inside the chip.But since its ram it can write or read in next to no time.

  4. Steve says

    With small mods you can compile the code for any ATmega168. I got it running and working on a mega168 I had wired up for other purposes, using a darlington pair on the output pin (PB3) to push my 5v source through an 8 ohm speaker.

    I’m going to try to expand the memory by adding a serial EEPROM to the board and seeing where that goes.

    Nice job!

  5. Berni says

    Nice one, on my PWM sound experiment i only played out sound that is real time generated in the MCU( Sweaped a sinus over various freqecnys)I never toght about playing a audio sample on that.That wouldn’t be hard to do anyway since i can just replace the sinus sample.