Using the SD library to create and remove files on a SD card
This example shows how to create and destroy a file on a SD card. Please click here for more information on the SD library.
Hardware Required
Arduino board
Ethernet Shield (or other board with an SD slot)
Formatted SD card
Circuit
image developed using Fritzing. For more circuit examples, see the Fritzing project page
The Arduino board has to be connected to the Ethernet Shield and also has a USB cable connected to the computer.
Schematics
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Code
The code below is configured for use with an Ethernet shield, which has an onboard SD slot. In the setup()
, SD.begin()
names pin 4 as the CS pin. This pin varies depending on the make of shield or board you are using.
In the setup()
, open a new file with SD.open()
named "example.txt". FILE_WRITE
enables read and write access to the file, starting at the end. In this example though, immediately close the file by calling myFile.close()
.
After checking to make sure the file exists with SD.exists()
, delete the file from the card with SD.remove
.
/*
SD card basic file example
This example shows how to create and destroy an SD card file
The circuit. Pin numbers reflect the default
SPI pins for Uno and Nano models:
SD card attached to SPI bus as follows:
** SDO - pin 11
** SDI - pin 12
** CLK - pin 13
** CS - depends on your SD card shield or module.
Pin 10 used here for consistency with other Arduino examples
(for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 24 July 2020
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
const int chipSelect = 10;
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// wait for Serial Monitor to connect. Needed for native USB port boards only:
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
// open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
// Check to see if the file exists:
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
// delete the file:
Serial.println("Removing example.txt...");
SD.remove("example.txt");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
}
void loop() {
// nothing happens after setup finishes.
}
See Also
SD library - Your reference for the SD Card library.
Notes on using SD cards - What you need to know when you use SD cards
CardInfo - Get info about your SD card.
Datalogger - How to log data from three analog sensors to an SD card.
DumpFile - How to read a file from the SD card.
Listfiles - How print out the files in a directory on a SD card.
ReadWrite - How to read and write data to and from an SD card.
Last revision 2015/08/18 by SM