Programming the WeMos Micro SD Shield

When you first get the Micro SD Shield the first two things you will want to know how to do are store and retrieve your data. This is pretty much the case with any storage media. WeMos has a few libraries on Github that you can use to get you started working with your Arduino-compatible shield right away. You can find them all under the WeMos Micro SD Shield directory.

WeMos Micro SD Shield WeMos D1 Mini

Shopping List

 

Code example for reading and writing to your WeMos Micro SD Card.

/*
 * Micro SD Shield - Read/Write
 *
 * This example shows how to read and write data 
 * to and from an SD card file
 *
 * The WeMos Micro SD Shield uses:
 * D5, D6, D7, D8, 3V3 and G
 *
 * The shield uses SPI bus pins:
 * D5 = CLK
 * D6 = MISO
 * D7 = MOSI
 * D8 = CS
 *
 * The SD card library uses 8.3 format filenames 
 * and is case-insensitive.
 * eg. IMAGE.JPG is the same as image.jpg
 *
 * created Nov 2010 by David A. Mellis
 * modified 9 Apr 2012 by Tom Igoe
 *
 * This example code is in the public domain.
 * https://github.com/esp8266/Arduino/blob/master/libraries/SD/examples/ReadWrite/ReadWrite.ino
 */

#include "SPI.h"
#include "SD.h"

const int chipSelect = D8;
File myFile;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  /* open the file.
   * note that only one file can be open at a time
   */
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
  // nothing happens after setup
}

The example code will create a file called "test.txt". The newly formed text file will be blank so after opening the file the example code will write "testing 1, 2, 3." and it will close the open file. 

Next the code will read the newly created file and output the file to the Serial Monitor. You can view the result by clicking on the Serial Monitor button on the top right within the Arduino IDE.

The result should be similar to the following:

 


Leave a comment

Please note, comments must be approved before they are published