8. lm35_setup

Arduino Datalogger with Memory Card

How do you save data from an Arduino-based device to a memory card? Fortunately, a microSD card and SD card breakout board now make this an easy task. Below is a simple tutorial on how to capture data generated from your Arduino and write it to a text file on a microSD card for later analysis. Let’s get started!

SD Card Breackout Board


All connections (pins) in the breakout board are clearly labeled. Take note — the breakout board uses SPI protocol, so for use with Arduino Uno or a Pro Mini-compatible board, make interconnections as follows:

  • 5 V to 5 V (VCC)
  • GND to GND
  • D13 to SCK
  • D12 to MISO
  • D11 to MOSI
  • D10 to CS

Another Arduino Library

Now get ready to install the required Arduino library. Just download the zip file “SdFat-master” from here, extract the zip file, and copy the “SdFat” folder into your Arduino IDE’s “Libraries” folder. Location of the library — usually in the “Sketchbook” folder — can be found in the Arduino IDE’s “Preferences” menu. As you may have noticed, there is an SD card library included with the Arduino IDE. However, we prefer the above-introduced library.


Also remember to format your microSD card using your computer (try FAT16 or FAT32 for bigger memory cards). Next, insert the microSD card into the microSD adapter and plug it into your breakout board. Finally, open/restart the Arduino IDE.

The Pre-Flight Test

To conduct your first test to ensure that everything is okay up to this point, open the “SdInfo” sketch in the IDE (File > Examples > SdFat menu). Make sure that your Arduino is connected to the computer and that the serial port is correct before uploading the sketch. Author’s hardware setup is shown here:

3.first test_screenshot


Next, open the serial monitor to enter any character on the keyboard and press Enter. The sketch will examine the memory card and report the characteristics on the serial monitor. The report of our 1-GB microSD card is shown below as an example. If this experiment is not successful, recheck the hardware and memory card format. If your memory card characteristics are reported in a similar manner to the one shown here, all is well and you are ready to log data.


Start Data Logging

Now you can write data to the SD card. First, open the “Datalogger” example sketch (File > Examples > SD > Datalogger) and upload it to Arduino. This sketch reads the value from three analog input points and writes it to a text file. Once uploaded, open the serial monitor to “see” the logging. After this, remove the memory card and connect it to your desktop PC with the help of a USB memory card reader (most laptops have a built-in card reader slot). Once you find the contents of the memory card, you can read the “DATALOG.TXT” file saved on it. Voila!


If you want to preserve your data in a neat format for future use and/or analysis, simply call upon your favorite spreadsheet to open and process the text file (see the example shown below).


LM35 & Memory Card

This is a little DIY project with an Arduino Uno, SD card breakout board, and LM35 temperature sensor. To build a temperature logger using a microSD card, just connect the LM35 analog temperature sensor chip to your already-prepared hardware (used for your previous experiments) as indicated in the wiring diagram shown below.

8. lm35_setup

After hooking everything up, upload the compiled code to Arduino and check the result on your computer after some time by reading the “LOG.TXT” file saved in the microSD card. This is a “dirty” experimental code; of course, you can polish it for your need and taste.

#include <SPI.h>
#include <SD.h>
File myFile;
int x = 0;
int tempValue;
void setup()
   pinMode(10, OUTPUT);
void loop()
   tempValue = analogRead(A0);
   myFile ="log.txt", FILE_WRITE);
   if (myFile) {
      myFile.print("Temperature ");

One Comments

Join the conversation!

Error! Please fill all fields.
  • sidneyb2001yahoo-com

    Dear Sir,
    How do I modify this data logger to use on a 120 VAC 50Hz line that has a high voltage transient problem due to a loose neutral main line coming into a school.
    I would like to monitor the 120 VAC line or the 220 VAC line for a period of 7 days to capture the time and the transient peak voltage levels that are occurring when there is a loan on the line. My e-mail address is:

Looking for the latest from TI?