Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteris

Mduino RTC + SD storage

Introduction


In this post we will teach you how to store the information that is provided by our RTC . 

These links will redirect you to our website where you can find how to program the Mduino RTC and also how to use the SD card. Is very important to have a look to those posts because what we will do is a fusion between the code of the RTC and SD.


https://www.industrialshields.com/es_ES/blog/nuestro-blog-1/post/rtc-test-on-m-duino-plus-version-51

https://www.industrialshields.com/es_ES/blog/nuestro-blog-1/post/how-to-connect-an-sd-card-to-industrial-arduino-ethernet-plc-82

 

Requirements

In order to work with a micro SD, you must have one Mduino PLC  included in the next families:


Also we will need a micro SD card and a tweezers.

Checking switches

Before proceeding to connect the SD card to the industrial controller, we must check that the SD switch is correctly configured. We must check the communication switches and put the SD switch at OFF position (If your PLC has it) . Also we need to check that  RTC SCL and RTC SDA are set to ON mode to enable I2C wires to the RTC.


 Software


When we had programmed the RTC we were sending the information to the Serial Monitor, now we need to send it to the SD. First of all we need to include the libraries of RTC, SD and SPI.
Second step is to create the file where we will store the information. After that we create all the constants, set the bus that communicate the Arduino with the SD card and store all the constants in a new internal variables.

On the following step we open the file of the SD card and store all the information of each variable, the last step consists in close the file, because if you don't 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);
}

Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send