Data logging is a crucial aspect of many IoT, environmental monitoring, and sensor-based projects. With an Arduino and an SD card module, you can store sensor data, timestamps, and other readings for future analysis. This guide will show you how to connect an SD card module to Arduino, write and read data, and troubleshoot common issues.
1. What You Need
✅ Arduino Board (Uno, Mega, Nano, etc.)
✅ Micro SD Card Module (SPI-Based)
✅ MicroSD Card (FAT32 formatted, 2GB–32GB recommended)
✅ Jumper Wires
✅ Sensors (Optional, e.g., DHT11, BMP280, etc.)
🔹 Note: The SD module communicates with Arduino using SPI (Serial Peripheral Interface).
2. Wiring the SD Card Module to Arduino
SD Card Module Pin | Arduino Uno Pin | Arduino Mega Pin |
---|---|---|
VCC | 5V | 5V |
GND | GND | GND |
MISO | 12 (SPI MISO) | 50 (SPI MISO) |
MOSI | 11 (SPI MOSI) | 51 (SPI MOSI) |
SCK | 13 (SPI SCK) | 52 (SPI SCK) |
CS (Chip Select) | 10 (SPI CS) | 53 (SPI CS) |
3. Installing the Required Library
- Open Arduino IDE.
- Go to Sketch → Include Library → Manage Libraries.
- Search for SD and install the SD Library by Arduino.
4. Initializing the SD Card (Test if SD Module Works)
Use the following basic test code to check if the SD module is working:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // Set CS pin for SD card
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card is ready.");
}
void loop() {
// Nothing here
}
What This Code Does:
✔ Initializes the SD module.
✔ Prints "SD card is ready" if detected.
✔ Prints "SD card initialization failed!" if there's an error.
🔹 Troubleshooting:
✅ Check that the SD card is FAT32 formatted.
✅ Ensure correct wiring (MISO, MOSI, SCK, and CS).
✅ Try another CS pin (e.g., 4 instead of 10) and update the code.
5. Writing Data to the SD Card
This example creates a file (data.txt
) and writes sensor readings to it.
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
void setup() {
Serial.begin(115200);
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
return;
}
File dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Hello, this is a test log!");
dataFile.close();
Serial.println("Data written to SD card.");
} else {
Serial.println("Failed to open file.");
}
}
void loop() {
// Nothing here
}
What This Code Does:
✔ Creates or opens data.txt
.
✔ Writes "Hello, this is a test log!" to the file.
✔ Closes the file after writing.
🔹 File Handling Functions:
✔ SD.open("filename.txt", FILE_WRITE);
→ Open file for writing.
✔ dataFile.println("Text");
→ Write data to file.
✔ dataFile.close();
→ Close the file to save data.
6. Reading Data from the SD Card
Use this code to read and display data from the SD card.
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
void setup() {
Serial.begin(115200);
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
return;
}
File dataFile = SD.open("data.txt");
if (dataFile) {
Serial.println("Reading file...");
while (dataFile.available()) {
Serial.write(dataFile.read()); // Print file content
}
dataFile.close();
} else {
Serial.println("Failed to open file.");
}
}
void loop() {
// Nothing here
}
What This Code Does:
✔ Opens data.txt
for reading.
✔ Prints the contents of the file to Serial Monitor.
🔹 Tip: Make sure the file exists before reading to avoid errors.
7. Logging Sensor Data to the SD Card
This example logs temperature and humidity data from a DHT11 sensor.
Required Components
✅ DHT11 Temperature & Humidity Sensor
✅ DHT Library (Adafruit DHT Sensor Library)
Wiring for DHT11 Sensor
DHT11 Pin | Arduino Pin |
---|---|
VCC | 5V |
GND | GND |
DATA | 2 |
Code: Logging DHT11 Data to SD Card
#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#define DHTPIN 2 // Pin connected to DHT sensor
#define DHTTYPE DHT11 // Define sensor type (DHT11 or DHT22)
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 10;
void setup() {
Serial.begin(115200);
dht.begin();
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
return;
}
}
void loop() {
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temp) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
File dataFile = SD.open("log.txt", FILE_WRITE);
if (dataFile) {
dataFile.print("Temperature: ");
dataFile.print(temp);
dataFile.print(" °C, Humidity: ");
dataFile.print(humidity);
dataFile.println(" %");
dataFile.close();
} else {
Serial.println("Failed to write to SD card.");
}
Serial.print("Logged: Temp = ");
Serial.print(temp);
Serial.print(" °C, Humidity = ");
Serial.println(humidity);
delay(2000); // Log every 2 seconds
}
What This Code Does:
✔ Reads temperature & humidity from the DHT11 sensor.
✔ Logs the data in log.txt
on the SD card.
✔ Prints the values to Serial Monitor.
8. Troubleshooting Tips
Problem: SD card not detected?
✅ Ensure the SD card is FAT32 formatted.
✅ Double-check MISO, MOSI, SCK, and CS wiring.
✅ Try a different SD card (some cards are incompatible).
Problem: Data not saving?
✅ Always close the file using dataFile.close();
.
✅ Ensure enough storage space is available on the SD card.
9. Practical Applications
✔ Weather Monitoring System – Log temperature, humidity, and air pressure.
✔ Energy Monitoring – Record voltage and current readings over time.
✔ GPS Tracking – Store latitude and longitude data from a GPS module.
✔ Event Logging – Record sensor triggers or button presses.
10. Conclusion
Using an SD card module with Arduino is a great way to store sensor data, log events, or create a portable data logger. With SPI communication, you can easily read and write files, making it ideal for IoT and automation projects.