This tutorial is written by Ng Yong Han.
Many years back, we listen to our favourite songs in a cassette tape, or with a radio (FM Stations), or a compact-disc (CD). In every household, there would be at least a radio with a cassette player and a CD player together. Nowadays, songs are mostly in compressed and digitized format, and mp3 is one of the most commonly used format available. Therefore, in a modern household there will be at least one portable mp3 player. Even cellphones, smartphones and Hi-Fi sets are now equipped with an mp3 playing function and an SD-card slot. Computers can also play mp3 files, but a program is needed to play these files.
This tutorial covers the interfacing of the Cytron MP3 Shield with the Arduino Due. The Arduino Due has a very huge program space and RAM, so it will give you a lot of opportunities to expand on your creations with the MP3 shield.
The first thing you would ask is, why an MP3 shield? Couldn’t a microcontroller process the data in the MP3 file? Actually, a very fast and a powerful microcontroller could process MP3 data, but it is not really practical. It requires a lot of processing power to uncompress the data and to pass the data through filters so to give a meaningful audible output. Therefore, a special chip is designed to handle the decoding, which is the MP3 decoder. Some System-on-Chips (SOC) in media players and such do incorporate an MP3 decoder for the convenience of software developers. That is why an MP3 decoder chip is used alongside with a microcontroller like PIC 16/18F series if you need to play MP3 files.
What do you need now are the Arduino Due, the MP3 Shield, an SD-Card formatted in FAT16/32, an AC-Adapter and a pair of speakers/headphones(See following Note 2). As usual, use jumper wires and connect Arduino Due‘s 20 and 21 to the MP3 Shield‘s A4 and A5 respectively.
In this tutorial, the MP3 decoder in the shield is a ST Electronics’ STA013 decoder chip. The chip will receive commands (like setting volume) from the microcontroller through I2C interface while receiving MP3 data through the SPI interface, and the decoded data is converted into sound by an external digital to analog converter (DAC).
Luckily, like the Microchip’s MDDFS, Arduino Due has libraries for performing file operations in SD-cards. A simple program flow of how the shield works with the Due is as follows:
1.) The SD card library is initialized and the presence of SD card is checked. The program will only run when the SD card is present with the correct file system (FAT16 or FAT32). If not, it will wait there until you insert a correct card and reset the system.
2.) The program checks for the presence of the STA013 chip by sending a command through the I2C interface and 0xAC is received. The program will not start if there is no 0xAC. Afterwards, STA013-specific configuration values are dumped from the Arduino Due’s program memory into the decoder through the same interface.
3.) STA013 receives commands from the system to start decoding and playing the samples from the file.
4.) An mp3 file is opened. 256 bytes from the file in the SD-card is copied into the RAM through the SPI ports, and then fed into the STA013 when the DREQ (data request) goes high. It means that the STA013 can accept data for processing. Sending the MP3 data into the STA013 requires the BIT_EN to be high from the Arduino, and when it is done, the BIT_EN must be low to allow operations on SD-Card since the SPI bus is shared between the SD-card and the data ports of STA013.
Figure: In “mp3shield.cpp”.
5.) When the STA013 no longers accept data for processing due to the internal buffer being full, the DREQ goes low. The DREQ returns to high position when the buffers in STA013 are available.
6.) When the 256 bytes in the RAM are all used up, the system refills the buffer into the RAM from the mp3 file and also advances the file pointer to 256 steps forward.
7.) The process 5 and 6 repeats until the file pointer reaches to the end of file. The system stopped playing the file and then returns to 4.
So in the example “mp3shieldplay.ino”, most of the functions in the file are self-explanatory. The (1) creates a new MP3Shield object with the pin 3 for SD-card’s Chip Select, and pin 2 for the Amplifier Shut Down.
Then at (2) you initialize the MP3 Shield. You can see the progress at the serial monitor (Ctrl+Shift+M).
The (3) sets the volume of the MP3 Shield. Warning: Do not set maximum value of 255 – it may cause damage to your hearing. Keep it at or below 220.
Finally at (4), you open the file “mp3file.mp3” and play it. It’s that simple. Of course, put an MP3 file of your choice into the SD card first and you can put other names beside “mp3file.mp3”.
That’s about playing an MP3 file! It is quick and simple as you do not need to worry about how the decoder works and such. After you know how to play a tune with the shield, why not experiment some more? Like adding a display, or put it into a nice wooden jukebox? The possibilities are really endless.
Note 1: If you need to upload the program into the Arduino Due, disconnect the AC Adapter first. Once it is being fully uploaded reconnect the AC Adapter as soon as the Due is started to prevent stuttering. The MP3 Shield cannot run optimally on USB power, therefore an external power source is needed.
Note 2: The click and pop sound may be irritating when the Shield’s amplifier is being turned on. If you find it uncomfortable, use the speakers instead.Warning: Do not set maximum value (255 being the maximum) for the Volume control – it may damage your hearing. Always keep them at or below 220 for safety.