It is common to use different sensors with microcontrollers in embedded applications. Microcontrollers are equipped with parallel data ports and several serial data communication interfaces that allow interfacing and communication with sensors. There are times when data obtained from sensors needs to be stored for future use. This could just be to record sensor data or get high-level analytics. Many microcontrollers have built-in or built-in EEPROM to store external data, while flash memory always remains reserved for storing the bootloader and firmware. This EEPROM is usually a few kilobytes in size and can only store a limited amount of data.
A practical solution to this problem is to store sensor data on SD or Micro SD cards. SD and Micro SD card modules can be easily interconnected with Arduino (or any microcontroller) via the SPI interface. SD and Micro SD cards facilitate gigabyte storage capacity that is more than enough to record data from any number of sensors or any amount of data.
In this project, we will interface a Micro SD card module with Arduino and use an 8GB Micro SD card to record data from the ADXL345 accelerometer sensor. Before continuing this project, learn how to interface the ADXL345 accelerometer with Arduino. You may also be interested in working on the ADXL345 accelerometer sensor.
SD and Micro SD card modules
SD and Micro SD cards are widely used memory cards. Both are different in technology and origin. SD cards are a bit bulky (24mm x 32mm x 2.1mm) compared to Micro SD cards. Maximum storage capacity is limited to 2 GB. SD cards are commonly used in portable computers, digital cameras, and baby monitors. Micro SD cards are smaller (15mm x 11mm x 1mm). The maximum storage capacity is 16 GB. Micro SD cards are commonly used in cell phones, personal computers, drones, dash cameras, etc.
SD and Micro SD card readers usually have an SPI interface for communicating data with a microcontroller/microcomputer. Except SD card reader reads/writes an SD card and Micro SD card reader reads/writes a Micro SD card, both types of modules interface with Arduino (or any other microcontroller/microcomputer) in a similar way, and the same library on Arduino is used to read or write to both types of modules.
SD Library
Arduino can communicate with SD and Micro SD card readers using its built-in SD library, as both modules use the SPI interface for data communication. The library also supports FAT16 and FAT32 file systems on SD/Micro SD cards, but file names must be in 8.3 format. Only one dot is allowed, although filenames can include PATHs separated by slashes. The library provides the following methods for working with SD/Micro SD cards.
SD.begin : This method initializes the SD/Micro SD card. It takes as a parameter the pin connected to the reader's SS pin.
SD.exists : This method tests the existence of a file or directory on the SD/Micro SD card. It takes the file name as a parameter.
SD.mkdir : This method creates a directory on the SD/Micro SD card. Takes the file name, including the path, as a parameter.
SD.rmdir : This method removes a directory from the SD/Micro SD card. Takes the file name, including the path, as a parameter.
SD.open : This method opens a file for reading or writing. Two parameters are required: file name and mode. The mode determines whether the file should be read or written. The file will open in read mode by default if mode is not specified. If the file is opened for writing, if it does not already exist, it will be created by the specified file name and path.
SD.remove : This method removes a file from the SD/Micro SD card. Takes the file name, including the path, as a parameter.
Many methods from the built-in file class are also used while reading or writing to SD/Micro SD cards. Some of the useful techniques of the built-in file class are as follows.
File Name : This method returns the file name.
File.available : This method checks if a file with a given name exists.
File.read : This method reads the contents of the file.
File.write : This method writes data to the file.
File Size : This method returns the file size in bytes.
File.print : This method prints data to the file.
File.println : This method prints data to the file followed by a carriage return and a newline.
File.close : This method closes the file and ensures that all data written to it is physically saved on the SD/Micro SD card.
Note that until the file.close method is called, data written to a file will not be saved. Therefore, a file must be closed after writing data to it. Otherwise, the recorded data will be lost.
Interfacing SD/Micro SD Card Module with Arduino
The SD/Micro SD card interfaces with the Arduino via the SPI interface. The module has six pins – VCC, GND, MISO, MOSI, SCK and CS. The SPI ports on Arduino UNO are shown in the image below.
Note that the SS pin of the SD/Micro SD card reader must be connected to pin 10 of the Arduino UNO.
Writing data to SD/Micro SD card with Arduino
Interface the SD/Micro SD card reader with the Arduino UNO as described above. Writing data to SD/Micro SD card involves using SD.begin , SD.mkdir (if a new directory is created for the data file), SD.open , File.write , File.print , File. println and File.close . The following example code writes a list of sites belonging to the EEWORLDONLINE electronics engineering network to a text file. Note that the data is written to the SD/Micro SD card in the setup function because it should only be written once in this case. If it was time-stamped sensor data, recording to the SD/Micro SD card would have to be done in the loop function. Also, note that the file class only needs to instantiate one file object.
The result of the above example code is shown below.
Reading data from SD/Micro SD card with Arduino
Reading data from the SD/Micro SD card involves using the SD.begin , SD.open , File.available and File.read methods. Note that the File.read method reads a single line at a time. Needs to be repeated until the desired number of lines are read or until the end of the file.
The result of the above example code is shown below.
Recording sensor data to SD card with Arduino
Now equipped with the knowledge of recording data to SD/Micro SD card using a card reader, we can record sensor data for future use. Let's connect the ADXL345 accelerometer sensor with the Arduino and record ten consecutive acceleration readings on the x, y and z axes in 500 milliseconds.
Required components
- Arduino UNO x1
- SD/Micro SD card reader x1
- SD/Micro SD Card x1
- ADXL345 x1 accelerometer sensor
- Connecting wires/jumper wires
- Test board
Circuit Connections
The SD or Micro SD card reader will interface with the Arduino via the SPI port. The circuit connections between the SD/Micro SD card reader and the Arduino are summarized below.
Arduino UNO | SD/MicroSD card reader |
5V output | CCV |
GND | GND |
13 | SCK |
12 | MISSO |
11 | MOSI |
10 | SS |
The ADXL345 accelerometer interfaces with the Arduino via an I2C port. The circuit connections between ADXL345 and Arduino are shown in the image below.
The final circuit looks like the one shown in the image below.
Arduino Sketch
How it works
Arduino detects acceleration along x, y and z axes with the help of ADXL345 accelerometer sensor. The code runs once and reads the sensor ten times at 500 millisecond intervals. Readings are written to a text file on a Micro SD card and saved.
Result
The following video shows the Arduino recording ten consecutive ADXL345 readings to an 8GB Micro SD card.
The following video shows checking the data recorded on the micro SD card.