t r o n i x s t u f f

fun and learning with electronics

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:

You will need to supply your own header sockets or pins and fit them  yourself. I have found the easiest way to do this is to put the pins in the header sockets, as such:

then push these into a solderless breadboard, with the shield on top upside down:

Then solder away. Before you know it, your shield is complete:

The red board colour is a nice contrast with the blue of my TwentyTen. 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. After downloading the library, it needs to be installed into the appropriate directory on your computer’s hard drive. If you are unsure how to do this, please read this tutorial by ladyada. If you use Ubuntu like me, place the extracted folder into /usr/share/arduino/libraries

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:

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:

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 last week’s articles, 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:

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.

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:

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:

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

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.

June 1, 2010 - Posted by | arduino, part review | , , , , , , , , , , , ,

14 Comments »

  1. 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

    Comment by roberto | June 5, 2010 | Reply

    • Hello Roberto
      Could you please email me your sketch [john at tronixstuff dot com], and if possible photos of your shield so I can have a look at your soldering?
      Thank you for reading, John

      Comment by John Boxall | June 5, 2010 | Reply

    • Or better yet, join our Google Group and ask the members for help: http://groups.google.com/group/tronixstuff

      Comment by John Boxall | June 5, 2010 | Reply

  2. 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!

    Comment by talsit | June 17, 2010 | Reply

    • 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

      Comment by John Boxall | June 17, 2010 | Reply

  3. 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

    Comment by Roberto | June 19, 2010 | Reply

  4. 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

    Comment by Martin Borja | July 23, 2010 | Reply

    • Email sent,
      problem solved :)

      Comment by John Boxall | July 23, 2010 | Reply

      • Hello, I have the same problem, but never managed to solve it. Can you help me with it?

        Thanks

        Comment by Raúl | February 26, 2011

      • please check your soldering, and that your microSD card is 2GB or less and formatted as FAT16.
        cheers
        john

        Comment by John Boxall | February 26, 2011

  5. 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.

    Comment by Matt Walker | August 11, 2010 | Reply

    • 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

      Comment by John Boxall | August 11, 2010 | Reply

  6. 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

    Comment by Christopher | October 16, 2010 | Reply

    • Hi Chris
      Please double-check your soldering, and then using the demonstration sketch in the review, and your 2GB FAT-16 formatted card, and see how you go.
      If you have any more questions, please ask in our Google Group – http://groups.google.com/group/tronixstuff
      cheers
      john

      Comment by John Boxall | October 16, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,588 other followers