Logger Shield How to use the SD card
Introduction

The other half of the data logger shield is the SD card. The SD card is how we store long term data. While the Arduino chip has a permanent EEPROM storage, its only a couple hundred bytes - tiny compared to a 2 gig SD card. SD cards are so cheap and easy to get, its an obvious choice for long term storage so we use them for the shield.

The shield kit doesn't come with an SD card but we carry one in the shop that is guaranteed to work. Pretty much any SD card should work but be aware that some cheap cards are 'fakes' and can cause headaches.

You'll also need a way to read and write from the SD card. Sometimes you can use your camera and MP3 player - when its plugged in you will be able to see it as a disk. Or you may need an SD card reader

These are very common, available in any computer store. The shield doesnt have the ability to display the SD card as a 'hard disk' like some MP3 players or games, the Arduino does not have the hardware for that, so you will need an external reader!

To use the SD card interface library you'll need a '328 Arduino. If you have an NG or '168 chipped Arduino we suggest upgrading to a '328. Its easy and inexpensive and you'll be very happy with the 2x RAM and Flash. All UNO's have Atmega328 chips

Format under Windows

If you bought an SD card, chances are its already pre-formatted with a FAT filesystem. The Arduino library we use supports both FAT16 and FAT32 filesystems. If you have a very small SD card, say 8-32 Megabytes you might find it is formatted FAT12 which isnt supported. You'll have to reformat these card

If your card is not formatted, its pretty easy to do and you only need to do it once. To format the card, place it into your card reader, then right click on the disk and select Format...

Make sure that in the File system pulldown menu, that FAT

And click Start

If you get the Properties of the card you will see it is FAT formatted. This card has some files on it so its not completely empty

Formatting under a Mac

To format the SD card, you will need the path to the SD card device and the SD card device number.

  1. Insert the SD Card into a card reader.
  2. Open Disk Utility
  3. Highlight the SD Card device (upper icon), not the SD Card volume (lower icon)
  4. Click Erase at the top of the window
  5. Select Volume Format: MS-DOS (FAT) Click Erase... > Click Erase
  6. It is now formatted in MS-DOS FAT32 !
Talking to the SD card

Interfacing with an SD card is a bunch of work, but luckily for us, Adafruit customer fat16lib (William G) has written a very nice Arduino library just for this purpose and its now part of the Arduino IDE known as SD (pretty good name, right?)

The only problem is that we've made a few nice updates to the SD library which are not in Arduino v22. So if you're using v22 please install our revision as follows!

Download the library by clicking the DOWNLOAD button at the top right. Then make a backup of the folder called SD in your ArduinoIDE/libraries folder. Then uncompress the newly downloaded folder and rename it SD. Inside the SD folder you should see README.txt and other files. Install it by dragging it in your ArduinoIDE/libraries folder and restarting the IDE

FOR MEGA ARDUINOS edit the SD/utility/Sd2Card.h file after installing and uncomment the line that says #define MEGA_SOFT_SPI 1 to allow the Mega to use the same pinout for SD cards as the Classic Arduinos

Next, select the CardInfo example sketch

This sketch will not write any data to the card, just tell you if it managed to recognize it, and some information about it. This can be very useful when trying to figure out whether an SD card is supported. Before trying out a new card, please try out this sketch!

Go to the beginning of the sketch and make sure that the chipSelect line is correct, for the datalogger shield we 're using digital pin 10 so change it to 10!

OK, now insert the SD card into the Arduino and upload the sketch.

Open up the Serial Monitor and type in a character into the text box (& hit send) when prompted. You'll probably get something like the following:

Its mostly gibberish, but its useful to see the Volume type is FAT16 part as well as the size of the card (about 2 GB which is what it should be) etc.

If you have a bad card, which seems to happen more with ripoff version of good brands, you might see:

The card mostly responded, but the data is all bad. Note that the Product ID is "N/A" and there is no Manufacturer ID or OEM ID. This card returned some SD errors. Its basically a bad scene, I only keep this card around to use as an example of a bad card! If you get something like this (where there is a response but its corrupted) you should toss the card

Finally, try taking out the SD card and running the sketch again, you'll get the following,

It couldn't even initialize the SD card. This can also happen if there's a soldering error or if the card is really damaged

March 26, 2012 12:02