2 SD card basic file example
4 This example shows how to create and destroy an SD card file
6 * SD card attached to SPI bus as follows:
7 ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
8 and pin #10 (SS) must be an output
9 ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
10 and pin #52 (SS) must be an output
11 ** Leonardo: Connect to hardware SPI via the ICSP header
18 This example code is in the public domain.
25 // change this to match your SD shield or module;
26 // Arduino Ethernet shield: pin 4
27 // Adafruit SD shields and modules: pin 10
28 // Sparkfun SD shield: pin 8
29 const int chipSelect = 4;
33 // Open serial communications and wait for port to open:
36 ; // wait for serial port to connect. Needed for Leonardo only
40 Serial.print("Initializing SD card...");
41 // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
42 // Note that even if it's not used as the CS pin, the hardware SS pin
43 // (10 on most Arduino boards, 53 on the Mega) must be left as an output
44 // or the SD library functions will not work.
47 if (!SD.begin(chipSelect)) {
48 Serial.println("initialization failed!");
51 Serial.println("initialization done.");
53 if (SD.exists("example.txt")) {
54 Serial.println("example.txt exists.");
57 Serial.println("example.txt doesn't exist.");
60 // open a new file and immediately close it:
61 Serial.println("Creating example.txt...");
62 myFile = SD.open("example.txt", FILE_WRITE);
65 // Check to see if the file exists:
66 if (SD.exists("example.txt")) {
67 Serial.println("example.txt exists.");
70 Serial.println("example.txt doesn't exist.");
74 Serial.println("Removing example.txt...");
75 SD.remove("example.txt");
77 if (SD.exists("example.txt")){
78 Serial.println("example.txt exists.");
81 Serial.println("example.txt doesn't exist.");
87 // nothing happens after setup finishes.