Create, Write, Read and Delete file into SD Card using SD Card Adapter Module in Arduino

in utopian-io •  7 years ago  (edited)

The SD Card Module is used for transferring data to and from the memory card. It works either on 3.3V or 5V, most of Arduino boards now use logic level shifter to accommodate 3.3V modules such SD Card Adapter.

db630567-53c0-4084-86b8-139fb2b4546e.jpg
image source

What Will I Learn?

This tutorial aims to:

  • Interfacing SD Card Adapter Module to Arduino Uno
  • Create, Write, Read and Delete file into SD Card

Software Requirements

Hardware Requirements

  • Arduino Uno
  • SD Card Adapter Module
  • SD Card
  • Connecting Wires

Difficulty

  • Basic

Tutorial Contents

Step 1: Physical Connection

The SD Card Adapter is a module that uses SPI (Serial Peripheral Interface) to communicate with Arduino. Which by default, we need to use the hardware SPI pins of our Arduino Uno from pin 11-13.
Untitled Sketch_bb.jpg

circuit developed using fritzing.

Connect the following:

SD Card Adapter ModuleArduino Uno
SCKpin 13
MISOpin 12
MOSIpin 11
CSpin 4
3V33.3V
GNDGND
Step 2: Software Connection
  • Connect the USB Cable from Arduino Uno to a PC/Laptop
  • Open your Arduino IDE
    Capture.PNG
  • Change your board to Arduino Uno: Go to Tools > Board: > Arduino/Genuino Uno
    1.png
  • Also, change the Port settings: Go to Tools > Port: > COM6 (Arduino/ Genuino Uno)
    2.png
  • And install the SD library this makes initialization, reading and writing of the SD card easy. Go to Sketch > Include Library > Manage Libraries
    3.png
  • A new window will appear and search for "SD" then click Install.
    4.png
Step 3: The Code

Include the SD library to use the functions inside on it. And also the SPI to communicate the module to the Arduino Board

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

Create a variable myFile with data type File (a function inside the library SD)

File myFile;

Code inside the setup() function:

void setup() {
}

Open serial communications and wait for port to open and connect

Serial.begin(9600); //to open the serial communication
 while (!Serial) {
    ;  //wait until its connected
}

Initializing the SD Card

Serial.print("Initializing SD card. . .");
  if (!SD.begin(4)) {      //This opens the communication of CS pin to the Arduino Board and if it is not connected the initialization will failed.
    Serial.println("Initialization failed!");
    return;
  }
// And if successful this will print.
  Serial.println("Initialization done.");

Create and open the file, note that only one file can be open at a time, so you must close the file after opening it.

 Serial.println("Creating test.txt. . ." );
myFile = SD.open("test.txt", FILE_WRITE);  //test.txt is the file name to be created and FILE_WRITE is a command to create file.
myFile.close();  //Closing the file

Check if the file is successfuly created

if (SD.exists("test.txt")) { // If the file test.txt exist.
    Serial.println("The test.txt exists.");
  } else { // And if not
    Serial.println("The test.txt doesn't exist.");
  }

If the file was successfully created, we can now write on it.

 if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("Testing 1, 2, 3."); //Input the *Testing 1, 2, 3.* inside the file.
    myFile.close(); // Close the file after opening
    Serial.println("Done."); //Display this if the file was successfully written.
  } 
else { // And if not display error message.
   Serial.println("Error opening test.txt");
  }

Re-open the file to read the text inside on it.

myFile = SD.open("test.txt"); \\Open the file
  if (myFile) {
    Serial.println("test.txt:");
    while (myFile.available()) { // If the file is present execute loop until done.
        Serial.write(myFile.read()); //Reading the text inside the file
    }
    myFile.close(); //Close the file after opening
  } else { //Display message if it was unsuccessfully opened.
    Serial.println("Error opening test.txt");
  }

To delete the file

  Serial.println("Removing test.txt...");
  SD.remove("test.txt"); // remove is a function in SD library to delete a file
  if (SD.exists("test.txt")) { //If the file still exist display message exist
    Serial.println("The test.txt exists.");
  } else { //If the file was successfully deleted display message that the file was already deleted.
    Serial.println("The test.txt doesn't exist.");
  }

There is no code inside the loop since we have nothing to do after the setup.

void loop() {
}
  • After copying the code, compile it and click the check button to Verify or press Ctrl+R
    6.png
  • And then check the mesage below if it has no error
    5.png
  • Then you can now upload the program, just click the arrow-sign or press Ctrl+U
    7.png
  • After uploading click the serial monitor to see the output or press Ctrl+Shift+M
    10.png
  • And a new window will pop-up that displays the output
    9.PNG
My Circuit

26856212_1805741639437612_1978402376_n.jpg

Curriculum

You can also check my previous tutorials in:

Thanks for reading and Happy coding!



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Great contribution sir !

Great Contribution :)

Thank you for the contribution. It has been approved.

Important Note: Explaining the code snippets would make tutorial better and differ from just copy-paste tutorial. I would appreciate if you edit this one and being careful with future ones.

Good work!

You can contact us on Discord.
[utopian-moderator]

Thank You sir, and i will do better for the next tutorial.

Hey @kimp0gi I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x