BLOG > Tutorial 11 for Arduino: SD Cards and Datalogging

This is my first in a series of more advanced Arduino tutorials. Now that we’ve covered the basics in tutorials 1-10 (you have watched them all right!?), it’s time to start pursuing some more complex projects!  In this episode, we’ll utilize an SD card shield from cooking-hacks.com to create a datalogger.  First, I’ll walk you through the process of reading from, and writing to an SD card.  Then, we’ll utilize some of our knowledge from tutorials 4, 6, 7, and 8 to add several sensors that we can poll periodically using our datalogger.  The logger will record sensor values to its SD card in a useful format so that we can later use a computer program like Microsoft Excel to visualize the data.  Download the schematics and source code first so you can reference them while you watch the video.  For the first time, I’m using fritzing to draw the circuit schematics.  So, take a look at them, and let me know what you think!

 

You can download the files associated with this episode here:

GNU GPL License Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.

279 comments

    1. get a 2-in-1 USB SD Card Reader Cable. The SD will show up in your libraries with the name you gave it. Open it. every listing is a file. Open a file and you will see the log on excel. Copy it to computer. You can delete files from the SD at you option. Put the sd in trash and then remove the USB cable from your PC.

  1. Hello jeremy,
    your tutorials are very helpful. Can we use any SD Shield for this.
    Could you please give some information for selecting the shield.
    A link from e-bay would be great!

  2. Hi jeremy, great tutorial. I am using a different sd card shield than the one used in this video but program seems to be working fine. Except the only problem I am having is the program doesn’t stop running when there isn’t a card inserted in the shield, it continues to the loop and starts looping Couldn’t access file like below.

    Initializing Card
    Card Init. Failed
    Couldn’t access file
    Couldn’t access file
    Couldn’t access file
    Couldn’t access file

    Any ideas on how to stop this … Thanks

    1. P.S. I also discovered that the number of entries in the log file is 2x if not 3x the amount. Any ideas why this is happening ?

    2. Does your SD card shield have a CD (Card detect pin)? You can probe that to see if a card is inserted. You could also just have it go into an infinite while loop if no card is found at boot (you’d need to hit the reset button to start the check again).

      1. Hi jeremy thanks for the reply. I can’t really tell if SD Card shield I’m using has a CD, I’m kinda new to arduino. Below is a link of the SD Card that I’m using, Would really appreciate if you could let me know if it has one. Thanks a lot.

        http://www.adafruit.com/products/98

          1. Hi jeremy thanks for your replies so far but how do you go about fixing the multiple entry in the log file ?

          2. hai jeremy blum ur marvaless i just started using arduino and searching for sd card project u just made it easy. can u send me schematic and program it will helpfull and also tell me in which site i can find these sample projects.

  3. hi Jeremy.
    is it possible to display the content of a flashdrive to an LCD (touch screen)?
    what can be the materials to be used? and what are the codes for it? please help us. thank you.

  4. Jeremy Blum, hey man great tutorial, Really helped me a lot in getting the data logged in the SD card but I am having the problem that writing on the SD card only happens when I open the serial monitor, Otherwise it doesn’t happen. Any reason why?

    1. Opening the serial monitor resets the Arduino, but it shouldn’t have any impact on whether it is able to write to the SD card. Are you actually taking the SD card out, putting it in a computer, and seeing that nothing is written without the serial monitor connected, and doing the same check when the serial monitor is open? Maybe it’s not writing either time, but you are mistaking the debug statements as evidence that it is actually writing to the card.

      1. Hey jeremy, i have exactly the same problem, the data is not written on the sd card unless I open the serial monitor. I did check it on my computer each time so i have absolutely no idea what it could be.

        1. Any possible answers to this? I’m also storing data from the accelerometer to the SD card, but then until unless I don’t open the serial monitor it does NOT store it in the SD card.. Please anyone? Urgent. Thanks

  5. Hi Jeremy Blum
    I have question, How do you created new name of file? I mean , I want created more one file in SD card.

  6. Hi Jeremy Blum
    i have a small problem
    excel but all data in one column with commas between them !
    what is the problem?

    1. Hi Jeremy Blum
      i have a small problem
      excel put all data in one column with commas between them !
      what is the problem?

    2. Under the toolbar select text to data. try to open the file with the comma or space separator. Basically you have all data going to a single location

  7. hej Jeremy
    Sorry for bothering you, but I have a problem. I have programmed arduino for different scenarios. If is scenario 1–> erase and write 1 in data file, if is scenario 2 –> then erase and rewrite the same file and write in file 2 and so on…

    How can i compare if is written 1 or 2 or anything else on SD card?

    Thank you, Johnny

  8. Hello dude! This is so cool. But this is only storing data to the sd card, do you have any link or tutorial about accessing files from the SD card? Thanks a lot!

      1. I think he meant.
        How to use the data which be stored in SD card.

        Such as we keep data in SD card and use it to display on LCD.

  9. Hi, this is almost idientical to the SD examples in the arduino IDE. However, they are also including the SPI.h
    I can’t get this to compile unless i also include that.
    As a noob, I wonder what’s going on?
    Also, how many bytes are your compiled sketch, with and without the SPI.h?
    Regards
    Pete

  10. Hi Jeremy,
    I wrote a program that logs pressure readings to an SD card and creates a new file after a valve closes. This generates lots of small files instead of appending to one really big one (weeks of data logging accrued).

    Question
    Is there a way to transfer the CSV files stored on the SD card to a nearby networked computer connected to the arduino’s USB port? I’d remove the SD card to transfer files for data analysis but the datalogger is in a difficult to access location. Arduino ethernet shield is an option as well.

    Thanks for your advice

  11. Hello there, quick question…I’m trying to create a snazzy touch screen menu that navigates through 5 to 10 different pages on a 7 inch LCD. In order to make the menus look nice I figured the easiest way to create the menu would be to photoshop my individual menu pages and then store the images on an SD. My arduino (Mega 2560) would then just pull the image it needed based on what it needed to draw at the time. Is this do-able or am I reaching?

  12. Hi.

    I’m working in a project about to get de ambient light with a ambient light sensor, and save all the data into a micro SD card, I using de SPARKFUN MICRO SD SHIELD. I need to save de data each period (1 Hour) and each 6 hours get the average of that 6 hours and also I need to get the time and the date for each read data.

    I hope that you help me with this project.

    See you.

  13. Hi,
    Great tutorial !! really helpful.

    I am trying to log voltage and current onto an SD card using .csv format.
    My issues is that I need current and voltage with decimal places. (float type)
    However I cannot convert from float to string in this line:

    String dataString = String(seconds) + “, ” + String(Current) + “, ” + String(Voltage);

    What should I do please?

    Thanks

      1. Was following that forum but kept doing the same mistake again and again ..
        Finally found my silly mistake :D
        Thanks :)

  14. First press the panic button (reset button) on the logger. Better yet, both buttons

    Then check the SD card files by using the SD reader on your computer. If the computer reads the SD it was properly initialized. Like, other media, it needs to have stored a file allocation table so files can be written to it.

    If not then initialize it with your computer.

  15. Hi Jeremy,
    thank You for Arduino tutorials. It’s great for beginners as me. I tried to do datalogging and I have problem with temperature sensor LM35 reading. I use the code to convert “temp = (5.0 * analogRead(tempPin) * 100.0) / 1024”
    When I use analogRead only for LM35 , it’s working. But if I use one more sensor (like LDR) or potentiometer , analogRead for LM35 starts jumping. If potentiometer value is 0, LM35 values are fine. If potentiometer is like 100, LM35 values are jumping.
    How can other analogRead change readings for temperature readings? How to solve it?
    I tried to change several LM35 sensors, it’s the same.

    Thank You in advance!

    1. Glad you got it working! I was going to suggest the delay() between sequential reads, but it looks like the community beat me to it! Gotta look out for those “high” impedance analog reads!

  16. I didin´t get the commandFile.available(). In SPI the comunnication is one byte at a time. So when we make the float decade why is commandFile.available() = 4? Shoudln´t it be 1? The folowing loop runs how many time in case of 1000? 4 times? Shouldn´t the float decade be inside the loop? And isn´t that a infinite loop? Im not getting it.

    Another question for 1000 , why is there 4 bytes? the max number with 4 digits is 9999 and 9 in binary is 1001 we only need 4 bits not 8. I don´t get it

    Thank u for your time jeremy

  17. Hi Jeremy,

    That was a very helpful tutorial — I would be interested in knowing if the SD card data had to be accessed by removing the card and inserting it to your PC or if it could be accessed via the microcontroller (through usb) and how that might work?

    I am interested in creating a battery powered application where data can be logged, and then when connected via USB it can be read by the connecting device (PC or laptop). The complication comes in sharing the SD card between devices.

    Any suggestions?

    Thanks in advance,
    H.

  18. Hi jeremy

    Your tutorial is really helpful thanks. If I want to read the values of the voltage and current and store them in the SD card how do I do that?

  19. Hi Jeremy, great tutorials!

    You are a great inspiration for me, but I have one question, instead of ID number, how can we put the date and time, on the data-logger?

    Thanks a lot!

  20. hi Jeremy this tutorial are great i learned a lot, i have a question, if i want to read my file from the sd card, how i can serate my datas from the commas and get each data

    1. If you store your data as a .csv file, and open it in a spreadsheet program (excel) it will automatically put the data in the columns/rows.

  21. hi Jeremy this tutorial are great, i learned a lot, but i have a question if i want to read my file how i can get my data from the file but without commas how i can divide my datas from the commas

  22. Hi jeremy,your tutorials are useful and knowledgeable. Plz help me regarding deleting data in particular line which we want in SD card using arduino also help how to display SD card data in LCD display Using arduino

  23. Hi Jeremy
    Your tutorials are awesom. Can we use the stored data log values to be processed by the arduino. So that we use those values to process again.

  24. Hi, Jeremy, i have one question, please maybe you know why when i log the data in format .csv and when i open the file, all the datas are only in one square? i Think a i did the same like you, but i cant get the same thing like you :) . Thank you for yor answer

  25. Hi.
    The tutorial was great and use full.
    Got a small problem.
    i need to write float type data to the SD card.
    How can i do it?
    please help me.

    1. Something like this:
      static char dtostrfbuffer[15]; //initialise the buffer with the rest of your initialisations
      double sensorValue = 0; //inititialise the sensor value double

      //in your logging area, use dtostrf.
      //this example is from a program I wrote. It takes a 0-5v (0-1023) and converts it to volts, then converts that double to a string with 3 digits, 2 after the decimal place.
      dataString += dtostrf((sensorValue/205),3,2,dtostrfbuffer);

      I hope this is enough to get you going. If not google dtostrf

  26. Hi Jeremy,

    Great Blog. All this stuff you do seems really cool. I am currently going trough a Electrical & Electronic Eng. degree and I could not find both the degree or the tutors more useless. Are the topics you cover here given where you study or are just personal interests that you develop on your free time?

    Sorry if this was a bit of-topic and thanks for any advice.;)

    1. Both. My degree is in ECE, and a variety of the projects I cover on this website were done during my my time in academia. Other projects were purely done in my free time.

  27. hello jeremy
    I have tried the data logger as on the video, but I want to ask for the data logger would I use can not function when combined with fuzzy logic programs. about what causes it and what’s the solution? Thank you for your attention
    greetings, Richar.

  28. hey
    i have a shield which has sd card port , xbee and bluetooh
    but i m not able to use the sd card
    it gives error when i upload cardinfo example
    i am using it with arduino mega
    please help

  29. Hello Jeremy,

    Thanks for the videos, they are a big help.
    I’m using an ultrasonic sensor that calculates distance and taking that information to calculate the velocity of an object approaching or moving away. I’m trying to save the information to an SD card as a csv file. I have no idea why the data is saved into rows instead of columns. Also, for some reason when the distance does not vary, the velocity should be zero, but the zeros never save and the data gets mixed up.

    Any ideas to fix this?

  30. hi Jeremy’

    i want to know about cs pin?
    i am using multiples sd cards and run one card at a time when first card’s memory is full then switch to 2nd sd card.how could i change cs pin in main programe?

  31. Hello Jeremy. Is there any way to transfer the data obtained by the sensor in the Arduino to a computer via wireless, email, or any other way?, Other than just save it in the SD card?.
    If we collect the data in the SD card from 4 sensor like the set up you have in your video, how many day of data can I save in a 8GB SD card?

    1. Yes, there are lots of way. You could stream it over a serial connection, use xbee, add a wifi shield, use bluetooth, use a GPRS module with SMS messages….

  32. Hi Jeremy
    I have a MLX90614 IR thermometer shield which communicates serially (UART) with arduino due. I am getting data on serial monitor but bot able to understand it. What may be wrong ?

  33. Hi Jeremy,

    I am trying to design a power monitoring and SD card data-logging system for a wind 12V, 98W turbine. I was wondering if you could help me out in the codes. Your tutorials has been so helpful. I am using INA219 current sensor and microSD card breakout board. Please help.

    Thanks.

  34. I was trying to used you code for datalogger and is giving me this message:

    sketch_jul01a.ino: In function ‘void loop()’:
    sketch_jul01a:99: error: ‘class TwoWire’ has no member named ‘send’

    As of Arduino 1.0, the Wire.send() function was renamed to Wire.write() for consistency with other libraries.

    sketch_jul01a:103: error: ‘class TwoWire’ has no member named ‘receive’

    As of Arduino 1.0, the Wire.receive() function was renamed to Wire.read() for consistency with other libraries.

    Can you give me a clue to fixed?
    The line that is marking me in the code is:
    Wire.send(0); //Ask for Register zero

  35. Hi, Jeremyblum.. I would like to display the MQ-135 air quality gas sensor real time data via SMS on a cell phone. As, it was displaying its data on an LCD but it won’t be sending the same gas sensor data on a cell phone..Here is the code:-
    #include
    LiquidCrystal lcd(2,3,4,5,6,7);
    int GasSensorPin = 0;
    int adcValue = 0;
    float R1=20.0;
    float approx=2373.315;
    float Vout, Rs, count, ratio, GasConc;
    int ppm;
    int x=13;
    /*byte p20[8] = {
    B10000,
    B10000,
    B10000,
    B10000,
    B10000,
    B10000,
    B10000,
    };
    byte p40[8] = {
    B11000,
    B11000,
    B11000,
    B11000,
    B11000,
    B11000,
    B11000,
    };
    byte p60[8] = {
    B11100,
    B11100,
    B11100,
    B11100,
    B11100,
    B11100,
    B11100,
    };
    byte p80[8] = {
    B11110,
    B11110,
    B11110,
    B11110,
    B11110,
    B11110,
    B11110,
    };
    byte p100[8] = {
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    };*/

    void setup()
    {
    Serial.begin(9600);
    lcd.begin(16,2);
    Serial.print(”
    “);
    delay(1000);
    pinMode(x, INPUT);
    }
    void loop()
    {
    if(digitalRead(x) == HIGH)
    {
    Serial.print(“AT+CMGF=1
    “);
    delay(1000);
    Serial.print(“AT+CMGS=”+919571457145”
    “);
    delay(1000);
    Serial.print(“Test SMS -HARSH ARORA
    “);
    delay(1000);
    Serial.write(0x1A);
    delay(1000);
    }
    else
    {
    Serial.print(“AT
    “);
    }
    lcd.setCursor(0,0);
    delay(1000);

    adcValue = analogRead (GasSensorPin);
    Vout = adcValue * (2.00/1000.00);
    Rs = ((5.0*20.0)-(20.0*Vout))/Vout; // R1=20.0
    ratio = Rs/approx;
    GasConc = 146.15*(2.868-ratio)+10;
    ppm=GasConc; //adcValue = analogRead (GasSensorPin);

    {
    lcd.setCursor(0,0);
    if(ratio>=2.868)
    {
    lcd.print(“CO2=”);
    lcd.print(“0”);
    lcd.print(“ppm”);
    }
    else
    {
    lcd.print(“CO2=”);
    lcd.print(ppm);
    lcd.print(“ppm”);
    }
    }
    delay(1000);

  36. hi jeremy,

    thank u for the wonderful tutorials on arduino it helped me alot in my projects.

    i am using arduino Due and i want to do data logging in an usb memory stick connected to the native port of arduino due through an OTG cable. But i don’t know how to do it. so could u plz help me or refer me something so that i could log data into the USB stick. thanx in advance…

  37. Hi Jeremy,
    I want log temperature values with time-stamp for every 0.5 seconds as a CSV file by Using Matlab . How to Synchronize DS1307 RTC with LM35 temp sensor readings and ArduinoUno.
    Also, how can i communicate this wireless instead USB cable

    Thanks for your resourceful tutorials and help in advance..!

  38. I get a error of: (x86)\Arduino\libraries\SD\src\utility\Sd2Card.cpp: In function ‘void spiSend(uint8_t)’:

    Anyone that want to help me, Please

  39. hello jeremy, this tutorial is awesome but could you help me on how to save a finger print template to an sd card … we are making a small project that would store templates on an external memory…thanks

  40. Hello Jeremy. Great tutorial! Very helpful. Thanks for this.
    I have a question. If my SD Card data is in hex and there are more than one data, for example if my SD Card data is :

    0xFE
    0xB2
    0x53
    0xAA

    how can i read the data per line and save it in an array?
    (so that will be arrayData[0] = 0xFE; arrayData[1] = 0xB2; arrayData[3] = 0x53; etc)

  41. Can I just say thanks! I know you are bombarded by questions all the time and I thought it might be refreshing just to offer a little appreciation for all the hard work you put into this. Keep up the awesomeness!

  42. Hi Jeremy,
    Your tutorials are very good! I have an issue with your tutorial 11. on line: if(commandFile) {
    I get an error: jeremy_blum_SD:25: error: expected unqualified-id before ‘if’

    I have compared my sketch to yours on YouTube over and over. I just can’t see the problem.
    One other thing.. I downloaded your sketches from your site. They will not run on my arduino compiler. ver 1.06
    Thanks

Leave a Reply to peter Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement