How to store information from RTC of industrial PLC

M-Duino RTC + SD storage
March 25, 2020 by
How to store information from RTC of industrial PLC
Andrei Postolache

Introduction

In this post, you will learn how to store the information provided by your RTC. 

The links will redirect you to our website where you can find out:

  • How to program an M-Duino PLC controller RTC   Read >>

and also

  • How to use an SD card   Read >>

It is very important to take a look at these posts because what we are going to do is a fusion between the code of the RTC and the SD card.

Requirements

In order to work with a micro SD, you must have an M-Duino PLC (Programmable Logic Controller Arduino for industrial automation) included in the families below:

Wifi & Bluetooth PLC Family


>>

Ethernet Controller Family


>>

GPRS / GSM Controller Family


>>

You will also need a micro SD card and some tweezers.

Checking switches

Before proceeding to connect the SD card to the industrial controller, you must check that the SD switch is correctly configured. You must check the communication switches and set the SD switch at OFF position (if your PLC has it). You must also check that RTC SCL and RTC SDA are in ON mode to enable I2C wires to the RTC.


 Software

Once the RTC programming has been successfully carried out following the steps of the blog of RTC test on M-Duino PLUS version, where the data was sent using the Serial Monitor, we will proceed to save it to the SD card. The first thing you need to do is to include the RTC, SD and SPI libraries.  

The second step is to create the file where you will store the information. Then you create all the constants, configure the bus that communicates the Arduino with the SD card and store all the constants in a new internal variable.

In the next step, you have to open the file of the SD card and store all the information of each variable. The last step consists of closing the file because if you do not do this, the storage will never stop.


#include <RTC.h>
#include <SD.h>
#include <SPI.h>

File myFile;

const int YEAR = 2020;
const int MONTH = 2;
const int DAY = 26;
const int HOUR = 12;
const int MINUTE = 20;
const int SECOND = 25;

//const int TIME = 1522242547;
//UNIX timestamp (spent seconds since Jan 1 1970 00:00:00)

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  SD.begin(53);
  RTC.setYear(YEAR);                      //sets year
  RTC.setMonth(MONTH);                    //sets month
  RTC.setMonthDay(DAY);                   //sets day
  RTC.setHour(HOUR);                      //sets hour
  RTC.setMinute(MINUTE);                  //sets minute
  RTC.setSecond(SECOND);                  //sets second
  //RTC.setTime(TIME);                    //sets UNIX timestamp
  if(!RTC.write()){                       //RTC.write writes in the RTC memory all that has been set
    Serial.println("Write date error: Are the switches well placed?");
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  SD.begin(53);
  myFile = SD.open("test.txt", FILE_WRITE);
  if (!RTC.read()) {
    Serial.println("Read date error: is time set?");
  } else {
    Serial.print("Time: ");
    myFile.println(RTC.getYear());
    Serial.print("-");
    myFile.println(RTC.getMonth());
    Serial.print("-");
    myFile.println(RTC.getMonthDay());
    Serial.print(" ");
    myFile.println(RTC.getHour());
    Serial.print(":");
    myFile.println(RTC.getMinute());
    Serial.print(":");
    myFile.println(RTC.getSecond());
    Serial.print(" (");
    myFile.println(RTC.getTime());
    Serial.println(")");
    myFile.close();
 }
 delay(1000);
}

Want to know more?

Solve technical questions by following our blog.  Blog >>

See how to implement our equipment in the Case Studies section.   Case Studies >>

​Search in our Blog

How to store information from RTC of industrial PLC
Andrei Postolache March 25, 2020

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 >>>