กลับหน้าหลัก
views
Arduino SD Card Basics: How to Save and Read Data
Last updated on

Arduino SD Card Basics: How to Save and Read Data


Arduino SD Card Basics: How to Save and Read Data

Many Arduino projects need to store data for later use, such as recording temperature, humidity, or sensor readings. SD Card Module makes this task straightforward.

Circuit diagram showing Arduino connected to SD Card Module on breadboard with 6 clear wiring connections

Required Components

  • Arduino UNO R3
  • SD Card Module (Micro SD type)
  • SD Card 1-16 GB (formatted as FAT)
  • Breadboard MB-102 830 points
  • Jumper wires M-M 40 pcs
  • Jumper wires M-F 40 pcs

How to Wire Arduino with SD Card Module

SD Card Module communicates with Arduino via SPI Interface. Wiring connection:

Arduino PinSD Card Module
5VVCC
GNDGND
Pin 4CS
Pin 11MOSI
Pin 12MISO
Pin 13SCK
Pin mapping table showing Arduino pins connected to SD Card Module pins

Preparing SD Card Before Use

This step is critical. Skipping it will cause the code to fail.

  1. Insert SD Card into computer
  2. Format to FAT or FAT32
  3. Create an empty file named test.txt in the SD Card
  4. Insert SD Card into SD Card Module

Example Code: Writing and Reading Data from SD Card

#include <SD.h>
#include <SPI.h>

const int chipSelect = 4;

void setup() {
  Serial.begin(9600);
  
  // Initialize SD Card
  if (!SD.begin(chipSelect)) {
    Serial.println("SD Card initialization failed!");
    while (1);  // Stop if error
  }
  Serial.println("SD Card ready");
  
  // Write data to file
  File myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("Hello from Arduino");
    myFile.println("Test Line 2");
    myFile.close();
    Serial.println("Write complete");
  } else {
    Serial.println("Cannot open file");
  }
  
  // Read data from file
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("=== File Contents ===");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("test.txt not found");
  }
}

void loop() {
  // Do nothing
}

Upload the code, then open Serial Monitor to see results at 9600 baud.

Serial Monitor screen showing successful write and read operations from SD Card

Reference Video

No video available in source article

Common Commands Summary

CommandFunction
SD.begin(pin)Initialize SD Card
SD.open(“filename”, FILE_WRITE)Open file for writing
SD.open(“filename”)Open file for reading
myFile.println(data)Write new line
myFile.print(data)Append data
myFile.read()Read character by character
myFile.close()Close file

Project Ideas to Extend

After understanding the basics, try these applications:

  • Record temperature and humidity from DHT22 sensor to SD Card every 5 minutes
  • Create a light data logger using LDR to store readings throughout the day
  • Log accelerometer data for vibration analysis

The SD library comes bundled with Arduino IDE, no additional installation needed. Try adapting this to your own projects.

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...