Arduino SD Card Module Project for Beginners: Read & Write Data

Arduino SD Card Module Project for Beginners: Read & Write Data

Project Overview

This beginner-friendly project demonstrates how to use an SD card module with Arduino to read and write data to an SD card. It covers basic file handling operations like checking file existence, creating a file, writing text to it, and reading data from the file.

Project Goals for Arduino SD Card Module

  • Learn how to connect an SD card module to Arduino for reading and writing data.
  • Understand file handling operations on an SD card using the SD library.
  • Store and retrieve text data from an SD card file.

Required Components for Arduino SD Card Module Project

Here’s a list of components needed to build this project:

ComponentDescriptionLink
Arduino UnoMain microcontroller boardBuy on Amazon
SD Card ModuleInterface for reading/writing SD cardsCheck Availability
MicroSD Card (with Adapter)Stores dataCheck Availability
Jumper WiresConnects components to ArduinoBuy on Amazon
BreadboardFor prototyping connectionsBuy on Amazon

What is an SD Card Module?

An SD card module is a device used to interface an SD card with a microcontroller like Arduino. It enables data storage and retrieval operations on an SD card, allowing you to save sensor data, logs, and other information for later use.

SD Card Module Pinout Overview

PinDescription
VCCPower supply (3.3V or 5V)
GNDGround connection
MISOMaster In Slave Out, connected to Arduino's digital pin 12
MOSIMaster Out Slave In, connected to Arduino's digital pin 11
SCKClock signal, connected to Arduino's digital pin 13
CSChip Select, connected to Arduino's digital pin 10

Circuit Connection for SD Card Module with Arduino

Follow these connections to set up the SD card module with Arduino:

SD Card Module PinArduino PinDetails
VCC5VConnect to 5V power supply
GNDGNDGround connection
MISOD12SPI communication line
MOSID11SPI communication line
SCKD13SPI clock signal
CSD10Chip Select for SD card module

How the Circuit Works

The SD card module is interfaced with Arduino using the SPI protocol. The code checks if an SD card is initialized correctly, creates a file if it doesn't exist, writes text data to the file, and reads it back to verify the content. The data is displayed in the Serial Monitor.

Arduino Code for SD Card Module

This code demonstrates reading and writing to an SD card using an SD card module. Copy and upload this code to your Arduino Uno.


#include 
const int chipSelect = 10;
File myFile;

void setup() {
  Serial.begin(9600); // Initialize serial communication
  while (!Serial); // Wait for Serial Monitor

  check_and_create_file();
  write_text();
}

void loop() {
  // Nothing happens after setup finishes
}

void check_and_create_file() {
  Serial.print("Initializing SD card...");

  // Check if the SD card is available
  if (!SD.begin(chipSelect)) {
    Serial.println("Initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");

  if (SD.exists("data_log.txt")) {
    Serial.println("data_log.txt exists.");
  } else {
    Serial.println("data_log.txt doesn't exist.");
    Serial.println("Creating data_log.txt...");
    myFile = SD.open("data_log.txt", FILE_WRITE);
    myFile.close();

    if (SD.exists("data_log.txt")) {
      Serial.println("data_log.txt created.");
    } else {
      Serial.println("data_log.txt creation failed.");
    }
  }
}

void write_text() {
  myFile = SD.open("data_log.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to data_log.txt...");
    myFile.println("Testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("Error opening data_log.txt");
  }

  myFile = SD.open("data_log.txt");
  if (myFile) {
    Serial.println("data_log.txt content:");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("Error opening data_log.txt");
  }
}

Steps to Upload Arduino Code

  • Connect your Arduino to the computer using a USB cable.
  • Open the Arduino IDE and paste the code into a new sketch.
  • Select the correct board (e.g., Arduino Uno) and port from the "Tools" menu.
  • Click the "Upload" button to transfer the code to the Arduino.

Check Output for SD Card Module

  • Open the Serial Monitor (Ctrl + Shift + M) after uploading the code.
  • Observe the messages for SD card initialization, file creation, and data writing.
  • The content of the "data_log.txt" file will be displayed in the Serial Monitor.

Troubleshooting Tips for SD Card Module

  • SD Card Initialization Failed: Ensure proper connections and check if the SD card is formatted correctly (FAT16 or FAT32).
  • File Not Created: Verify that the SD card has sufficient space and is not write-protected.
  • Data Not Displayed: Check if the data was written correctly and if the file opened successfully.

Suggestions for Beginners

Start by testing the SD card module with basic read and write operations. Once you’re comfortable with file handling, explore data logging from sensors for more complex projects.

Recommended Book for Learning Arduino

Arduino Programming for Absolute Beginners - This book provides easy, step-by-step instructions, making it ideal for beginners who want to learn Arduino programming.

For more Arduino tutorials, visit MechatronicsLab.net, where you’ll find resources on Arduino, ESP8266, ESP32, and Raspberry Pi.

No comments

Theme images by Dizzo. Powered by Blogger.