Step 2: Storage With MicroSD Card

Most music players now have built-in flash memory. We are hobbyists who probably can't solder those chips. We want something simple to use. A MicroSD card is perfect, and it's easy to make your own MicroSD card socket that you can prototype with on a breadboard (see picture).

SD and MMC cards are easy to use because they provide a SPI (serial peripheral interface) interface that can be used to read and write data to and from the card. Please read the following resources to understand SPI and the SD card:

AT90USB1286 Datasheet section 17 http://www.atmel.com/dyn/products/product_card.asp?part_id=3874

If you don't read the above three links, you will not know what I'm talking about next.

In short, the SPI bus is a bus where you place data onto the data lines (MISO and MOSI) one bit at a time, and the bit is sampled on the edge of a clock signal.

Our microcontroller has a dedicated SPI peripheral. By examining the above links I provided, we know the following facts:

Our microcontroller is the "master" and the SD card is the "slave"
The SD card uses SPI mode 0 (CPHA=0, CPOL=0), this means the clock signal starts low and the data input samples data when the clock transition to high
The maximum clock speed of the SPI bus

From the above information, we are able to initialize the dedicated SPI peripheral within the microcontroller. Refer to section 17 of the AT90USB1286 datasheet.

If you didn't work out the obvious electrical connections you will need, here's an explaination:

MOSI (Master Out Slave In)
The master refers to the device that generates the clock (the microcontroller), the SD card is the slave. Data on this pin travels from the microcontroller to the SD card. Also known as "DI".
Connect the DI pin on the SD card to the microcontroller's MOSI pin

MISO (Master In Slave Out)
Data on this pin travels from the SD card to the microcontroller. Also known as "DO".
Connect the DO pin on the SD card to the microcontroller's MISO pin

Chip select, the SD card pays attention to the data traveling on the SPI bus when this pin is low, and ignores the data on the bus when this pin is high. This is also known as "SS".
The CS pin on the SD card can be connected to any free pin on the microcontroller

This is the serial clock pin,
Connect this pin on the SD card and microcontroller's SPI clock in (called SCK in the datasheet)

The next step will take you through a step-by-step that shows you the basics of communicating to a SD card. As preparation, if you do not already have a good MicroSD card holder, then take some male pin headers with 0.1" spacing, and solder it to a MicroSD card adapter, as shown in the pictures below. The steps to make this makeshift card holder is in my appendix.
<p>Alright I found my problem. It is with the hardware initialization. I can init the USB but the mcu stops when it goes to do the SDCardManager_Init();</p><p>Anyone else have this issue? Any idea what could be wrong? I have the SD card wired up according to the wiring picture above... Maybe im missing a library? but then i would expect a compile error and that is not the case...</p><p>Any ides out there? Thanks for your time.</p>
<p>Hi, </p><p>I'm also trying to do this project. Did you manage to make your sd card part working ?</p><p>Thanks in advance</p>
<p>Very cool project and pretty comprehensive instructable! Without being aware of yours, I made an alarm clock with custom sounds. It's not as advanced as yours but it has other functionality worth observing: <a href="https://www.instructables.com/id/Alarm-clock-with-Wiring-or-Arduino/">https://www.instructables.com/id/Alarm-clock-with-Wiring-or-Arduino/</a> </p>
<p>Very nice tutorial</p>
<p>@frank26080115</p><p>Hello Frank, great tutorial here. I am focusing on the usb mass storage device section. I have the hex file generated and loaded onto my teensy++. Now what? Should my computer automatically treat the sd card as a MSD? Or do I need a scuzzy program in order to talk and get data off of the SD card? Please clear up how your USB MSD code is operating and how the computer recognizes and talks with the Sd card. Thank you for any help.</p><p>PS if any one else understands what needs to happen here I would greatly appreciate your 2 cents :)</p>
<p>I tried to compile the decoder_demo file you got here unfortunately there errors and can't be compiled.I am using studio 6.2. Can you please help me.</p>
<p>This is a brilliant tutorial, very well done and very thorough. Thanks a ton for this !</p>
<p>@<a href="https://www.instructables.com/id/Music-Playing-Alarm-Clock/#CF73PTNGZ38JG6J" rel="nofollow">frank26080115</a></p><p>This project is AMAZING! Thank you for the documentation. It is inspiring. You are the only person that gives me hope that I could have the SD card in my ATmega32U4 based Arduino exposed as a Mass Storage Device. I'm a total beginner at hardware and any help you could offer would be GREATLY appreciated, praised, and credited.</p><p>What can you suggest I do to get your LUFA MSD into an ino? Unfortunately I'm pretty tightly bound to the IDE as I am not a C[++] programmer. (Quite a pain since I do all my coding in VIM!)</p>
<p>Hi Frank</p><p>I've been through your whole tutorial and have a working music playing clock. I would just like to say thank you for making this guide I really appreciate the time you've put into this and I have learned so much. Once again thanks and good luck for the future!</p>
THANK YOU SO MUCH!! <br>I have been trying to understand the technique of writing and reading, and the address of the RTC chip and so much, just now I got it. <br>Great work :) <br>Really, thank you a million :) <br>Greetings and keep the good work to make the planet better :)
what's the highest bitrate mp3 you've been able to smoothly play on this?
I think it can do 128kbps
but have you tested? There's theory and math, and then there's actual real world performance. Sometimes real world is better than the math, especially when dealing with human perception, and sometimes it's far worse.<br>
Sparkfun seems to have fixed their VS1033D breakout board now, so hopefully Step16 is no longer needed for new builds
Hey, sorry for bothering you again:( <br>i am currently using a mbed to program the alarm clock, and I have successfully decode one song from sd card. Just wondering how can you decode the name of the song so the mbed can decode any song that I put in through USB. Currently I am using code like ' fopen(&quot;/sd/mp3/test1.mp3&quot;,&quot;rb&quot;);' so the title of the song must be test1. Thank you very much:)<br><br>
Hello, I have purchased all parts for this kit and progressing on&nbsp;it.&nbsp;For IR Code Capture I am getting problem and stuck since very long time. Even for &pound;1 valued IR diode I have spent &pound;16 and for remote controls I have spent &pound;40. I would appreciate if any body can guide me for&nbsp;Remote Control. Where can I get intented remote control (make / web link).<br> <br> Please help. Your help is highly appreciated.<br> <br> Best regards,<br> Ankush
You are supposed to be able to use any remote control. Nobody sells the remote control I used.<br><br>You are supposed to try it out, and if it doesn't work, figure out why it doesn't work, and fix the problem. So what is the problem? What do you expect to see? What do you see instead?
Hello Frank,<br><br>I am using codes given by my remote controls in IRCodeInterpret module but not matching with and just displaying message Checking .... on Realterm.<br><br>As described by you, remote control should display same pattern that's what not happening.<br><br>Any other ways to overcome this problem ?<br><br>Please help.<br><br>Best Regards,<br>Ankush
Hi Frank,<br><br>Thanks for your response. I am getting codes with IRCodeCapture module but each time for same button pressed on remote control codes displayed are different.<br><br>I put these codes in IRCodeInterpret module and check it. I get output on RealTerm screen as checking.. <br><br>When I put ircodes in header file and check interpret module then I should get which button is pressed on remote control. But it is not showing due to codes not matching.<br><br>Hope the problem is resolved with your help.<br><br>Regards,<br>Ankush<br><br>
Great project! really helps a lot. I just got couple of questions wanna ask you about. Firstly if i decided to use an arm micro-controller, i would not need to use a music decoder right? Instead I need a DAC? <br> Secondly, how do u decide what size of RAM and frequency u need]? <br> Thirdly, is sd card originated formatted in fat16 and what does fatfs do exactly (the difference between it and fat16) ? <br>Lastly,why do you choose ADR instead of ARM? Is 8 bits AVR gonna be fast enough for playing mp3 file? <br>Cheers and look forward to hear from u <br> <br> <br> <br>
Most ARM microcontrollers have DACs built-in<br><br>I did not really make a decision on RAM and frequency, I just knew what I had will work.<br><br>FAT16 is the format, FatFs is the software used to read the FAT16 format.<br><br>I simply like using AVR chips. It works... Developing for them is comfortable.
to determine RAM requirements, you can start by considering that FatFs requires a 512 byte cache at least<br><br>The frequency required can be determined by calculations using the MP3 file bitrate, and the maximum SPI bus transfer rate<br><br>I can use ARM if I wanted to, I have worked with AT91SAM7X, mbed, and STM32F4. Most ARM chip requires a custom designed PCB because they do not typically come in DIP form, and I didn't really want to spend the money on the custom PCB.
Thank you so much for the answer. I am trying to calculate the mp3 bit rate which is 100kbps (3mb/(5min x 60)) .however how do I relate it to the frequency of the microcontroller? <br> <br>
frequency of the microcontroller will limit the speed of the SPI bus, which is used to transfer the data between the SD card, the mcu, and the decoder
thanks for the answer.,i think i wrote my question in the wrong way..i wanted to ask how do you calculate the minimum and maximum frequency that you need to have for the microcontroller
helllooo..thanks for this! <br>can i ask what other micro-controller and music decoder that i can use which would have the same functions but cheaper? Many thanks :)
I've seen designs that use the STA013 but you need a DAC to go with that, I don't know if it'll be cheaper but it's another option<br><br>Most ARM processors can probably decode the MP3 file without external help, the mbed already have an example program available for it, it also have a built in DAC. The mbed is kind of expensive at $60-ish but it's awesome, and the only people who say bad things about it are too stupid/lazy to compile offline for it.<br><br>A few weeks ago, STMicroelectronics were giving out free STM32F4Discovery kits, now they cost some money, but not too expensive, this board has a ton of audio features, it has a microphone built right in, and a I2S sound output thing with a headphone jack. The processor itself have DSP and FPU. It's a good candidate.
sorry for so many questions....i am really a beginner in this field..may i ask why you need music decoder for your design? i saw online that avr has mad library that could use to decode mp3. and also arm has the libmad library to decode the music without music decoder right?
show me a 8 bit (not 32 bit) AVR using MAD please.<br><br>I highly doubt a 8 bit AVR at 8 MHz can use MAD.
hey.i have decided to use a mbed.. however from my research, you still need a music decoder to go with it? also i couldn't get how sd card communicate with usb host and usb host with pc using mbed.thanks :)
you don't need a decoder, see http://mbed.org/users/Gruenfrosch/programs/madplayer/liih0r<br><br>Also, you are not being a USB host, you are being USB device, NOT host<br><br>You cannot communicate between SD card with MCU and SD card with computer at the same time
hey.sorry to bother u again.can i use vs1003b instead of vs1033d as the only difference is the i2s interface? And the I2S interface for external DAC function is not used in this case right?<br>Thanks :)
those two chips are compatible if you are simply playing MP3 to headphones<br><br>You can use almost any AVR microcontroller you want, if you want the USB mass storage capability then obviously you need one that features USB. Just make sure it has enough memory to do what you want, enough pins for everything, and a SPI bus to do the talking between the SD card and MP3 decoder.<br><br>I posted rough wiring schematics in the instructable, I'm sure you can adapt the circuit to whatever you are doing
So do that that mean if i wanna use a loudspeaker, then i must use the audio decoder with I2S interface? Thanks :)
no, you need an amplifier of some sort
sorry...i didn't quite understand..so i can use vs1003b connect to speaker but i need an amplifier to make the music sounds louder right? also, if i am now using sd card only, like what u did at the end, do i still need file system? thanks a lot :)
You can connect speakers directly to the VS1003b but only small speakers, if you need big speakers, you need an amplifier.<br><br>You should always use a file system, unless you have some sort of weird requirement
where to find teensy board ???
Is there a way to make the alarm start with a very low volume, then build up to regular volume over a few minutes?
Of course, that should be easy<br><br>The function that plays out the data runs in a loop, just increase a variable in that loop and repeatedly set the volume using that variable
Well then, it would appear I have some parts to buy.
Hey, Great project! All the parts I need just came in the mail so I have begun building this alarm clock. However, this is my first electronics project (so i'm pretty noob at it) and I am stuck trying to setup realterm correctly.. I uploaded the hex file to the microcontroller, and I have realterm installed and running. But how do you setup realterm to get it to read the data the microcontroller is sending?
if you are using the Teensy++, it should appear to your computer as a serial port, which has a specific COM port number, you need to tell RealTerm to open that specific port.<br><br>If you are using the ATmega644 then you need a USB-to-serial cable, which also will have a COM port number
I am using the Teensy++, I have everything setup correctly I believe.. But when I click start: overwrite or append, nothing happens. I have the capture file setup as well but it doesn't capture anything..
uncheck &quot;direct capture&quot; and see what happens
perfect, thanks
Very cool project! I wrote a very in-depth tutorial on the hd44780 and thought you might want to add it to your LCD Basics section: <a href="http://www.robotenthusiasts.com/page/index.html/_/tutorials/pic-microcontrollers/hd44780-lcd-r14">http://www.robotenthusiasts.com/page/index.html/_/tutorials/pic-microcontrollers/hd44780-lcd-r14</a><br>
Thanks, I added your link<br><br>I like your custom character tool

About This Instructable




Bio: I am an electrical engineer. I graduated from U of Waterloo. I used to work for Adafruit Industries as an EE. Now I work for ... More »
More by frank26080115:LED Pocket Watch Easy Cord Wrapping Around Power Bricks Using SMD Components on Breadboards 
Add instructable to: