Advertisement
Ruddog

SD Card Example

Jun 13th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. /* Thank you wemos
  2.  * Micro SD Shield - Basic file example
  3.  *
  4.  * This example shows how to create and destroy an SD card file
  5.  *
  6.  * The WeMos Micro SD Shield uses:
  7.  * D5, D6, D7, D8, 3V3 and G
  8.  *
  9.  * The shield uses SPI bus pins:
  10.  * D5 = CLK
  11.  * D6 = MISO
  12.  * D7 = MOSI
  13.  * D8 = CS
  14.  *
  15.  * The SD card library uses 8.3 format filenames and is case-insensitive.
  16.  * eg. IMAGE.JPG is the same as image.jpg
  17.  *
  18.  * created Nov 2010 by David A. Mellis
  19.  * modified 9 Apr 2012 by Tom Igoe
  20.  *
  21.  * This example code is in the public domain.
  22.  * https://github.com/esp8266/Arduino/blob/master/libraries/SD/examples/Files/Files.ino
  23.  */
  24.  
  25. #include <SPI.h>
  26. #include <SD.h>
  27.  
  28. const int chipSelect = D8;
  29. File myFile;
  30.  
  31. void setup()
  32. {
  33.   // Open serial communications and wait for port to open:
  34.   Serial.begin(9600);
  35.   while (!Serial) {
  36.     ; // wait for serial port to connect. Needed for Leonardo only
  37.   }
  38.  
  39.   Serial.print("Initializing SD card...");
  40.  
  41.   if (!SD.begin(chipSelect)) {
  42.     Serial.println("initialization failed!");
  43.     return;
  44.   }
  45.   Serial.println("initialization done.");
  46.  
  47.   if (SD.exists("example.txt")) {
  48.     Serial.println("example.txt exists.");
  49.   }
  50.   else {
  51.     Serial.println("example.txt doesn't exist.");
  52.   }
  53.  
  54.   // open a new file and immediately close it:
  55.   Serial.println("Creating example.txt...");
  56.   myFile = SD.open("example.txt", FILE_WRITE);
  57.   myFile.close();
  58.  
  59.   // Check to see if the file exists:
  60.   if (SD.exists("example.txt")) {
  61.     Serial.println("example.txt exists.");
  62.   }
  63.   else {
  64.     Serial.println("example.txt doesn't exist.");
  65.   }
  66.  
  67.   // delete the file:
  68.   Serial.println("Removing example.txt...");
  69.   SD.remove("example.txt");
  70.  
  71.   if (SD.exists("example.txt")) {
  72.     Serial.println("example.txt exists.");
  73.   }
  74.   else {
  75.     Serial.println("example.txt doesn't exist.");
  76.   }
  77. }
  78.  
  79. void loop()
  80. {
  81.   // nothing happens after setup
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement