Loading... Please wait...

Tutorial - Using SD card Breakout Boards with Arduino

Posted by

Saving data from your Arduino-based project to a memory card is simple and a great way to log any data for analysis later on. Doing so is easy thanks to the SD card and microSD card breakout boards we have here at Tronixlabs Australia:

SD card breakout boards from Tronixlabs Australia

Please note: Memory cards are shown for size comparison and not included with the breakout boards!

The goal of this tutorial is to show you how to capture data generated from your Arduino or compatible board and write it to a text file on a memory card. 

The choice of breakout board comes down to which memory card you would like to use, some prefer SD card as their computer has an integrated socket, and some like microSD cards to maintain compatibility with other devices.

This tutorial will cover both types, as once you have connected it to your Arduino or compatible board, the sketches and further processes are identical. So let’s get started. 


After looking at your breakout boards, you can see that the pins are clearly labelled. There is only one difference to note - if you have the SD card breakout board, it will have separate pins for 5V and 3.3V, whereas the microSD just has Vcc.

You will also need six male to female jumper leads, which we also have in stock

The boards use the SPI bus, so if you have an Arduino Uno or compatible board or a Pro Mini-compatible the connections are as follows:

Arduino to breakout board

  • 5V to 5V or Vcc
  • GND to GND
  • D13 to SCK
  • D12 to MISO
  • D11 to MOSI
  • D10 to CS

If you have an Arduino Leonardo or Mega - for the sake of simplicity connect your breakout board using the ICSP pins on your board. These are the three rows of two pins found on the right-hand side of the board as numbered and shown below:

Tutorial - Using SD card Breakout Boards with Arduino by Tronixlabs Australia

Your connections will be as follows:

Arduino ICSP to breakout board

  • 2 - 5V or Vcc
  • GND - GND
  • 3 - SCK
  • 1 - MISO
  • 4 - MOSI
  • D10 - CS

Arduino Due users - you have a little extra work to do due to the faster CPU speed, check out Bill’s notes here

Arduino Library

Now it’s time to install the required Arduino library. The observant among you will realise that there’s already an SD card library included with the IDE, however this is somewhat inefficient with regards to sketch size and thus we’ll use an enhanced version by Bill Greiman.

To install this library, download the .zip file from the right-hand side of this github page. Once downloaded, expand the .zip file and copy the SdFat folder into your Arduino IDE’s libraries folder.

If you're not sure where your library folder can be found - it's usually in your sketchbook folder, whose location can usually be found in the Arduino IDE preferences menu, for example:

Tutorial - Using SD card Breakout Boards with Arduino by Tronixlabs Australia

Now restart the Arduino IDE if it was already running - or open it now.

Testing the Breakout Board

At this point we can run a simple test to check everything is working. If you have not already done so, format your memory card (checking for anything you need on it before doing so!). You can use FAT16 or FAT32 for larger cards.

Then insert the memory card into your breakout board, making sure it locks in (when pushing in, the process feels “spring loaded”). Then open the “SdInfo” sketch in the IDE - this is found in the File > Examples > SdFat menu - for example:

Tutorial - Using SD card Breakout Boards with Arduino by Tronixlabs Australia

Ensure your Arduino is connected to the PC, check the serial port is correct and the board type is selected using the options in the Tools menu, then upload the sketch.

Once uploaded, open the Serial Monitor - then enter a character on the keyboard and press Enter (etc). The sketch will examine the memory card and report the characteristics on the serial monitor, for example our 16 GB microSD card resulted with the following:

Tutorial - Using SD card Breakout Boards with Arduino by Tronixlabs Australia

If this wasn’t successful, double-check the wiring and card format. However if your card characteristics were returned in a similar manner to that shown above, all is well and you’re ready to log data.

Writing data to the SD card

Now for our goal, to write data to the SD card. First of all, copy the demonstration sketch from our github page and enter it into the Arduino IDE. This sketch reads the value from two analogue input pints and writes it to a text file. 

This is a typical Arduino sketch, and the code required to use the SdFat library is included in section A of the sketch (lines ten to 73) and section B (lines 45 to 73 within void setup()). 

Then to write text or data to the memory card, review the lines 93 to 96. You can see that


is used to write the contents of a variable, and 


is used to write text to the file. 

To test all of this, insert your memory card into the breakout board and run the sketch. Once uploaded, open serial monitor. This will tell you when logging starts and finished. Press reset after it has finished for a few times to generate separate files. 

Now remove the memory card and connect it via a USB card reader to your computer. Once you read the contents of the memory card, notice that the filename increments by one every time, for example:

Arduino and SD card tutorial results by Tronixlabs Australia

As noted earlier, this accomplished by the code in section B of the sketch - this is best reused as it ensures you don't write over a file with the same name. When logging data it's best to simply capture everything then sort it out later on a PC. 

Now to open the data on a PC - simply use your preferred spreadsheet to open the text file. You will need to delimit the data by "comma" (note how this separates the data in line 94 of the sketch. Once this has completed your data is neatly presented for further use and analysis - for example:

If you simply need to log data then you can modify this sketch by changing the logdata() function to meet your needs. 

You can change the filename created by the sketch easily - by default with this code it is DATAxx.csv where xx is an incremental number starting from zero, one is added to every new file placed in the same directory. You can change the prefix DATA to something else (keep it short) on line 22 of the demonstration sketch. 

There are many more features to the SdFat library, which can be explored through the example sketches from the library, they can be reviewed easily from the examples page

If you found the code in this library to be a little too complex - please check out the SD library that is included with the Arduino, it's easier to use however takes up more of your Arduino's memory that is used for sketches. 

And if you're new to Arduino, we highly recommend reading a copy of "Arduino Workshop", which is available from the tronixlabs store

If you have any questions about the content in this tutorial, please ask in the Tronixlabs customer support forum or email support@tronixlabs.com.

Learn how to use DS1307 and DS3231 real-time clock modules with Arduino and Tronixlabs

Don't forget to stay tuned for news, updates and more by following us on twitter, facebook or Google+.

So have fun and make something!

John Boxall.