Arduino LESSON 21: Log Sensor Data to an SD Card

In most of our work so far, we have just watched our data go by on the Serial Monitor. In most cases, you will want to have some means to store your data. The easiest way to do this is to use a simple SD card reader. For this example, we use the Virtuabotix SD Card Reader.

SD Card Reader
Arduino connected to a BMP180 pressure sensor and an SD Card Reader

In this tutorial, we will need to have some sensor hooked up so we will have some data to store. We will be using the BMP 180 Pressuer and Temperature sensor from adafruit.  We have a complete tutorial on this sensor  HERE.  You will need to go to that lesson and get the sensor hooked up, the library installed, and the software done. All this is explained step-by-step in the LESSON.

The BMP180 is connected to the arduino as follows:

Connecting Up the BMP180 Pressure and Temperature Sensor
BMP180 Pin Arduino Pin
Vin 5V
GND GND
SCL A5
SDA A4

Once you have the BMP180 connected, test and make sure your code is working, and you are getting good pressure and temperature readings. Once that is working, you are ready to connect your SD card Reader/Writer.

The SD card reader should be connected as follows:

Connecting the SD Card Reader
Sd Card Reader Pin Arduino Pin Details
GND GND Common Ground
3.3 V – (NOT USED)
+5 5V Power
CS 4 Chip Select
MOSI 11 SPI Data
SCK 13 Clock
MISO 12 SPI Data
GND GND Common Ground

In the video we will show step-by-step how to develop the software. You should follow along in the video, and not copy and paste the code below. You will never learn to program if you do not write your own code. The code below is to help you in case you get stuck.

If you have the BMP180 and the SD card connected correctly,  this should create a file called PTData.txt on the card, and write comma delimited data to the file. Note that if the file does not exist on the card, the command:

will create the file. If the file already exists, this command will append data to the existing file. If you want to start with a clean new data set, erase the old PTData file.

When you run the program, you end up with a PTData.txt file on the SD card. When you have finished logging your data, you can pop the card out, put it into your PC, and then import the data into excel. You should now be able to plot, graph or analyze the data using all the powerful features of Excel.

30 thoughts on “Arduino LESSON 21: Log Sensor Data to an SD Card”

  1. In the vertical axis we have temperature or pressure.
    The horizontal axis in the graphs (values 0,100,200,300,400,500)
    what does it stand for?

    Thank u very much for your videos!

    1. That is just the number of points, sequentially. It does not represent a physical parameter. As the chart goes by, you are just looking at the trend in the data. You can include a x axis that is tied to time if you want.

    1. I connected them together on the breadboard. Not sure if they are connected together on the SD component, so went ahead and connected both to ground on the breadboard.

  2. well nice tutorial and i learn allot, but is this sketch working with sd slot come with Ethernet shield .
    thanks for such a good video tutorial.

  3. hello, I like a lot how you explain these concepts!
    I suggest you to make some lessons about ENC28j60 ethernet module, this could be useful for many people.
    Thanks,
    Daniele

  4. Hello Paul
    Your tutorials are really useful. Thank you. I make rockets in a club with the association Planete sciences and CNES in France. Could you make a Arduino tutorial with an accelerometer, ADXL345 for example, of which one records the data on a SD card. These data would be then re-used with matplotlib.
    Thank you for your work

  5. Hi!
    When I puted out my SD card and again put in Arduino can not to write datas again on SD card. Please for help !

    Kindly regards !

    Tomaž

    1. Richard, it looks like the item is out of stock, but has not been discontinued. The thing is, I know that one works and is simple to use. I have not tried the seedstudio one.
      PJM

  6. Hello Paul,

    Thank you for the lesson. May I ask you a question: I have a Raspberry Pi with a Sleepy Pi (Arduino based) and connected Adafruit BME280 sensor. How would you suggest the sensor data can be logged on the SD Card of the Raspberry Pi, in a similar way as you described above?
    Thanks,
    Henk

  7. Hi Paul,
    Our group has hit its first brick wall. The SD card will not initialize. We have searched all the internet for solutions and we are definitely not alone. Have added the pin 10 HIGH etc suggested by some for the uno, but without success. We will save data to the pi instead, its just a shame for some mobile data collection experiments. I will try to get the exact SD card reader/writer you have from Virtuabotix but beware to all, our $1.86 read/writer from ebay is not talking to anyone and we have tried three different SD cards.
    By the by, the file naming system is the 8characters.3characters only
    This course is the best out there, many thanks.
    D&Team.

    1. You might try and simplify the code to just see if you can write and read the SD card. Maybe even try one of the arduino “example” programs that download with the IDE. If those dont work, maybe a bad card reader.
      PJM

  8. I am new with Arduino, and I’m getting weird errors. Do you know how to fix them? They are:
    Arduino: 1.6.6 (Windows 7), Board: “Arduino Nano, ATmega328″sketch\sketch_nov28a.ino.cpp.o: In function setup':
    *******: undefined reference to
    Adafruit_BMP085::begin(unsigned char)’

    sketch\sketch_nov28a.ino.cpp.o: In function loop':

    ******: undefined reference to Adafruit_BMP085::readTemperature()’

    *******: undefined reference to Adafruit_BMP085::readPressure()'

    sketch\sketch_nov28a.ino.cpp.o: In function __static_initialization_and_destruction_0′:

    *******: undefined reference to `Adafruit_BMP085::Adafruit_BMP085()’

    collect2.exe: error: ld returned 1 exit status

    exit status 1
    Error compiling.

    This report would have more information with
    “Show verbose output during compilation”
    enabled in File > Preferences.
    ————————————————————————
    I changed some of the text to *s for security.
    I am in a hurry. Please respond ASAP.
    Thanks,
    JFryer

  9. The VirtuaBotix card reader/writer isn’t available any more, so I bought the Adafruit SD shield. The shield does not have the same pin configuration as the VirtuaBotix, so I’m a little confused as to pinning. There’s no MISO or MOSI; no SCK or chip select (or they’re labeled differently). Any help is appreciated! Thanks

  10. All of the tutorials given by you worked for me .But SD card is not initialising.I had tried with two different SD cards but not initialising.
    Is there any specific format for SD card to be used with arduino card reader?
    Please help me.

    Thanks.

  11. I’m trying to make this program sample at a higher rate (up to 9.6 KHz or more by adjusting the prescale factor). I haven’t been able to get it anywhere close. Any ideas on how to do this or maybe another tutorial?

  12. Thanks Paul for taking the time to make these videos!
    They are much appreciated.
    I have been doing some logging similar to this on sd card, but like the post above, I would like to first set the time on my arduino and then add time stamp to the data as it is being collected. I have yet to figure that out. Without adding rtc to the layout.
    If you have any suggestions I would definitely appreciate the advice.

    Thanks!

  13. Man, you are an Electronics Angel, sent by the Gods of Technology to help us humble mortals navigate through the intricacies of Arduino.

    Thank you very much for your help.

  14. Hi first of all thanks very much for your fantastic effort it is really interesting and usefull and I really apreciate the way you explain.
    write you because I have an issue with this exercise ..
    I don’t know why I try to compile and it always tell me that my sketch is too big and I tried all what came to my mind but I couldn’t find a solution..
    Well I am using an arduino nano that have the same microprocessor as arduino uno…
    Does anybody have an idea??
    Thanks for your help ..

  15. A code to gracefully disconnect the sd card:
    stop=Serial.readString();
    if(stop.equals(“STOP”)){
    mySensorData.close();
    Serial.println(“STOPPED”);
    exit(0);
    }
    In order to disconnect the sd card type “STOP” in the Serial Monitor, then the file will be closed and arduino loop will stop.

    I also think it is better to put
    ‘mySensorData = SD.open(“PTData.txt”, FILE_WRITE);’ on setup() and to put ‘mySensorData.close();’ like in the code I posted above, Now the arduino will open the file once and close it once which is more efficient.

    1. Good approach.
      An alternative – if you want to have an automatic dat logging process – would be to have a timer do the automatic closing for you when a certain time has expired and open new file with a new, automatically created different filename and reset the time to zero.

      An example would be to create an hourly / daily (or whatever timed) data logging process.

  16. Thanks Paul for your very instructive tutorials.

    One question with regard to the SD card hookup:
    In other sources (e.g. Arduino.cc forum) it was suggested to use a logic level shifter to be inserted between Arduino and SD card as it was stated that in spite of the voltage regulator on board the logic signals are not converted. If the level converter isn’t used there might be a potential to fry the SD card module.

    For future experiments I am currently waiting for Micro SD card modules which already have the logic level converter chip on board.

    As I own currently only one of those SD card modules without logic level converter and want to start experimenting already before the new modules arrive it would be good to know if you have some experience with that and what you suggest.

  17. Addit:
    Paul, in your tutorial on youtube you were struggling with the naming of the data file (name length).
    The reason why you have to stay with short names:
    Remember the old DOS times and the 8+3 convention …

    So file names are restricted to max 8 characters before the “.” and 3 characters thereafter.

  18. HIe,

    i was working on the same code but i am facing this issue, when I am using the usb cable with the arduino the code works just fine it gives the data to the computer and store it to the sd card as well but as soon as i detach the usb from system and attach a 9V battery it doesn’t work.
    Kindly help me sorting this issue. (PS i am using arduino uno)

Leave a Reply

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