Sensors/Modules

SD Card Module with Arduino : Data Logger

Many times it happens that we need to log the sensor’s data somewhere so that we can refer the data afterwards. In many IOT projects we need to store the data from devices and log them with time and other references for future use or when there is a need to update it to server. For this purpose we can use a SD Card module for logging the data.

This module is very easy to use and is also very cheap.

Here, Arduino creates a file in an SD card to write the data and save it using a SD library. You will find different types of SD Card module but all works in the same fashion. It works using the SPI Communication Protocol

Connections:

Since, different boards have different SPI pins so, the connection alter with the board used. Here is the connection table given for Arduino Uno and Arduino Mega.

SD card module  With Arduino Uno With Arduino Mega
VCC 3.3V or 5V (check module’s datasheet) 3.3V or 5V (check module’s datasheet)
CS 4 53
MOSI 11 51
CLK 13 52
MISO 12 50
GND GND GND

 

 

Preparing SD Card:

The first thing you should do, when using the SD card module with Arduino is to format the SD card as FAT16 or FAT32.Also,you can use SD card formatter software to do the same.

Step to be followed for micro SD Card formatting:

1) To format the micro SD card, insert it in your computer. Go to My Computer and right click on the SD card. Select Format as shown below.

2) A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.

Testing the SD card module

Insert the formatted SD card in the SD card module.

Connect the SD card module to the Arduino as shown in the circuit schematics below or check Pin Wiring in previous section.

To make sure everything is wired correctly and the SD card is working properly, in the Arduino IDE window go to FileExamples > SD > CardInfo.

Upload the code to your Arduino board. Make sure you have the right board and COM port selected.

Open the Serial Monitor at a baud rate of 9600 and you should see your SD card information.

If everything is working properly you’ll see a similar message on the serial monitor.

Read and write to the SD card

The SD library provides useful functions for easily write in and read from the SD card.

To write and read from the SD card, first you need to include the SPI and SD libraries:

#include <SPI.h>

#include <SD.h>

 

You also have to initialize the SD card module at the Chip Select (CS) pin – in our case, pin 4.

SD.begin(4);

 

To open a new file in the SD card, you need to create a file object that refers to your data file. For example:

dataFile = SD.open("data.txt", FILE_WRITE);

 

The first parameter of this function is the name of the file, data.txt, and the FILE_WRITE argument enables you to read and write into the file.

This line of code creates a file called data.txt on your SD card. If the data.txt file already exists,  Arduino will open the file instead of creating another one.

To write data to the currently open file, you use:

dataFile.write(data);

 

In which the dataFile is the file object created previously and the data is what you want to write in the file.

You can also use the print() or println() functions to print data into the file:

dataFile.print(data);

dataFile.println(data); // followed by a new line

 

To read the data saved on your file:

dataFile.read();

 

You can only write within a file at once, so you need to close a file before proceeding to the next one. To close the data.txt file we’ve just created:

SD.close("data.txt");

 

The argument of this function is the file you want to close, in this case data.txt.

For a complete sketch on how to read and write, in your Arduino IDE go to FileExamples > SD > ReadWrite.

 

 

Show More

Related Articles

Leave a Reply

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

Back to top button
Close