How to Upload a Sound File Onto an Aduino
Playing WAV Files with a DAC
What are we making?
We're going to make a WAV player! (If y'all're not familiar, a WAV is an uncompressed audio file). Nosotros're going to build information technology with an Arduino and the AD5330 breakout board, an eight-bit DAC. WAV files are uncompressed and the Arduino doesn't have too much internal ROM and so we'll take to shop the WAV files on an SD card. This means we'll also add the microSD shield to the project.
A DAC is a Digital to Analog Converter. Basically a DAC takes a digital value (think 1's and 0'southward) supplied by a microcontroller and turns it into a corresponding analog voltage. An 8-bit DAC ways that the DAC has 8 pins on which to fix the digital value. Remember, an 8 scrap value can exist any number between 0 and 255. We will ability the DAC with iii.3 Volts. Past using an Arduino with the DAC we will exist able to create 256 distinct voltages between 0 and three.3V by providing 8 bits via digital output pins on the Arduino. By creating unlike voltages on a prepare frequency we can create sound!
In this tutorial we'll comprehend how to attach the microSD shield to the Arduino platform, and how to connect the AD5330 breakout board to the shield. Afterward connecting all of the pieces together we'll wait at some code to get WAV files playing from the SD carte du jour.
What do I need?
Hardware:
- Arduino (Duemilanove or Pro)
- AD5339 8-Bit DAC Breakout Lath
- microSD Shield
- microSD Bill of fare
- Jumper Wires (17)
- half-dozen Pivot Stackable Headers (2)
- 8 Pivot Stackable Headers (2)
- Breakaway Male Headers
- Speaker
- 10uF Capacitor
- Breadboard
Code:
- AD5330 Arduino Library with tutorial examples (GitHub)
Hook up the hardware!
Geez that's a whole lot of stuff we've got. Connecting all the pieces won't be very hard, but it will take some soldering! So what all practise we need to do? Well for starters the microSD shield needs to go on acme of the Arduino. First by soldering the half-dozen and 8 pin stackable headers onto the shield. Make certain that the male ends are pointing down away from the components on the shield.
SD Shield with Headers
Now the shield can exist placed on top of the Arduino.
SD Shield Installed on Arduino Duemilanove
Let'due south move on to the AD5330 breakout lath. In order to connect this to the shield and the speaker we'll need to place the breakout board onto a breadboard, just commencement we'll have to solder some headers onto the breakout lath. Solder breakaway male headers to the breakout lath; make sure the pins are facing abroad from the components on top of the board.
AD5330 Breakout with Male person Headers
At present you can put the AD5330 into the breadboard
AD5330 Breakout Installed in a Breadboard
Alright, with those ii things taken care of nosotros tin can become to wiring the project up. Things get a piddling messy hither so use care while wiring all of these signals. You'll demand 17 jumper wires to wire everything from the microSD shield to the AD5330 breakout board. The tabular array beneath shows the pins that need to be connected. For each connexion plug a jumper wire from the female header on the microSD shield to row on the breadboard that aligns with the specified pivot name.
microSD Shield Pin Number | AD5330 Pivot Proper noun |
Rx | DB0 |
Tx | DB1 |
2 | DB2 |
3 | DB3 |
4 | DB4 |
5 | DB5 |
half dozen | DB6 |
7 | DB7 |
ix | BUF |
10 | PD |
An. In 0 | Gain |
An. In 1 | CLR |
An. In 2 | LDAC |
An. In 3 | WR |
An. In 4 | CS |
+3.3V | VCC |
GND | GND |
Double check all of your wiring. With this many wires information technology's very like shooting fish in a barrel to misplace one or two of them; if that happens the whole excursion may not work once yous upload some lawmaking. Debugging the unabridged project and so becomes an do in frustration. Trust me, it's better to just double check the wiring now and brand sure you've wired the projection up properly. Now that the microSD shield is wired upwardly to the AD5330 breakout lath we're near finished. The whole indicate of the projection, though, is to play some music. The missing slice is glaringly obvious; nosotros still need to throw a speaker on at that place! You might be tempted to merely attach the speaker to the VOUT pin of the AD5330, but unfortunately that won't cut it. In order to create sound the voltage must be Ac coupled. It may sound complicated, merely really all it means is that we demand to put a capacitor between the VOUT pivot and the speaker. Identify the positive atomic number 82 of a 10uF Capacitor in the VOUT row of the breadboard and put the other end of the capacitor onto the positive concluding of the speaker. Connect the negative terminal of the speaker to the GND pin of the AD5330 breakout lath. (If your speaker didn't come up with wires soldered onto the terminal you lot'll have to add them yourself.)
Entire Project Setup
Adding the Firmware
Now on to the fun stuff! Download the two zip files from the Code Downloads section at the pinnacle of the tutorial. The starting time cypher file contains an Arduino Library for the AD5330. Unzip this in the \hardware\libraries directory of your Arduino directory. The library contains two example sketches: a tone sketch and a 'WAV files from SD' sketch. The first sketch merely produces a single tone on the speaker while the second set will retrieve WAV files from the SD bill of fare and play them.
The 2d download, the C lawmaking, is actually the same program as the 2d Arduino sketch; however this lawmaking is written in manifestly old C. There are many things I love almost Arduino, but sometimes you simply need some extra speed and power and it'south hard to get around this in the Arduino IDE. Using C gives y'all some extra room to push information a flake faster. We'll talk about this some more than later.
Let'due south get started looking at the code. Open the first sketch, the AD5330_Tone sketch. Later on yous've unzipped the folder in the Arduino directory open up the Arduino IDE. Go to File->Examples->AD5330 and select AD5330_Tone. Cheque out the sketch a flake before you load it. It's simple enough to figure out without digging likewise much. After defining the pin locations nosotros set all of the pins on the Arduino as outputs. I used the datasheet to notice out how to configure all of the inputs to the AD5330. After setting the initial values of all of the pins we're off to the 'Loop' code. To create a tone we demand to send a sound wave to the AD5330. To send a sound wave to the AD5330 all nosotros need to practice is gear up a value on the DB0-DB7 pins, clock them into the module and delay, then set the DB0-DB7 pins to zero, clock the new data in and filibuster again. If this process is repeated over and over again the tone that'south generated volition be directly related to the filibuster. The shorter the delay fourth dimension, the higher the tone will exist; the longer the delay the lower the tone will be. Bank check out the graphic representation below of how a digital moving ridge is represented.
Digital Representation of a Sine Moving ridge
Fix the board to Arduino Duemilanove w/ 328 and compile the code. After yous load the code to your Arduino you should hear a sound coming out of the speaker. If you don't you lot need to go dorsum and double check all of your wiring. Play with the FREQ and VOL values to encounter how they affect the tone and book of the generated sound. If the code doesn't compile and gives you errors related to pre-existing files it may be because you already have a FAT library installed. If this is the case the Fatty library will have to exist temporarily removed while using this code.
That was neat, and produced a pretty nice sound, but we tin can exercise that with i pin very easily. I desire to play some music on this affair! I decided I wanted to play eight flake WAV files, pretty much because information technology would take the to the lowest degree amount of piece of work. WAV file are uncompressed then the data manipulation wouldn't be a business concern; and eight-bits, well, duh... For some reason the kickoff affair I idea of later hearing 8-scrap DAC was the theme song to Mario Bros. And then I got on the Internets and started searching for some Mario WAV files. Wasn't too hard. The first files I grabbed happened to accept a sampling rate of 22 kHz. Then with this in mind I'd settled several issues: the sample width and the sample rate. Our WAV actor is going to play 8 flake WAV files with a sampling rate of 22 kHz. If y'all put whatever other kind of formatted WAV file on this it may yet play, but information technology won't sound very good!
Open up the Arduino sketch from the AD5330 library named AD5330_SD_WAV_Playback. You'll come across that several files are now included so the sketch can utilize FAT16. Some of the code at the offset of the sketch will wait familiar; the Setup section of the code is really the same! But there's also some more advanced stuff going on. When playing sound files it's pretty of import that we update the data at the correct charge per unit; never faster or slower than our sample rate of 22 kHz. In club to do this an Interrupt Routine was created. The interrupt will end the 'Loop' code no matter what information technology'southward doing and update the data on the AD5330. This ways that nosotros can be sure that we're sending sound consistently at 22 kHz. The code for the interrupt routine is inserted betwixt the Setup and Loop sections of the code.
The 'Loop' department of the code is commented pretty well. Here's a basic run downwards of what's going on:
- Find a WAV file on the SD card.
- Create two buffers that will agree data
- While 'playing' the information from i buffer, read new data from the SD card into the other buffer
- When the starting time buffer is empty; swap them
- If a song is over look for another WAV file. If no more WAV files are found and so end running!
Sounds pretty piece of cake huh? It'south actually not that bad in one case you become used to using the FAT16 functions. By the way we didn't talk about why we actually need an SD card for this. Unfortunately the ATmega328 simply has 2 kB of EEPROM. This isn't nearly enough to concord a WAV file, so we added the SD card for some external retention.
One time you lot read through the sketch go ahead and compile it. Brand sure the Arduino Duemilanove w/ 328 is selected every bit your lath and so load information technology up. Format your SD bill of fare for FAT16, then load some 8 bit WAV files onto the card. Any audio file can be converted into an 8bit WAV file; at that place are some instructions at the terminate of this tutorial on how to catechumen an audio file using iTunes. Once you've got the WAV files onto the SD card put the SD menu into the shield. Reset the Arduino and the Arduino should start playing the files. If null happens there are a few things you need to bank check: first double bank check your wiring; then make sure you lot've loaded 8-flake WAV single channel 22kHz WAV files. Any other format could crusade the project to be upset :(.
After the initial "Wow that'southward cool!" wears off, the offset thing that will probably come to your mind is "huh, this sounds like crap." Well, aye, it does. There's a couple of things to consider:
- Oh come on, your using an Arduino, a $ii speaker and an viii bit DAC all wired up together on a breadboard. What did you look?
- Sometimes Arduino just doesn't cut it! Our interrupt routine isn't running fast enough. We tin can better this by migrating the code to C. Let'due south do that!
Look, I'k the first to admit that I'k an Arduino junkie. I dearest how like shooting fish in a barrel information technology is to set upwards, and that I know there'south nothing wrong with the hardware. I really similar how easy information technology is to get sensors up and running. But there are some shortfalls with the compilation process and I often observe that I have to move over to C in order to really maximize the ability of the ATmega328 microprocessor.
Download the second zero file to go the C code for this projection. The code is actually exactly the same, but the digitalWrite functions are replaced with direct annals access to make writing to the pins much faster. Ane of my favorite things about the Arduino platform is the serial bootloader. A non-and then-picayune known underground about the Arduino hardware is the regular hex files (perhaps compiled using WinAVR) can be uploaded using the serial bootloader. See this tutorial for more help on the subject. What this means is that I tin can write ameliorate, more efficient code in C but still accept advantage of the Arduino hardware platform. And that'south exactly what'southward been done in the second zip file.
Once you lot've downloaded the zip file with the C code feel gratis to check out the source. You lot'll find that it is strikingly familiar to the Arduino sketch that plays songs from the SD card. All that's left to practice is load the hex file onto the Arduino. You should hear the difference between the 'C' version and the 'Sketch' version of the code immediately. It'south still non loftier quality sound, but information technology's a much more than clear and crisp audio.
Creating 8-Bit 22kHz WAV files
Google '8-scrap WAV converter' and agglomeration of free software applications will evidence up right away. Y'all may even have your own program or script that volition make this conversion for you. Nevertheless since most people are at to the lowest degree familiar with iTunes we'll create a WAV file in that program that'due south compatible with our firmware.
I employ Windows so I repent to all you Mac and Linux users. Please help others with some recommendations inside the comments below the tutorial page. Showtime upwards the iTunes application; once information technology's opened upwards click on the Edit menu and select Preferences.
iTunes Preferences Window
In the 'General' Tab of the Preferences Window click on 'Import Settings.' Select WAV Encoder from the 'Import Using' drop-down menu and so select 'Custom...' from the 'Setting' drop-downwards carte du jour. In the WAV Encoder window, choose 22.050kHz Sample Rate, 8-fleck Sample Size and Mono Channels. Click OK in the WAV Encoder window, the Import Settings window and the Preferences window to save your settings
iTunes WAV Encoder Window
Now whenever you import a song to your iTunes Library information technology will be imported with these settings. You tin catch the imported file from the iTunes Library directory and put information technology on the SD bill of fare for your WAV player. Remember to change this setting back to the original format for your regularl music imports or you won't exist impressed with the audio quality!
Ane mode to double check the files attributes is to right click the WAV file in the directory and select 'properties.' In the summary tab all of the audio properties will exist listed, just double check to make sure that the sample rate is 22 kHz, the sample size is viii-bit, it'south a ane channel file, and the audio format is PCM. As long as your WAV file has these properties it tin can be loaded onto the SD card and played by the WAV thespian.
WAV Properties Window
And that'due south it! Hopefully yous learned a fleck from this tutorial. Feel gratuitous to annotate on any errors you find and mail whatever improvements you lot brand. Proficient luck!
-Ryan
Source: https://www.sparkfun.com/tutorials/160
0 Response to "How to Upload a Sound File Onto an Aduino"
Postar um comentário