Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Mechatronics LAB
Published © MIT

Simple SD Card/Read Write– Arduino Workshop

In this project, we will connect up an SD Card to an Arduino and by using the SD.h library to access the card, we will create a new file on

BeginnerFull instructions provided1 hour1,724
Simple SD Card/Read Write– Arduino Workshop

Things used in this project

Hardware components

Arduino UNO REV3
×1
Resistors
×1
SD Card & Breakout
×1
connecting wire
×1
Breadboard
×1

Story

Read more

Schematics

Simple SD Card/Read Write– Arduino Workshop

Code

Code snippet #1

Plain text
#include <SD.h>

File File1;

void setup() {
  Serial.begin(9600);
  while (!Serial) { } // Wait for the serial port to connect (for Leonardo only)
  
  Serial.println("Initializing the SD Card...");
  if (!SD.begin()) {
    Serial.println("Initialization Failed!");
    return;
  }
  Serial.println("Initialization Complete.\n");
  
  Serial.println("Looking for file 'testfile.txt'...\n");
  if (SD.exists("testfile.txt")) {
    Serial.println("testfile.txt already exists.\n");
  } else {
    Serial.println("testfile.txt doesn't exist.");
    Serial.println("Creating file testfile.txt...\n");
    File1 = SD.open("testfile.txt", FILE_WRITE);
    File1.close();
  }
  
  Serial.println("Writing text to file...");
  File1 = SD.open("testfile.txt", FILE_WRITE);
  if (File1) {
    for (int i = 1; i <= 10; i++) {
      File1.println("Test Line " + String(i));
    }
    Serial.println("Text written to file.\n");
  }
  File1.close();
  
  Serial.println("Reading contents of testfile.txt...");
  File1 = SD.open("testfile.txt");
  if (File1) {
    while (File1.available()) {
      Serial.write(File1.read());
    }
    File1.close();
  } else {
    Serial.println("Error opening testfile.txt");
  }
  
  Serial.println("\nDeleting testfile.txt...\n");
  SD.remove("testfile.txt");
  if (SD.exists("testfile.txt")) {
    Serial.println("testfile.txt still exists.");
  } else {
    Serial.println("testfile.txt has been deleted.");
  }
}

void loop() {
  // Nothing to do here
}

Credits

Mechatronics LAB
75 projects • 45 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments