Part review – microSD card Arduino shield

Hello readers

Today we are going to look at a micro SD card Arduino shield. The reason to use such a thing is to have a storage dump for any data that you generate with your Arduino project that can accept a very large amount of data – up to several gigabytes if you have a large enough micro SD card. With the appropriate sketch it is also possible to read from the card, navigate file directories and so on, but to keep it simple I am just going to examine the most popular aspect – writing our data to the card. However if enough people ask me I will spend the time to figure out the rest.

Initially I imagined this project would be quite difficult, but after some research it was fine. You’re lucky to not have to do the work completed by myself :)

Anyhow, moving on. The shield is shipped in the usual minimalist packaging, a plastic bag and the shield:

shield

You will need to supply your own header sockets or pins and fit them  yourself. Then solder away. Before you know it, your shield is complete:

soldered

The red board colour is a nice contrast with the blue of my Eleven. Now of course you will need a micro SD card to write your data to. Contrary to popular belief you can use SDHC micro SD cards that are larger than two gigabytes in size. First of all, you will need to format your micro SD card. Check the instructions or help system of your computer’s operating system to determine how to do this. However ensure that the format type is either FAT32 or FAT 16, not MacOS or ext3 or NTFS, etc. Next we need to prepare the Arduino IDE to work with the shield. There is a library of functions that needs to be installed for the project to work. Bill Greman has written an excellent library to use, download it from here.

On the software side of things, please note that the shield requires exclusive use of digital pins 10, 11, 12 and 13 for the SPI interface to the card reader. The next thing to do is test our new shield. Plug the shield into your Arduino Duemilanove or compatible board, then the micro SD card into the slot. It will need a small amount of pressure, as it “clicks” in. Also note that in order to remove the card, you push it and it pops out a little. Don’t try to just pull it out with your thumbnail. It is also wise to only insert and remove the card when the power is off.

Assuming you have installed your library correctly, fire up the Arduino IDE and select File menu > examples > SDFat > SDFatinfo. Plug in your shield, upload the sketch, then hit the serial monitor button. Enter a character and press enter – you should be presented with something like:

sdfatinfosketchscreendump

This display shows various data about the card, the formatting type and so on. If it did not work, check your soldering on the shield, re-format the card with FAT16 or FAT32, reseat the shield into the Arduino, reconnect to the PC and try again.

Next it is time to write something to the card, to get a feel for how things work. Run the “SDFatwrite” example, open the serial monitor box, enter a character and press enter. Now open the resulting text file found on your micro SD on your computer. You should have something that looks like:

sdafatwrite

There really is a lot of code in the demonstration sketch, but to make things easier to adapt, have a look at line 90 to 94 of the sketch.

The writeString() function writes text to the file, just like Serial.write() would to the screen. The writeNumber() function writes integers, or unsigned integers to the file. And the writeCRLF() function starts a new line in the file. You can basically copy and paste the code into your own sketch and use these functions, as long as your variable types are suitable for the functions.

In saying that, I have made a demonstration sketch to prove this. Using the real time clock shield from a previous article, an Analog Devices TMP36, and a 560 ohm resistor/LED on digital pin 2, we can make a temperature logger with time and date. This involves a nice stack of Arduino goodness:

triplestack

and a solderless breadboard with the temperature sensor and the LED setup. If you had a really small breadboard, you could plonk it into the micro SD shield and save space. Alas, mine did not fit.

demosetup

But it worked. Now for the sketch – you can download it here: demonstationsketch.pdf. If you examine the sketch I have filled it with comments and points of interest. If you are unsure of how the real time clock code works, please visit here. Fore more information about the temperature sensor, please visit here. There was no need to compute Fahnrenheit in the sketch, as this can be done later on in a spreadsheet, saving you sketch memory and storage space.

The purpose of the LED is to let you know when the sketch is about to start, and when it has finished. Once the blinking starts at the end of the sketch, you can power off and remove the micro SD card as the program has written and closed the file. If you do this before the sketch has finished, you may corrupt the file and lose your data. Here is an example of the file from the demonstration sketch:

tempdemoscreenshot

Notice how there are distinct columns between the data. This is important as later you may want to import the text file into a spreadsheet to analyse your data. For example, if you use the Insert > Sheet from file… command in the Openoffice.org spreadsheet, you can select which columns of data to import, like this:

ooossimport

Which will leave you with nicely delimited data that you can twist around to your heart’s content:

ssresult

