Basics with SD card in an industrial PLC

Working with a programmable logic controller for industrial automation
October 14, 2019 by
Basics with SD card in an industrial PLC
Boot & Work Corp. S.L., David Martin

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 thought thinking about memory space or supply line disconnections. There are a lot of different kinds of cards from a 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:



Requirements


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



Software


The first thing we must do is include 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)
        ;
}

Basics: SD card in Arduino PLC


Are you looking for further information?

Our tech-sales team will be glad to help you.

 

​Search in our Blog

Basics with SD card in an industrial PLC
Boot & Work Corp. S.L., David Martin October 14, 2019

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>