Logging data on MicroSD Cards with Arduino

Author : Dinesh Kumar Wickramasinghe

Introduction and Images

Hi Everyone, Here again with a new tutorial. This one is interesting and about saving data on MicroSD cards using Arduino. This tutorial will show you how to save data on MicroSD cards on your Arduino projects. I will use a microSD card module for this. Here are two images of my final project.

Micro SD card Module
Arduino MicroSD Module

You need to use a MicroSD card module in this project. There are different types of MicroSD card modules are available. I will use the one as shown in the above image. This version is very easy to use and cheap. Please note that the file system of your MicroSD card should be FAT16 or FAT32. Otherwise the Arduino SD library won’t be able to read/write on it.

Experiment One

First we will do a simple experiment to write the text "Hello World" in a text file on your microsd card and read it back. Later we will do a bit complex project to log the temperature data.

Use the below schematic to connect your MicroSD card module with Arduino.


Schematic
Arduino MicroSD Module Schematic
Source Code
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("Hello World");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }

  myFile = SD.open("test.txt");
  
  if (myFile) {
    Serial.println("Reading file test.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("error opening test.txt");
  }
}

void loop() {}
                    

The above source code will create a file named "test.txt" on your MicroSD card and save the text “Hello World” in that file. Then read the file back and print the text on Serial monitor.

Once you done connecting the Micro SD card module to Arduino then carefully insert your MicroSD card to the module. Now upload the above code. Open the serial monitor and see the output. You will see an output similar to the below screenshot.

Arduino MicroSD Module Read Text File

If you want, now you can remove the MicroSD card from the module and connect it to your PC and examine the text file.

Experiment Two

In the second experiment, we are going to read and store temperature readings from DS18B20 digital waterproof temperature sensor and save these reading in a .csv file. After that, we will plot these readings using Microsoft excel or any other similar spreadsheet application.

To know more about reading temperature data using DS18B20 sensor, please refer my DS18B20 Temperature data monitor tutorial. It has more information about installing additional libraries (OneWire and Dallas Temperature) for this sensor.

Schematic

Use the same way we connected the micro sd card module in the experiment one. Connect the DS18B20 temperature sensor as below.

Arduino MicroSD Module DS18B20 Temperature Data Logging
Source Code
#include <SPI.h>
#include <SD.h>
#include <OneWire.h> 
#include <DallasTemperature.h>

const int chipSelect = 4;
const int noOfReadings = 10;
#define ONE_WIRE_BUS 2 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin(); 
  while (!Serial) {
    ;
  }
  Serial.println("Initializing SD card...");
  Serial.println("Reading Temperature...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");

  String dataString = "";
  for(int i = 0; i < noOfReadings; i++){
    Serial.print(i+1);
    Serial.print(". Temperature is: ");
    sensors.requestTemperatures();
    Serial.println(sensors.getTempCByIndex(0));
    dataString += sensors.getTempCByIndex(0);
    dataString += ",";
    delay(2000);
  }
  Serial.println("Temperature reading done!");
  Serial.println("Saving data to SD card!");
  
  File dataFile = SD.open("temData.csv", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println("Finished!");
  }
  else {
    Serial.println("error opening temData.csv");
  }
}

void loop() { }
                    

This source code will read temperature data every 10 seconds and save 10 readings on your MicroSD card.

After getting 10 temperature readings, it will stop data saving and close the file. You can monitor the readings using the serial monitor.

Please note that, this time we are saving data as on a .csv file. So that later we can open this file using Microsoft Excel or similar software and generate a chart.

Upload the above code and keep your device power up for about 2 minutes to get temperature readings (You can monitor the readings on serial monitor. You will get the finished message once data logging done).

Once the data logging done, you will get the finish message on Serial monitor as the below screenshot.

Arduino MicroSD Module DS18B20 Temperature Data Logging

If you want to save more temperature readings, increase the value of the counter variable in the below line of code.

const int noOfReadings = 10;
                    
Plotting Data

Once the data logging done, (When you get the finished message on Serial Monitor) remove the MicroSD card and connect your MicroSD card to your PC using a card reader. Then open the .csv file using Microsoft Excel (or any other compatible spreadsheet software).

Select the data range and click on chart icon. Select the line chart type and click Ok. Now you can see the temperature readings as a line chart.

Below screenshot shows my readings plotted on Google sheet.

Arduino MicroSD Module DS18B20 Temperature Data Graph
Conclusion

Hope you enjoyed this project and this method is very important when we need to store many data in our Arduino projects. If you faced any issues and if you want to know more information, please put them as a comment. Have a nice day.

Comments Area
Add your questions and comments here. Comments will be published after the admin approval
Published By : kemal
Added Date :1/29/2020 12:00:45 AM
It works, thanks ! What changes should be made to the codes when multiple sensor is used?
Published By : Rupali Saini
Added Date :5/17/2019 1:02:54 PM
its showing card not present what should I do?
Published By : Rakesh
Added Date :2/25/2019 10:18:41 PM
Worked perfectly !!! thanks a lot. Hope to see more such projects.

 

Similar Projects

Go Top