Simple MP3 Player

The goal of this project is simple:

  • Play MP3 songs, from a micro-SD card, randomly, forever.

Hardware

This project requires three pieces of hardware:

Assembly is trivial:

  • Plug the two boards together.
    • They only fit one way :)
  • Insert your headphones.
  • Insert your pre-prepared SD-card.

Completed Project

SD Card Setup

You'll need to format your card carefully, such that it can be read by the onboard card-reader. In short this means you must use a RAW block-device, not a partitioned one, and you must format it as FAT16, or FAT32. (Because of that you will also be restricted to DOS-style 8.3 filenames.)

In my case I ran this:

root@deagol:~# mkdosfs -F16 /dev/sdX -I
mkfs.fat 3.0.27 (2014-11-12)
root@deagol:~# mount /dev/sdX /mnt
root@deagol:~# cp /tmp/*.mp3 /mnt
root@deagol:~# ls /mnt/*.mp3
/mnt/track001.mp3  /mnt/track002.mp3  /mnt/track003.mp3
root@deagol:~# umount /mnt

You'll notice I explicitly didn't create a partition. I just used the raw block-device.

A lot of the existing MP3-player projects will tell you to use filenames of the form trackNNN.mp3, and warn you that only 999 files are supported. That's because the API for playback allows you to write the following to play the file named track017.mp3:

blah.playTrack(17);

In this project we read the filenames from the card, and play then regardless of what they are - via a different API function (blah.playMP3(filename);). So long as you avoid UTF-8, and use filenames that honour the 8.3 format you're fine.

Software Setup

The code for this project has pretty much already been written for us! What we need to do is clone the appropriate library to our local Arduino development-host, then we can merely load the examples for the functionality we need.

Clone the repository:

deagol ~ $ git clone https://github.com/mpflaga/Sparkfun-MP3-Player-Shield-Arduino-Library.git
Cloning into 'Sparkfun-MP3-Player-Shield-Arduino-Library'...
remote: Counting objects: 3303, done.
remote: Total 3303 (delta 0), reused 0 (delta 0), pack-reused 3303
Receiving objects: 100% (3303/3303), 3.13 MiB | 1.26 MiB/s, done.
Resolving deltas: 100% (2158/2158), done.
Checking connectivity... done.
deagol ~ $

Now we need to move the libraries into the correct location:

deagol ~ $ mkdir -p Arduino/libraries/ || true
deagol ~ $ mv Sparkfun-MP3-Player-Shield-Arduino-Library/SdFat/ ~/Arduino/libraries/
deagol ~ $ mv Sparkfun-MP3-Player-Shield-Arduino-Library/SFEMP3Shield/ ~/Arduino/libraries/

Once that is done you can open your Arduino development-environment and load the examples. There are two sets of examples - one for the SD card-reader, and one for the player. I'd suggest starting with the former.

Click on the Menu-item "File | Examples | SdFat | SdInfo". This will load a program which will dump details about your SD-card. Compile it and upload to your board, then open the serial-console.

Once you've entered a dummy-keystroke you'll see output like this:

init time: 14 ms

Card type: SDHC

Manufacturer ID: 0X3
OEM ID: SD
Product: SU04G
Version: 8.0
Serial number: 271253504
Manufacturing date: 9/2010

cardSize: 3965.19 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true

SD Partition Table
part,boot,type,start,length
1,0X0,0X0,0,0
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT16
blocksPerCluster: 128
clusterCount: 60499
freeClusters: 60340
freeSpace: 3954.44 MB (MB = 1,000,000 bytes)
fatStartBlock: 1
fatCount: 2
blocksPerFat: 256
rootDirStart: 513
dataStartBlock: 513
Data area is not aligned on flash erase boundaries!
Download and use formatter from www.sdcard.org/consumer!

To do something more useful, playing files, load another example. This time we choose "File | Examples | SFEMP3Shield | Examples | MP3Shield_Library_Demo.ino". Once loaded change the serial speed from:

Serial.begin(115200);

To:

Serial.begin(9600);

At that point you can see a menu on your serial console, which will let you control the audio.

Our Random Player

Taking code from the samples seen above I've put together the following application, which picks a random file from the SD-card, plays it, then repeats forever.

The complete code reads as follows:

Download the code.

Challenges?

  • Because randomness is non-perfect you might find track 6 plays 100 times in a row.
    • Keep track of the most recent track(s) and avoid repeating them immediately.
    • This will fail if you only have a small number of files though, because all tracks will be recent.
  • Wire up an IR-based controller.
  • Wire up an LCD display for showing the currently-playing song.