In this spreadsheet I have calculated the minimum, maximum and average temperature – and in Fahrenheit as well. By just capturing the raw data using the micro SD shield you can offload a lot of processing work from the Arduino and onto your personal computer  – a much more efficient solution. The spreadsheet has been placed in the files section of our Google Group.

So there you have it. You now have the tool and an understanding of how to capture data from the real world, and bring it home to analyse and make decisions from it. The possibilities are almost limitless, using a wide range of sensors, user inputs, even GPS modules, you can get a better understanding of the world around you. High resolution photos are available on flickr.

So have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of tronixstuff.com.

18 Responses to “Part review – microSD card Arduino shield”

  1. roberto says:

    execellent tutoria!…but I have a lot of problems….
    first, I used the libraries in sparkfun site…they are very complicated to use, and I decide to use SdFat, more simple but…work not lineary…otherwise work for very short time, like 10 seconds, not more…also, if I reset or unplugged the micro sd arduino show the message SD error 1,0
    I try all different version (sorted by date) of SdFat libraries but same prblems appear.
    I use twoo different micro sd , transflash 2 giga and sandisk 256 mb formatted to fat 16 …same problems…could you hel me!
    (apologiz my schoolastic english…i come from italy)
    thank you for all
    roberto

  2. talsit says:

    Hi hi!!
    I was wondering if you know what type of throughout you can get with this device?
    I’m looking into making a “high-speed” data logger and I would need to be able to write about 40kb/s.
    Cheers!

    • John Boxall says:

      Hello
      Good question. Was that 40 kilobits per second, or 40 kilobytes per second (for example, writing the character “q” 40000 times per second)?
      Just completed a very un-scientific test of writing the real time to a file. The best I could get was roughly 5900 characters per second, or about 5.76 kilobytes per second.
      Maybe post your this question at chiphacker.com and let the team over there have a think about it.
      Cheers
      John

  3. Roberto says:

    Hi john
    thank you for all..i resolve the problem!
    simply,
    for work properly is necessary define the number of pin in sketch…in otherwords:
    i use the example SDFatAnalogLogger, but after 20 seconds the sketch was out. So I have defined the pin like this:

    #define CS 8
    #define MOSI 11
    #define MISO 12
    #define SCK 13

    and in void setup…
    pinMode(CS, OUTPUT);
    pinMode(MOSI, OUTPUT);
    pinMode(MISO, INPUT);
    pinMode(SCK, OUTPUT);
    pinMode(10, OUTPUT);

    i think that this problems is caused from different pin set in SDFat and Fat16 (optimized for sparkfun shield) libraries

    if any people whant the sketch i could sent him
    bye
    roberto

  4. Martin Borja says:

    Hello,

    I got to try this shield with SDFat Lib
    Prior to soldering the shield, I was able to get it to run.
    After soldering to headers, suddenly I just keep on getting the

    type any character to start

    card.init failed
    SD error
    errorCode: 1
    errorData: 3F

    type any character to start

    error when running SDFatInfo.

    Please help.
    I can’t seem to get the board to work.

    Best regards,
    Martin

  5. Matt Walker says:

    Great review and tutorial! This site has been infinitely helpful as I’ve started with the Arduino.

    Could you use this at the same time as the LCD? Pins 11 and 12 are used in both. Can you just change them on the lcd and reflect that in the lcd code or are additional steps required?

    Thank you so much for your help.

    • John Boxall says:

      Hello Matt
      Thank you very much for your feedback, I really appreciate it.
      Yes, you could use the microSD shield and an LCD at the same time, as you can reassign the pins to use for the LCD in the function LiquidCrystal. Just don’t use D10~D13, as these are required for the microSD shield.
      have fun
      john

  6. Christopher says:

    Hi, i have the same problem as roberto but i dont know how to fix it. I use a Arduino duemilanove and the microSD Shield from sparkfun. I tried the SdFAT lib, the SdFatWrite example. But as i type any character to start, it pops up the SD error 1,0. I use a 8GB Transcend microSD FAT32 formated. I also tried it with a 2GB microSD FAT16 but then ill get the SD error 1,FF. Can anyone help me?

    Chris

  7. pinterest pins says:

    It’s a pity you don’t have a donate button! I’d certainly donate to this outstanding blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this website with my Facebook group. Chat soon!

  8. Rodrigo says:

    john, i was wondering to know if there’s a way to read the info from the file with the arduino, for example, in my sketch i ask the user to select a number, store it into the card, and then give that number to a variable, or is it a way to save it into arduino’s memory, without the program erasing? greets

Trackbacks/Pingbacks


Leave a Reply

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: