SD Card Module with Arduino: How to Read/Write Data

SD Card Module with Arduino: How to Read/Write Data © GPL3+

In this tutorial, you’ll learn how to use SD and micro SD cards with Arduino in a simple project to measure the environment temperature.

  • 751 views
  • 0 comments
  • 2 respects

Components and supplies

Apps and online services

About this project

You can read this and other amazing tutorials onElectroPeak's official website

Overview

Storing data is one of the most important parts of every project. There are several ways to store data according to the data type and size. SD and micro SD cards are one of the most practical ones among the storage devices, which are used in devices such as mobile phones, minicomputers and etc.

In this tutorial, you’ll learn how to use SD and micro SD cards with Arduino.

In the end, as a simple project, you will measure the environment temperature every hour and store it on the SD card.

What You Will Learn

  • How to use SD and micro SD card
  • Writing data on SD card
  • Reading data from SD card

What is SD and Micro SD Card Module?

The SD and micro SD card modules allow you to communicate with the memory card and write or read the information on them. The module interfaces in the SPI protocol.

To use these modules with Arduino you need the SD library. This library is installed on the Arduino application by default.

NoteThese modules can not handle high-capacity memory cards. Usually, the maximum identifiable capacity of these modules is 2GB for SD cards, and 16GB for micro SD cards.

Important SD Module Library Commands

A brief explanation of practical SD library’s commands is provided in the following table:

*file is an instance from File class.

You can find more information about the SD library Here.

How to Use SD and Micro SD Card with Arduino?

TipThe module used in this tutorial is the micro SD module, however, you can use the code and tutorial for SD modules as well.

Circuit

Using this module is very simple and its configuration is as follows:

Code

Writing data on SD card with Arduino:

#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("This is a test file :)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++) {
myFile.println(i);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}

The result of the above code execution:

Reading data from SD card with Arduino

#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}

The result of the above code execution:

Project: Save Temperature Data on a micro SD using DS3231 Module

You can find DS3231 here. In addition to the IC clock and calendar, this module also has a temperature sensor.

Circuit

Code

To work with the DS3231 module, you must first add the library (Sodaq_DS3231.h) to the Arduino application.

/*
Save temperature in SD/microSD card every hour with DS3231 + SD/microSD module + Arduino
modified on 15 Apr 2019
by Mohammadreza Akbari @ Electropeak
https://electropeak.com/learn/
*/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"
File myFile;
DateTime now;
int newHour = 0;
int oldHour = 0;
void save_temperature() {
myFile = SD.open("temp.txt", FILE_WRITE);
now = rtc.now();
myFile.print(now.hour());
myFile.print(":");
myFile.print(now.minute());
rtc.convertTemperature(); //convert current temperature into registers
myFile.print(",");
myFile.println(rtc.getTemperature()); //read registers and save temperature on deg C
myFile.close();
}
void setup ()
{
Wire.begin();
rtc.begin();
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
now = rtc.now();
oldHour = now.hour();
}
void loop ()
{
now = rtc.now();
newHour = now.hour();
if (oldHour != newHour) {
save_temperature();
oldHour = newHour;
}
}

After storing the temperature at different times of the day, you can draw this information into Excel using the chart.

Draw a chart in Excel:

To do this, follow these steps:

Connect the SD card to your PC.

Enter the Excel software and select the From Text option from the data window and select the file from your memory card.

Select the cells and draw a diagram with them.

What’s Next?

  • Create an entry/exit control device. Using the RFID module and Arduino, save entrance and exit time for several persons on the memory card. (Consider an RFID card for each person)
  • If you find this tutorial helpful and interesting please like us on Facebook.

Code

code 1Arduino
Writing data on SD card with Arduino.
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("This is a test file :)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++) {
myFile.println(i);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
Code 2Arduino
Reading data from SD card with Arduino
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
code 3Arduino
Code for project
/*
Save temperature in SD/microSD card every hour with DS3231 + SD/microSD module + Arduino
modified on 15 Apr 2019
by Mohammadreza Akbari @ Electropeak
https://electropeak.com/learn/
*/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"
File myFile;
DateTime now;
int newHour = 0;
int oldHour = 0;
void save_temperature() {
myFile = SD.open("temp.txt", FILE_WRITE);
now = rtc.now();
myFile.print(now.hour());
myFile.print(":");
myFile.print(now.minute());
rtc.convertTemperature(); //convert current temperature into registers
myFile.print(",");
myFile.println(rtc.getTemperature()); //read registers and save temperature on deg C
myFile.close();
}
void setup ()
{
Wire.begin();
rtc.begin();
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
now = rtc.now();
oldHour = now.hour();
}
void loop ()
{
now = rtc.now();
newHour = now.hour();
if (oldHour != newHour) {
save_temperature();
oldHour = newHour;
}
}

Comments

Similar projects you might like

SD Card Temperature Data Logger

Project tutorial by 3 developers

  • 10,267 views
  • 0 comments
  • 10 respects

Blynk Terminal to SD Card

Project tutorial by Andrei Florian

  • 4,558 views
  • 0 comments
  • 16 respects

SD Card Doorbell

Project tutorial by Projecter

  • 2,728 views
  • 0 comments
  • 7 respects

Arduino Fingerprint Attendance System w/ Cloud Data Storage

Project tutorial by ElectroPeak

  • 8,015 views
  • 5 comments
  • 42 respects

Programming Nextion LCD Without SD Card

by Tamer Savaşcı

  • 16,596 views
  • 5 comments
  • 7 respects
Add projectSign up / Login