JeremyBlum.com

v3.1

Tutorial 11 for Arduino: SD Cards and Datalogging

| 250 Comments

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.

250 Comments

  1. Pingback: Tutorial 12 for Arduino: RFID Card Reading | JeremyBlum.com

  2. Hi jeremy.
    Am noob in arduino..how do i read data from SD card then display the data on an LCD module??

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

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

  4. OR can i use USB flash drive instead of SD card?
    If can, how can i do it?

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

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

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

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

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

        • 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

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

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

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

    • 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

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

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

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

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

  14. 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?

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

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

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

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

  19. Sorry, I found it.
    If somebody face the same issue, here is the link
    http://forum.arduino.cc/index.php?PHPSESSID=hqnc150imkj8r24cjb4huktig1&topic=6261.0

    • 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!

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

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

  22. 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?

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

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

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

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

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

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

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

  29. Can someone help me accessing SD card without using SD.h and directly form SPI?

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

    • i requirement is to log voltage levels.So i need to use decimal points

    • 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

  31. 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.;)

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

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

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

  34. 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?

  35. 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?

  36. 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?

  37. 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 ?

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

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

  40. Pingback: Tutorial 11 for Arduino: SD Cards and Datalogging | Faweiz Video

  41. 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);

  42. 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…

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

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

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

  46. 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)

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

  48. Pingback: Modulo SD/uSD | Blog de PatagoniaTecnology

  49. Pingback: Arduino Mega Sd Card Problem

Leave a Reply

Required fields are marked *.