In this project, we will connect an SD Card to an Arduino and use the `SD.h` library to:
1. Create a new file on the SD card.
2. Write some text to that file.
3. Print out the contents of the file.
4. Delete the file.
This tutorial will teach you the basic concepts of accessing an SD card and reading and writing files on it.
Required Components
1. Arduino
2. Resistors
5. Breadboard
**Important Note:** Never connect the Arduino’s output pins directly to the SD card without first dropping the voltage from 5V to 3.3V, as this will damage the SD card.
Circuit Diagram
- **Digital Pin 12** on the Arduino connects directly to **Pin 7 (DO)** on the SD card.
- **Digital Pins 13, 11, and 10** go via resistors to drop the logic levels to 3.3V.
- The remaining connections supply 3.3V to power the SD card via **Pin 4** and ground **Pins 3 and 6**.
Refer to the datasheet for your specific SD card breakout board as pinouts might differ.
Code
#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
}
Instructions
1. Ensure your SD card is freshly formatted in the FAT or FAT32 format. For Mac users, ensure the partition scheme is set to “Master Boot Record.”
2. Connect your SD card to the Arduino following the circuit diagram.
3. Upload the code to the Arduino.
4. Open the Serial Monitor to see the output.
Expected Output
If everything goes well, the Serial Monitor will display:
Initializing the SD Card...
Initialization Complete.
Looking for file 'testfile.txt'...
testfile.txt doesn't exist.
Creating file testfile.txt...
Writing text to file...
Text written to file...
Reading contents of testfile.txt...
Test Line 1
Test Line 2
Test Line 3
Test Line 4
Test Line 5
Test Line 6
Test Line 7
Test Line 8
Test Line 9
Test Line 10
Deleting testfile.txt...
testfile.txt has been deleted.
Additional Resources
For more detailed information and advanced topics, consider referring to the book **"Beginning Arduino."**
Comments