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 the card, write some text to that file, print out the contents of that file, then delete the file. This will teach you the basic concepts of accessing an SD card and reading and writing files to it. You will need an SD Card and some way of connecting it to an Arduino. The easiest way is to get an SD/MMC Card Breakout Board from various electronics hobbyist suppliers.Required Component SD Card Arduino:
1.Arduino
2. Resistors
5. Breadboard
Never connect the Arduino’s output pins directly to the SD Card without first dropping the voltage from them from 5V down to 3.3V or you will damage the SD Card
This book will help you to gain more knowledge about Arduino
Beginning Arduino
Digital pin 12 on the Arduino goes straight into pin 7 (DO) on the SD Card. Digital pins 13, 11, and 10 go via the resistors to drop the logic levels to 3.3V. The remaining connections to the SD card supply 3.3V to power the SD card via pin 4 and ground pins 3 and 6. Refer to the datasheet for your particular SD Card breakout board in case the pin outs differ from the circuit board above
Code SD Card Arduino:
#include <SD.h> File File1; void setup() { Serial.begin(9600); while (!Serial) { } // wait for serial port to connect. // Needed 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....."); String dataString; File1 = SD.open("testfile.txt", FILE_WRITE); if (File1) { for (int i=1; i<11; i++) { dataString = "Test Line "; dataString += i; File1.println(dataString); } Serial.println("Text written to file.....\n"); } File1.close(); Serial.println("Reading contents of textfile.txt..."); File1 = SD.open("testfile.txt"); if (File1) { while (File1.available()) { Serial.write(File1.read()); } File1.close(); } // if the file isn't open, pop up an error: else { Serial.println("error opening testfile.txt"); } // delete the file: 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 see here }
No comments