Está en la página 1de 6

22/9/2016

ArduinoTone&MP3SoundwithNoShield

Arduino Tone & MP3 Sound with No Shield


Published 7 months ago by Mate Marschalko
Arduinos are not designed to play sound on their own as its quite a dicult task for
that 16mhz microcontroller chip you have on an UNO to process and play sound at
64 128kbit per second rate which is the bit rate of a regular MP3 le.
However if you decrease the bit rate to only 8k and down sample the le then it is
possible with the help of only two digital output pins and a few resistors. As you will
see the sound volume will be quite low so we will build a simple amplier with an
NPN transistor.

Play sound
The rst thing we do is build the circuit that will be connected to your mini speaker.
For that you will need 6 resistors with the same amount of resistance and a few
jumper wires.

http://www.webondevices.com/arduinotonemp3soundwithnoshield/

1/6

22/9/2016

ArduinoTone&MP3SoundwithNoShield

Back

Blog Resources

Next thing to do is download and install the library called avr-sound created by
Muayyad Alsadi. Download the les from the repository and install it or manually
place it into your libraries folder.

Custom audio les


Adding your own audio les is not as simple as uploading an MP3 le. You will need
to down sample the le rst. Audacity is a free application that lets you do that.
After recording and opening your audo le in Audacity rst click Track / Stereo
Track to Mono. Next change the Project Rate to 8000hz at the bottom of the project
window. Finally go to File / Export Audio and select Other uncompressed les under
le format the hit the Options button. In here change the settings to RAW (headerless) and Unsigned 8-bit PCM then export. This has created a .raw le for you.
When you downloaded the avr-sound library it came with a python application called
snd2h.py. Copy your raw audio le into the same folder you have this python
application then go to your command line tool and run the following command to
convert your raw audio le into a .h le that you will then be able to use straight
away in the Arduino IDE:
http://www.webondevices.com/arduinotonemp3soundwithnoshield/

2/6

22/9/2016

ArduinoTone&MP3SoundwithNoShield

pythonsnd2.pymyaudiofile.raw

Back

Blog Resources

This has created a .h le for you which will need to be included in your new sketch
just like it is for the 3 examples that came with the library. Once you have done that
Download FREE Ebook: Introduction to JavaScript Electronics
add this piece of code to your main sketch:

#include
#includemyaudiofile.h"
voidsetup(){
avr_sound_init();
}
//thelooproutinerunsoverandoveragainforever:
voidloop(){
play_myaudiofile();
delay(500);
}

Final task left is to create the circuit.

Amplifying the sounds


Unfortunately the sounds wouldnt be loud enough from the output pins without an
external power source so we need to build a simple amplier to make it louder.
Heres the nal circuit including a R-2R resistor ladder to generate the sound and an
NPN resistor that switches power from an external battery:

http://www.webondevices.com/arduinotonemp3soundwithnoshield/

3/6

22/9/2016

ArduinoTone&MP3SoundwithNoShield

Back

Blog Resources

Download FREE Ebook: Introduction to JavaScript Electronics

I tried this with an external 3.7V lipo battery which worked perfectly.

Further improvements
Audio quality is obviously not amazing so I wouldnt use this for human voice or
music but might be perfect for simple sound eects, buzzes or tunes.
I unfortunately wanted this for human voice so I will have to keep looking. I
previously tried the WTV020SD-16P module which is really cheap on ebay but it was
an absolute pain to work with. It only accepted a specic SD card and by that I mean
a specic brand at a specic size and it was also quite picky about le compressions.
Even when it worked it randomly stopped and wouldnt play at all.
Im now looking at the new DF Player Mini. Its quite promising and still cheap so I
will order a few in, test and let you guys know how it goes.
Back to all

http://www.webondevices.com/arduinotonemp3soundwithnoshield/

4/6

22/9/2016

ArduinoTone&MP3SoundwithNoShield

Leave a Reply
You must be logged in to post a comment.

Back

Blog Resources

Download FREE Ebook: Introduction to JavaScript Electronics

Free Ebook
Step up your web developer career and learn hardware prototyping.
This ebook will get you started with JavaScript Arduino electronics development in a
couple of hours.
Email address

Send me the PDF

Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
Twitter

http://www.webondevices.com/arduinotonemp3soundwithnoshield/

Facebook

5/6

22/9/2016

ArduinoTone&MP3SoundwithNoShield

Back

Blog Resources

Download FREE Ebook: Introduction to JavaScript Electronics

Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.

All rights reserved | Contact at hello@webondevices.com

http://www.webondevices.com/arduinotonemp3soundwithnoshield/

6/6