SD card integration with the MediaTek LinkIt ONE development platform

Introduction

The MediaTek LinkIt ONE development board supports communication with external memory, such as an SD card, for data storage. This tutorial provides a detailed description of SD card integration and operation on the LinkIt ONE development platform. This is done by storing the output from three analog sensors in a log style file on an SD card.

In this tutorial you’ll implement read and write operations in the Arduino environment, illustrating the two main data communication operations to and from external memory storage on LinkIt ONE development platform. By the end of the tutorial, you’ll have a fully functioning SD card on a LinkIt ONE development board.

    The content of the tutorial is as follows:
  • Description of the hardware requirements and setup.
  • Details of the software requirements to interface with the hardware and provide the data communication.
  • A conclusion and other relevant information for future developments.

Hardware setup

The LinkIt ONE development board provides 10 MB internal flash storage and an SD card slot. You can manipulate these two storage areas using the LSD and LFlash classes of the Storage module.

The LSD class provides for interaction with the LinkIt SD card slot. If you’d like to use an Arduino SD shield, use the SD library provided by Arduino instead. This means you can mount and use an Arduino SD shield at the same time as using the LinkIt SD card slot.

The board doesn’t provide for using the built-in SD card and SPI functions together, you have to choose which one is active. So, before using the SD card classes, make sure the selection switch — shown in the figure below — is set correctly in the SD Card position.

Once the board is configured to SD Card mode, calling SPI classes may result in an unexpected SD card behavior. If you want to use an SD Card and SPI mode at the same time, use an external SD shield.


This tutorial shows how to use the SD card by saving information from the analog signals transmitted by three sensors. Any analog sensors could be used, but in this case a photo resistor and two potentiometers are employed, connected to the analog input pins A0, A1 and A2. For more information on pins, see the  LinkIt ONE development board pin-out diagram .The SD card support on the LinkIt ONE development board provides for an SD card of class 10, with memory not exceeding 32GB.

The list of the components required, with their descriptions, is given below.

Component Description Quantity
LinkIt ONE development board   1
SD card 32GB class10 micro SD card 1
Breadboard Basic breadboard 1
Photo Resistor Basic photo resistor 1
Potentiometers Basic pontentiometers 2

The breadboard, used in this tutorial is from the Sidekick Basic Kit for LinkIt ONE from Seeed Studio, which is a collection of essential components for LinkIt ONE projects. The photo resistor and potentiometers are from the Vilros Ultimate Starter Kit.

The following two figures were created with Fritzing. The first one is the schematic and the second is the breadboard view showing how the components for this tutorial are connected.

Schematic view for the sensor connectivity on the LinkIt ONE development board 

Breadboard view for the sensor connectivity on the LinkIt ONE development board

The built hardware — development board, breadboard, photo resistor and potentiometers — is shown below.

Software implementation

This section describes the requirements for the software implementation on the LinkIt ONE development board and includes the following:

  • Setup: for the LinkIt ONE SDK (for Arduino)
  • Data communication: writing to the SD card.
  • Data communication: reading from the SD card.

Setup

If you haven’t already done so, follow the instructions in the LinkIt ONE get started guide to install and configure the Arduino IDE and LinkIt ONE SDK (for Arduino).

Writing data to an SD Card

This section describes how to write data to an SD card. The code required is illustrated with a sketch that writes the output from the three analog sensors to the SD card. To start create a new sketch in the Arduino IDE and add the following code:

  1. Include the LSD library.

    Use the #include <LSD.h> command to provide the library support for SD card (external) memory management.

    /* 
    SD card datalogger
    This example shows how to log data from three analog sensors to an SD card using the LSD library.
    */
    #include <LSD.h>
  2. Add a setup() structure. The resources this sketch will use are initialized in the standard Arduino setup() structure.

  3. Initialize serial communication.

    Messages printed on the serial port of a LinkIt ONE development board are used to support debugging of the software. For more details about serial communication on the LinkIt ONE development board, please refer to the  developer’s guide .

    Initiate serial communication by calling the begin() function of the Serial class. Provide the communication data rate in bits per second (baud) for serial data transmission. For communicating with a computer, you can use 9600 baud. Then use a while() loop to wait for the serial connection to be established, as in the code below:

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

    Call the  LSD.begin()  function to initialize the SD memory card. After successful initialization, the message card initialized is sent to the serial port.

    Serial.print("Initializing SD card...");
    LSD.begin();
    Serial.println("card initialized.");
  5. Add a loop() function. The sensor data will be read and written to the SD card in a standard Arduino loop()structure.

  6. Read the output of the sensors from analog pins.

    Create a String variable to hold the data from the output of the three analog sensors. Then add a for control loop structure to read the value from each analog pin (pins 14, 15 and 16) in turn and append the read value to the String.

    // make a string for assembling the data to log:
    String dataString = "";
    
    // read three sensors and append to the string:
    for (int analogPin = 14; analogPin < 17; analogPin++) {
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      if (analogPin < 16) {
        dataString += ",";
      }
    }
  7. Open and write to a file.

    Create an LFILE object to open a file on the SD card in which to write the content of the string containing the three sensor output values. The LSD.open() function provides the full file path and operating mode (read or write) to the LFile object. The string is then written into a file with the println() function.

    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    LFile dataFile = LSD.open("datalog.txt", FILE_WRITE); 
    
    // if the file is available, write to it:
      if (dataFile) {
        dataFile.println(dataString);
  8. Commit the written data to the SD card

    Use either the flush() or close() methods after write() is complete to ensure the data is written to the file. Otherwise, the data may get lost if a power loss or board reset event occurs.

    dataFile.close();
  9. Print the string to the serial port. To check the content of the data files, print the string to the serial port too.

    // print to the serial port too:
    Serial.println(dataString);
  10. Handle errors

    If the file doesn’t exist or cannot be opened, add an error message to the serial output that could be used to assist with debugging.

    // if the file isn't open, pop up an error:
    else {
        Serial.println("error opening datalog.txt");
    }

The code is now finished, and here is the complete code you can copy into your sketch:

/*
SD card datalogger

This example shows how to log data from three analog sensors
to an SD card using the LSD library.

*/

#include <LSD.h> 

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...");

  LSD.begin();

  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
  // read three sensors and append to the string:
  for (int analogPin = 14; analogPin < 17; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 16) {
      dataString += ",";
  }
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
LFile dataFile = LSD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
  dataFile.println(dataString);
  dataFile.close();
  // print to the serial port too:
  Serial.println(dataString);
}
// if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

Verify the sketch by clicking the checkmark tool button on the Arduino toolbar, as shown below:


Once the sketch is verified, upload the sketch to your LinkIt ONE development board using the debug port as described in MediaTek LinkIt ONE developer’s guide.

The Mass Storage Bootup/Normal Bootup switch should be set to  Normal Bootup position on the LinkIt ONE development board before uploading the software. The Normal Bootup  mode provides for digital input or output (I/O) on the LinkIt ONE development board. A detailed description of these modes is given in  MediaTek LinkIt ONE developer’s guide . The figure below specifies the location of the switch, more details are also provided in  LinkIt ONE development board pin-out diagram .


After the upload is complete, in Arduino on the Tools menu, point to Port; click the COMx item that matches the LinkIt ONE development board’s modem port, which can be determined from Device Manager according to the description in the MediaTek LinkIt ONE developer’s guide. To view serial output data from the three analog signals open the Serial Monitor by clicking the button on the right upper corner of Arduino, as shown below, or open it from Tools menu by clicking Serial Monitor.

The serial monitor will now display the analog output values obtained from the A0, A1 and A2 analog input pins along with any of the debug messages, as shown below.

 

The file content written on the SD card should match the output in the Serial Monitor. To check this, set the Mass Storage Bootup/Normal Bootup switch to  Mass Storage Bootup mode, which swaps the USB port from digital I/O mode to mass storage mode. A detailed description of these modes is given in MediaTek LinkIt ONE developer’s guide. After switching the mode, on your PC go to Computer, and observe that there are two removable disks corresponding to the internal storage (10MB) and the SD card (32GB) on the LinkIt ONE development board. Open the SD card storage, where you’ll find the  datalog.txt  file created by the sketch.

 

Open  datalog.txt . The figure below shows the data written to the SD card.

Compare the content of datalog.txt and the serial monitor and you will see they are the same. Congratulations, you have successfully written data to the SD card on your LinkIt ONE development board.

Reading data from an SD Card

This section describes how to read data from an SD card. The code required is illustrated with a sketch that transfers the content of an SD card file to the computer using the read() function and serial port. To start create a new sketch in the Arduino IDE and add the following code:

  1. Include the LSD library integration. 


    Use the  #include <LSD.h>  command to provide the library support for SD card (external) memory management.

    /* SD card datalogger
    
    This example shows how to read data from a file stored
    On an SD card using the LSD library.
    
    */
    
    #include <LSD.h>
  2. Add a  setup()  function.

    The resources this sketch will use are initialized in the standard Arduino  setup() structure.

  3. Initialize serial communication.

    Initiate serial communication by calling the begin() function of the Serial class. Provide the communication data rate in bits per second (baud) for serial data transmission. For communicating with the computer, you can use 9600 baud. Then use a while() loop to wait for the serial connection to be established, as in the code below: 

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

    For more details about serial communication on the LinkIt ONE development board, please refer to the developer’s guide

  4. Initialize the SD card.

    Call the  LSD.begin()  function to initialize the SD memory card. After successful initialization the message card initialized is sent to the serial output.

    Serial.print("Initializing SD card...");
    
    LSD.begin();
    Serial.println("card initialized.");
  5. Read the content from a file

    Create an LFILE object, then call the  open()  function of the LSD class, giving the file path and the mode (optional) in which to open the file. If the data file is found, check in a while()  loop if there are any bytes available to read from the file and then write the content of the file to the serial port byte by byte using the Serial.write(dataFile.read())  command. Once the content of the file has been read,close the file using the  close()  function to deallocate resources assigned to this operation and discard any unread data.

    LFile dataFile = LSD.open("datalog.txt");
    
    // if the file is available, read from it:
    if (dataFile) {
      while(dataFile.available()){
        Serial.write(dataFile.read());
      }
      dataFile.close();
    }
  6. Handle errors

    If the file doesn’t exist or cannot be opened, add an error message to be written to the serial output that could be used to assist with debugging.

    // if the file isn't open, pop up an error:
      else {
         Serial.println("error opening datalog.txt");
      }
    )
  7. Add a  loop()  structure
    Add an empty standard Arduino loop( ) structure, shown below, to ensure the sketch validates correctly:

    void loop()
    {
    }

The code is now finished, and here is the complete code you can copy into your sketch:

/*

SD card datalogger

This example shows how to read data from a file stored 
on an SD card using the LSD library.

*/

#include <LSD.h>

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...");
  LSD.begin();
  Serial.println("card initialized.");

  LFile dataFile = LSD.open("datalog.txt");

  // if the file is available, read from it:
  if (dataFile) {
    while(dataFile.available()){
      Serial.write(dataFile.read());
    }
    dataFile.close();

  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    }
}
void loop()
{ 
}


Once upload is complete, open the  Serial Monitor . The data in the serial monitor should match the content of the  datalog.txt  file created in "Writing Data to an SD Card", as shown below.Verify and upload the sketch to your LinkIt ONE development board. (For more information see "Writing Data to an SD Card".)

 

Deleting datalog.txt from the SD card before running this sketch will result in a file operation error, which will be notified to the serial port as shown below.

Final words

In this tutorial you have implemented SD card read and write operations on a LinkIt ONE development board, using the  LSD.h  library to initialize and support data communication to and from an SD card.

For more information on the SD card functions provided in the MediaTek LinkIt ONE API and how to use the SDK and development board, please refer to: