Introduction
The Arduino microcontroller board is widely used for various DIY projects, including data logging, sensor monitoring, and automation. One of the critical aspects of many Arduino projects is storing data for later retrieval or analysis.
In this article, we will discuss how to interface an “SD card module with an Arduino” board to store and retrieve data efficiently.
What is SD Card Module?
An SD card module is a small electronic device that interfaces with an SD (Secure Digital) memory card, providing a convenient way to store and retrieve data from the card. The module typically consists of an SD card slot, voltage regulator, and level shifter circuitry that allows it to communicate with a microcontroller board, such as an Arduino.
Hardware Components
To interface SD Card with Arduino, you’ll need the following hardware components to get started:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
SD Card Module | – | 1 |
Jumper Wires | – | 1 |
SD Card Module Pinout
Pin Label | Pin Description |
GND | Ground connection. Connect to any of the GND pins on the Arduino board |
3.3 V | This is the 3.3 V output of the onboard linear voltage regulator, which accepts 5 V and provides the necessary 3.3 V for the SD Card as well as an optional output for other peripherals |
+5 V | Connect the 5 V pin on the Arduino to this pin. 5 V is the input supply for the SD card module. |
CS | Chip select pin. This pin is the output pin of the Arduino and the input pin for the SD card module. |
MOSI | Master Out Slave In Pin. This pin is the output pin of the Arduino and the input pin for the SD card module. |
SCK | SPI Clock line. This pin is the output pin of the Arduino and the input pin for the SD card module. |
MISO | SPI MISO line. This pin is the input pin of the Arduino and the output pin for the SD card module. |
GND | Ground connection |
SD Card Arduino Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | SD Card Interface |
---|---|
5V | 5V |
GND | GND |
D4 | CS |
D11 | MOSI |
D13 | SCK |
D12 | MISO |
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Installing Libraries
Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.
SD Card Test Code
Now copy the following code and upload it to Arduino IDE Software.
// include the SD library:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;
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("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
while (1);
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.println();
Serial.print("Card type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
while (1);
}
Serial.print("Clusters: ");
Serial.println(volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(volume.blocksPerCluster());
Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
Serial.print("Volume size (Kb): ");
Serial.println(volumesize);
Serial.print("Volume size (Mb): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Gb): ");
Serial.println((float)volumesize / 1024.0);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}
Code Explanation
This Arduino code is used to interface an SD card module with an Arduino board and test if the SD card is working properly.
The code begins with including the required libraries for using the SD card module, which is SPI.h and SD.h. Then, variables are declared using the SD utility library functions, which include Sd2Card, SdVolume, and SdFile.
Next, the chip select pin is set to 4 as an example, but it can be changed to match the SD shield or module used. In the setup function, serial communication is initialized, and the code tests if the SD card is working correctly by using the initialization code from the utility libraries. If the initialization fails, the code displays an error message and waits for the user to resolve the issue. If the initialization succeeds, the code displays the type of SD card, the number of clusters, blocks per cluster, and the total blocks.
Afterward, the code prints the type and size of the first FAT-type volume and lists all the files found on the SD card with their names, dates, and sizes in bytes. In the loop function, the code continuously runs until the Arduino board is turned off or reset.
Applications
Here are some applications of the SD card module:
- Data logging for various sensors such as temperature, humidity, pressure, and GPS
- Storing multimedia files such as images, audio, and video
- Portable data storage for mobile devices and cameras
- Robotics and automation systems for saving configurations, logs, and data
- Embedded systems for storing configuration files, firmware, and log files
- Music players and sound recorders
- Gaming consoles and devices for saving game data and progress
- Security systems for storing surveillance footage
- Educational projects for teaching data storage and retrieval concepts.
Conclusion.
SD card module with an Arduino board opens up new possibilities for data storage and retrieval in DIY projects. With the SD card module, you can store large amounts of data, such as sensor readings, without the need for expensive external storage devices.