|| Back to Support & Help Documentation ||

M-Duino uSD


     By following these steps carefully, you will be able to achieve the use of the uSD using the Industrial Shields equipments.

Introduction


In this post, we will start working with the programming of the micro SD card for data storage in a PLC controller Arduino. First of all, we will introduce the library used to manage the programming of the micro SD and, finally, we will see and comment on two examples to write and read from our uSD card.

 


The SD Card is a nice way to save the information inside our industrial controller thout thinking about memory space or supply line disconnections. There are a lot of different kinds of cards from few megabytes to gigabytes, allowing us to size our systems as we need.    

Firsts steps


Before starting the basic examples with the SD card, keep in mind it will be useful reading the next blogs:



Requeriments


In order to work with an SD card, you must have one an industrial Arduino PLC included in the next families:


We must start the SD with the next command. Keep in mind that the number next to the functions means where the CS chip is located. For our products, the number 53 is the one we need:

SD.begin(53);

Software


The first thing we must do is including the Arduino library that contains the SD card functions:

#include <SD.h>

We must start the SD with the next command. Keep in mind that the number next to the functions means where the CS chip is located. For our products, the number 53 is the one we need:

SD.begin(53);

For an extended utility, we can capture the returned number of this function, and check if the init process was fine:

if(!SD.begin(53))
{
    Serial.println("Initialization failed"); while (1)
        ;
}

In order to create a file or open an existing one for writing, we must put the next line:

File myFile = SD.open("test.txt", FILE_WRITE);

We can look for errors during the file opening:

if (!myFile) 
{
    Serial.println("File opening failed"); while (1) ;
}

Writing in an opened file:

myFile.println("Test text");

Closing a file:

myFile.close();

Opening a file for reading:

File myFile = SD.open("test.txt");

Reading a character:

myFile.read();

Knowing the size of the file:

myFile.size();

When the file is closed, we can delete an existing file:

 SD.remove("test.txt"); 


Examples

You can see reding and writing bytes examples in the following paragraphs:

Writing information to the SD

// SD Write Example
// This example writes a test text in the SD card

#include <SD.h>

// Counter variable
int n = 0;

// Setup function
void setup()
{
    // Set the speed of the serial port    
    Serial.begin(9600UL);

    // Init the SD Card     
    if (!SD.begin(53))
    {
        // Error during SD init         
        Serial.println("Card failed, or not present");    
        while (true)
            ;
    }
}

// Loop function
void loop()
{
    // Open the file
    File dataFile = SD.open("test.txt", FILE_WRITE);

    // Check if the file opening was fine   
    if (!dataFile)
    {
        // Error during the opening Serial.println("Card failed, or not present"); while (true) ;
    }

    dataFile.println("Test text"); dataFile.close();

    // Program end
    while (true)
        ;
}


Reading information from the SD

// SD Read Example
// This example read a file in the SD card

#include <SD.h>

// Counter variable
int n = 0;

// Setup function
void setup() 
{
    // Set the speed of the serial port    
    Serial.begin(9600UL);

    // Init the SD Card     
    if (!SD.begin(53)) 
    { 
        // Error during SD init         
        Serial.println("Card failed, or not present");    
        while (true)
            ;
    }
}

// Loop function
void loop()
{
    // Open the file
    File dataFile = SD.open("test.txt");

    // Check if the file opening was fine   
    if (!dataFile) 
    {
        // Error during the opening     
        Serial.println("Card failed, or not present");
        while (true)
            ; 
    }

    // Read each byte of the file
    for (int n = 0 ; n < dataFile.size() ; n++)
      Serial.println(char(dataFile.read()));
    dataFile.close();

    // Program end
    while (true)
        ;
}


1 Star 2 Star 3 Star 4 Star 5 Star