Home > Uncategorized > Use an SD or MMC card on your next AVR project

Use an SD or MMC card on your next AVR project

January 16th, 2009

SD / MMC cards support a SPI mode.  By connecting it to an AVR’s SPI port, it can be used for general storage relatively easy.  Here I demo the use of an SD / MMC card as general storage for electronics projects. I am using it with a 1MHz Atmel AVR atmega8, standard 2GB microSD card with adapter sleeve, and using a PWM port as a cheap DAC for sound output.  I first loaded the SD card with pre-recorded sounds at 8KHz and 8-bit unsigned linear.  I then read them in real time from the SD card and write the values to the OCR so that they set the duty cycle on a PWM port.  I use a simple RC low pass filter as a cheap DAC (Digital to Analog Convertor) that is output to a speaker.

Schematics

Coming soon.

SD MMC Card Pinout

SD MMC Card Pinout

pin number name avr pin
1 CS SS
2 DATAIN MOSI
3 VSS1 GND
4 VDD 3.3v VCC
5 CLK SCK
6 VSS2 not connected
7 DATAOUT MISO
8 DAT1 not connected
9 DAT2 not connected

Source Code

I first found GPL example code at http://www.avrrepository.com/samplecode/sd_breakout/doc/main.html. It does not support writing though :-( .  Because it is GPL, I modified it so that it supported writing as well.  Here are the files modified to support writing (I haven’t had time to clean them up).

mmc_if.c

mmc_if.h

Demo Video

mike Uncategorized

  1. josev
    February 19th, 2009 at 07:35 | #1

    This is cool! Is the development board commercial ? or home made? I am making a little board with ATMEGA168 , but as perb other links using the SD card the mega168 may not have enogh memory. I think is because they use a file system. But I am looking on just reading and writing a single file. How the file is identified by the ucontroller , in your case do you have several files in the SD o is one with a fixed name? I will appreciate any tips on this thanks…. Jose

  2. March 21st, 2009 at 20:58 | #2

    It is a custom made board. I switched from atmega8 to atmega168 to get more flash to fit the FAT library. I’m using the FAT library from elm-chan.org which takes up 4-5k of flash. The one from CC Darmintech in my blogroll is smaller … I just haven’t started using it yet.

  3. a.mlw.walker
    April 3rd, 2009 at 09:46 | #3

    Hi Mike, I am very pleased i found your site. I am new to avr programming, and for my first project i built a mp3 player. It uses FAT.h and FAT.c at the moment to read the mp3’s from the sd card (i think!). after playing an mp3 it generates a random integer (dont ask why at moment) but i want to be able to write that random number to a text file on the sd card. And i am struggling alot. with the two files you have modified at the top embedded into my project, what few lines of code are needed to ammend a text file with a variable? I would be very grateful for any help you can give me.

    Alex

  4. April 6th, 2009 at 22:39 | #4

    I suggest you check the docs at the elm-chan.org link. It shows the f_open() and f_write() calls that you’ll need. It also has lots of examples. You can use my source code as an example also.

  5. melx
    April 9th, 2009 at 20:20 | #5

    hi mike, your proyect seems really interesting, I’m kind of new using Sd’s with AVR. I have to play .wav files from a 2gb SD using an atmega16.

    So rhis is what i did ,first I recorder the audio with the software called “Free MP3 Sound Recorder” with a sampling rate=8khz,a format: PCM(.WAV) ,8 bits per sample ,them i loaded the files in the SD. Now I need to read those files and play it ,just like u did in the video. I’m really on how to read the file from the SD, which libaries do i need? do I have to expecifie the allocation of the file or simply I just write the file name with the extension… Any tips?

  6. April 9th, 2009 at 21:36 | #6

    I think you are on the right track with the .wav file format. You’ll need to make sure that you use either signed or unsigned depending on how you are treating it in software. .wav files do have a header prepended to them. For your purposes, you can ignore the header by simply attempting to play it as if it were audio. It will make a short quick “blip” that you probably won’t even notice when you try to play it. As for output, what I used was PWM. You can configure the timer counters to output a PWM wave form. If you put a properly sized cap and resistor on the pin, it will smooth it out well enough to sound pretty good.

    Here is a link to another project that explains this technique pretty good. I think this example in the link below sends the audio over serial instead of an SD card.
    http://www.rpi.edu/~kouttd/03/Rage_against_the_arduino.html

    As for the SD card, it takes two pieces. First, you need to be able to read and write sectors of data on the SD card. They are organized in 512 byte sectors. The source code linked in this post shows this, mmc_if.h and mmc_if.c. This allows you to store bytes on the sd card as sequential sectors of 512 bytes each. You start at the first sector, read off 512 bytes, and then move to the next. This will work just fine to read and write data but gives no organization to the data like putting it in files. For this example, that is exactly what I did. I defined fixed offsets where I put each recording.

    This is not very convenient which is why a filesystem is important. By putting a filesystem on top of the raw interface to the SD card, you will be able to read and write files. In several projects I’ve used the FAT16 filesystem which lets me share the SD card with my PC using a USB card reader. I can copy over the .wav files and choose which one I want to access. This provides a level of organization and gives a much cleaner interface to work with the data. I chose the FAT library from elm-chan.org ( http://elm-chan.org/fsw/ff/00index_e.html ) for several projects. It gives very familiar looking functions such as f_open() and f_read().

    I haven’t yet posted any projects where I used the FAT16 filesystem on top of the mmc_if.c/h files that I share here. I’ll email those to you since I don’t have time to clean them up and post them.

    Lastly, I’ll mention that CC Darminitech (in my blogroll) developed his own lightweight FAT32 filesystem library. I haven’t used it myself but he has good things to say about it.

  7. josev
    April 10th, 2009 at 06:40 | #7

    Alex, Mike, do you have a link on the file system applications ?

    Mike , you mention to Alex to use your example code but mmc_if.c access the SD card in raw spi, ie you can not read the files in the PC. I downloaded the elm project and I will start compiling and see if the mega168 will fit but so far i see they are plenty of files, I am yet digesting them.

    Is there a way to get the code of your example on the video playing the mp3 with the remote, do not worry about being clean, a working project is enough drive to make an effort understanding a code with not many comments etc.

    Just for information and as my contribution, using avr168, I’ve made a thermometer and compass using the simple Dinsmore sensor for a car . Now I am playing with a more accurate HMC6352 magnetometer which uses I2C , so if anyone interested I can pass the code. Thanks Jose

  1. September 11th, 2010 at 17:22 | #1
  2. March 9th, 2011 at 01:43 | #2
You must be logged in to post a comment.