Professional Documents
Culture Documents
Tweet
So far in this series weve had a diverse look at how Arduino can interact with a range of real-world devices from servo motors to ultrasonic range finders TVs to
humidity sensors. Now well see if we could get the Arduino to make a few sounds. Well actually do a bit better than that well look at the importance of pulse width
modulation (PWM) to microcontrollers by building our own digital audio player called Auduino.
The two versions of our Auduino audio player. The Ethernet shield version (bottom) gives better results with more flash cards.
How it works
The ability to turn digital data 1s and 0s into a proportional analogue voltage is a fundamental tenement of digital audio and its where the digital-to-analogue converter (DAC) gets its
most celebrated use. Audio DACs appear in every smartphone tablet MP3 player PC and laptop. Theyre everywhere. DACs are also used to control motors and sensors LEDs and more
anywhere where you need to turn a digital number into a real-world analogue voltage. One of the simplest ways to do it is what makes microcontrollers so popular its called Pulse Width
Modulation (PWM).
The problem for all microcontrollers is that their standard digital outputs only have two positions: high and low. The output will either be at the supply rail (Arduinos 5V) or ground (0V);
theres no in between. This is where PWM comes in. It starts with a clock signal output that runs at a much higher rate than we need; in our case beyond the 20kHz audio spectrum. That
http://apcmag.com/arduino-project-5-digital-audio-player.htm[31/03/2014 11:10:20 AM]
clock signal is a squarewave meaning it spends half the time at digital-1 and half at digital-0 in each clock cycle. In geek speak we say it has a duty cycle of 50%.
What PWM allows us to do is modulate that duty cycle so that when its pulse waveform output is fed and filtered through a particular device the waveform is averaged and becomes an
analogue voltage thats proportional to the duty cycle. So if the waveform signal varies between 0 and 5V and has a 10% duty cycle itll have an average analogue voltage of 0.5V. At 50%
duty cycle itll be 2.5V; at 90% itll be 4.5V.
This is how the pulsating LED on your iPhone or iPad works. The LED will be fed with a PWM waveform that varies between 0 and 100% duty cycle at a high enough frequency so you
cant see the LED flashing.
Your typical music file is the result of the reverse process: turning a complex analogue voltage into a digital representation. Audio CDs capture two channels (stereo) at a 44.1kHz sample
rate with 16-bit resolution. What that means is there are 216 or 65536 distinct voltage levels that can be captured every 22.6 milliseconds or so.
Its called Pulse-Coded Modulation (PCM) and its the basic format used by Windows WAV file format. Look at a WAV file and youll see a long series of 16-bit numbers that represents
the audio (one per sample and one per channel). Turning those digital samples back into an analogue voltage is what every audio player does.
One way of doing this happens to be PWM. Remember how we said before that a PWM signal is modulated to create a variable duty cycle waveform? Guess what we use to modulate the
clock signal with? The digital audio samples from our WAV file. So what we end up with is a PWM waveform output whose duty cycle is proportional to the PCM audio data in the WAV file.
We feed the output into an audio amplifier and hey presto! We hear that digital audio WAV file coming through the speakers.
PWM can create an analogue voltage thats proportional to its duty cycle.
Arduino limitations
While we love Arduino this is where we run into its limitations it only has an 8-bit bus and combined with its limited 16MHz clock speed and our audio code library we only have enough
speed to handle WAV files with a 19kHz sample rate and 8-bit depth. That said Ive worked on our simple audio converter to tweak the encoding so it produces good-sounding files with a
better than AM radio frequency bandwidth which is good enough for music and certainly more than enough for digital audiobooks.
01 Download Auduino
Download the auduino.zip file from apcmag.com/arduino.htm and unzip it to the C:Auduino folder on your C: drive. Launch the InstallWavConverter.reg file. This installs a Shell registry
key that adds a new menu entry to Windows Explorers context menu. (Open the REG file in Notepad and you can see how it works.) Click the Yes button to confirm and then OK when
its completed (RemoveWavConverter.reg deletes the registry key if you wish).
Were building our prototype this month on the same breadboard we used for our LED dice and weather station projects as it makes it easier to troubleshoot and program the Arduino. You
can see how it goes together from the circuit diagram the overlay diagram and the photos. The SD card module has two rows of pins and each pair is wired together so it can plug straight
into the breadboard.
The layout diagram of our digital audio player using an 840-point breadboard.
Choosing SD cards
The SD card module talks to the Arduino over the SPI (Serial Peripheral Interconnect) bus but SPI is really just a 1-bit economy mode interface thats easy to implement whereas SD cards
normally communicate over a 4-bit wide bus. Combine our low-level translation circuitry with the fact that not all SD cards do SPI well and that unfortunately limits the cards we can use.
We recommend Class 2 or Class 4 SD cards that are preferably 8GB or smaller. You can try larger Class 4 cards (weve used a 32GB card successfully) although youll need to make
some changes to our Arduino code: change the cardType variable from oldCard to newCard . Basically the newer or larger the card the slower we need to run the SPI bus so doing this
drops it back from 8MHz (half-speed) to 4MHz (quarter-speed). Any slower than 4MHz however and the data rate becomes too slow and you can hear the continual jitter in the sound
output. We purchased new 4GB and 8GB Class 4 Verbatim cards. The 4GB card didnt work but the 8GB card worked perfectly at the 8MHz/half-speed rate.
Our modified SD library still works with all existing Arduino programs (sketches) but adds a new instruction that allows us to adjust the Arduinos SPI speed and allow more cards to work.
Alternative option
http://apcmag.com/arduino-project-5-digital-audio-player.htm[31/03/2014 11:10:20 AM]
Weve also come up with a more reliable option which involves ditching the $1.50 SD card reader and replacing it with a $10 Ethernet Shield that includes a microSD card slot. You remove
the components connected to pins 10 11 12 13 and change the sketch by setting the chipSelect variable to 4 (this is marked in the sketch code). You also need to set the cardType
variable to oldCard . The Ethernet Shield has the proper level translation built in to reliably work with any microSD card. The Shield sits on top of the Arduino and you use the same I/O
pins for the switch and audio out.
Software libraries
Well need the tmrpcm and a modified SD card library. Youll find these in the zip file you can download from apcmag.com/arduino.htm (look under the Project 5 heading). Copy the SD
and tmrpcm folders to the libraries folder in your Arduino IDE folder (yes overwrite the original SD library) and restart the IDE if youve had it running already.
The apc_05_audioplayer.ino file is our sketch which ties everything together. It boots up the Arduino turns on the SD module checks the card and waits for you to press the button. Press
it for less than a second and itll load and play the first file. If you press the button again for less than a second itll pause playback; pressing it again resumes playback. Press it for more
than a second and itll automatically skip forward to the next audio track and begin playback. There are no volume controls you adjust this at your amplified speakers.
Unfortunately the tmrpcm library creates a pop whenever a track stops or starts. A low-pass filter will help a little but its caused by the PWM signal turning on and off.
Parts List
The specific parts for this project should cost you less than $40 if you choose the SD card module option. The hardest thing to find will be the SD card module itself we bought ours from
eBay for just US$1.48 (including shipping) from Hong Kong. The rest of the parts can be picked up from your local Jaycar Electronics or most online electronics stores.
Give it a crack
A digital audio player we can control offers plenty of ideas. Just a few I can think of off the top of my head include a talking robot a voice-warning security system a talking weather station
and Im sure you can come up with a few of your own. By building this project youll learn more about digital audio and even be able to incorporate some of the techniques into your own
Arduino projects.
Cost per
unit
Source
Arduino Uno R3
US$14
eBay
US$1.48
eBay
Push-button momentary
switch
$1.25
Jaycar
$0.40
Jaycar
$0.25
Jaycar
100uF/16VW electrolytic
capacitor
$0.36
Jaycar
$0.40
Jaycar
$0.35
Jaycar
1k/0.25W metal-film
resistor
$0.46
Jaycar
4.7k/0.25W metal-film
resistor
$0.46
Jaycar
10k/0.25W metal-film
resistor
$0.46
Jaycar
47k/0.25W metal-film
resistor
$0.46
Jaycar
$6.75
$1.35
Jaycar
$4.95
Jaycar
Dupont wires
US$3.66
eBay
US$9.62
eBay
Tom Steyer
hi, thanks for this compete article, your solution is a good option to listen music, i have a question about the voltage for SDHC card module, you use 5v, why you dont use
directly 3,3v? i have the same module and i use 3,3v to make it works. thanks
http://darrenyates.com.au Darren Yates
Hi Tom,
The card module has a 3.3V voltage regulator on board, so the card runs at 3.3V. However, the Arduino, running out 5V, has its outputs hitting that 5V mark. Feeding
5V outputs into a 3.3V-expecting input on the SDHC card normally doesnt do the card any good.
Cheers,
Darren.
Search Site
Search
Follow us
Recent Comments
Darren Yates on Arduino Masterclass
Part 4: Build a mini robot
Darren Yates on Arduino Project 5:
Digital audio player
HarryDGrif on Best iPhone and iPad
apps, February 2014
SicaBixby on The best PC gaming
accessories: 8 gaming keyboards
reviewed and rated
Raquib Buksh on Arduino Masterclass
Part 4: Build a mini robot
Magazine Resource
APC Tutorial Files
Arduino Source Codes
Tweets by @APCmag
Vote
View Results Polldaddy.com
Top
Future is AOP & PPA Digital Publisher of the Year and BMA Media Company of the Year.
This siteis part of Future plc, an international media group and leading digital publisher. We produce content across five core areas:
About Future
Jobs
PR
Advertising
Digital Future
Privacy Policy
Cookies Policy
Subscriptions
Investor Relations
Contact Future
Future Publishing (Overseas) Limited, Beauford Court, 30 Monmouth Street, Bath BA1 2BW. All rights reserved. England and Wales company registration number 06202940. Australian Office: Suite 3, Level 10, 100 Walker Street, North Sydney, NSW
2060 T: +61 2 9955 2677