Tutorial: play simple Arduino audio samples


David Mellis from the Arduino crew presents this tutorial on playing short audio samples using only the Arduino and a simple speaker. Using the PCM Library for Arduino, this project takes a short (4 second) audio sample of your choosing which you encode on your PC into MP3 format (down sampled to 8 KHz, 8-bit mono sound), then convert to a series of numbers that can be pasted into your Arduino program. (This conversion is accomplished using the EncodeAudio application linked in the tutorial.) Once you paste your converted MP3 numbers into your Arduino sketch, you simply upload to the board to play the sound on command. This technique could have interesting prank potential when paired with a sensor and some triggering code.

You can download the PCM Library as well as the source code for this project from David’s Github page.

Join the Conversation

20 Comments

  1. This is not playing MP3 audio, just WAV data. It’s unsigned 8 bit PCM encoded samples. I did the same thing with the Arduino_light library for the Propeller.

  2. /me never seen a point of getting anything more then a beeeep wrt sound from a mcu … if you want to play music, get some codec chip to play it, downsampling and making dac with pwm or even worse 2 bit dac with 2 pins and a resistor .. it sounds like crap, what’s the point…

    1. Markus did a nice implementation of music with PIC32 and I thought the result was great – I especially like the simplicity of the DIP circuit :D

  3. WONDERFUL! This is great. I do not understand the negativity left by people that do not have the creativity to use your idea. I plan on trying to use it for simply providing a visual and audible ID periodically. I do not expect to listen to the New World Symphony on it but if it can play a two-syllable word without me having to add a shield, it will be excellent. Thanks for your efforts.
    JJ

  4. Great but I have some problems….. When i push verify it give me some errors compiling…

    PCM\PCM.c.o: In function `stopPlayback’:
    C:\Users\Roberto\Desktop\arduino-1.0.3\libraries\PCM/PCM.c:148: multiple definition of `stopPlayback’
    PCM.c.o:C:\Users\Roberto\AppData\Local\Temp\build3890887407977407011.tmp/PCM.c:148: first defined here
    PCM\PCM.c.o:(.data.speakerPin+0x0): multiple definition of `speakerPin’
    PCM.c.o:(.data.speakerPin+0x0): first defined here
    PCM\PCM.c.o: In function `__vector_11′:
    C:\Users\Roberto\Desktop\arduino-1.0.3\libraries\PCM/PCM.c:74: multiple definition of `__vector_11′
    PCM.c.o:C:\Users\Roberto\AppData\Local\Temp\build3890887407977407011.tmp/PCM.c:74: first defined here
    PCM\PCM.c.o: In function `stopPlayback’:
    C:\Users\Roberto\Desktop\arduino-1.0.3\libraries\PCM/PCM.c:148: multiple definition of `sounddata_length’
    PCM.c.o:C:\Users\Roberto\AppData\Local\Temp\build3890887407977407011.tmp/PCM.c:148: first defined here
    PCM\PCM.c.o: In function `stopPlayback’:
    C:\Users\Roberto\Desktop\arduino-1.0.3\libraries\PCM/PCM.c:148: multiple definition of `sounddata_data’
    PCM.c.o:C:\Users\Roberto\AppData\Local\Temp\build3890887407977407011.tmp/PCM.c:148: first defined here
    PCM\PCM.c.o: In function `startPlayback’:
    C:\Users\Roberto\Desktop\arduino-1.0.3\libraries\PCM/PCM.c:93: multiple definition of `startPlayback’
    PCM.c.o:C:\Users\Roberto\AppData\Local\Temp\build3890887407977407011.tmp/PCM.c:93: first defined here

    how can i solve it????

    1. In order to make this code work on the MEGA you will have to change the int speakerPin =11 to int speakerPin =10 and connect speaker to Pin 10 instead of Pin 11.

      The pin mapping is not the same for UNO/MEGA, etc.

  5. David, great project and runs fine on a Uno. Folks with poor sound quality need to make sure they follow the .wav file conditioning described in the tutorial. That said, could you comment on what would have to be done to get the audio output off of pin 11. I’d like to use your approach in an project that also uses the SPI interface.

  6. The link to the tutorial is broken.
    Does anyone know where i can find the “EncodeAudio” spoken of?

  7. In order to make this code work on the MEGA you will have to change the int speakerPin =11 to int speakerPin =10 and connect speaker to Pin 10 instead of Pin 11.

    The pin mapping is not the same for UNO/MEGA, etc.

  8. In order to make this code work on the Leonardo, change in the PCM.c to use the OCR0A,TCCR0,COM0A instead of OCR2A,TCCR2,COM2A respectively and remove the AASR setting.

  9. this libary does exactly, what I wanted to have :)
    Is it somehow possible to reduce the clack-noise, when it starts playing the sample?

  10. I would like to use this library for Arduino MegaCore (Atmega128). But there is an error. I’m rookie.
    Please help me; What exactly do I need to change in the library? Is there any way to write a full explanation? Which pin should I use?

Leave a comment

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.