Skip to content Skip to sidebar Skip to footer

How to Set RTC Via Arduino IDE Serial Monitor?

   Set RTC via Arduino Serial Monitor

How to Set RTC Via Arduino IDE Serial Monitor? - In this article I will share the code and method where we can set the time on the RTC (Real Time Clock) via Serial communication. This code is very useful and can be used in various applications, where we have to manage the time only with Serial communication.

How to Set RTC Via Arduino IDE Serial Monitor?

So, we use certain characters or words, when we enter that character, it will change a certain schedule. So I set the following command:

  • DT to change the Date
  • MN to change the Month
  • YR for changing Year
  • HR to change Hours
  • MT to convert Minutes
  • SC to change Seconds
For the circuit and the type of RTC as an example, we use the DS3231 or DS1307 . The wiring is as follows:
How to Set RTC Via Arduino IDE Serial Monitor?

For connection from RTC DS3231 or also a family with it, DS1307 uses I2C communication. Then only 4 pins are used, namely VCC, GND, SDA and SCL. To operate this RTC using Arduino, please download the RTClib library at the following link:

Download DS3231 Libary RTClib

If you have, enter the RTClib Library library that you have downloaded to the Arduino IDE by:
  • Open Arduino IDE
  • Click Sketch > Include Library > Add .ZIP Library
  • Look for the RTClib-master.zip Library that you have downloaded
  • Click OK
In addition, you can also add available libraries in the Arduino IDE library manager by:
  • Click the Sketch menu > Include Library > Manage Libraries
  • Search RTClib
  • Then click install.

Program

The program for this is as follows:

#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String read_string = " ";
int read_int = 0;

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("RTC not found");
    Serial.flush();
    abort();
  }
}

void loop () {
  readRTC();
  setRTC();
  delay(1000);
}

void readRTC()
{
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); 
  Serial.print(", ");
  Serial.print(now.day(), DEC);     
  Serial.print(' ');
  Serial.print(Month[now.month()]); 
  Serial.print(' ');
  Serial.print(now.year(), DEC);    
  Serial.print(", ");
  Serial.print(now.hour(), DEC);    
  Serial.print(':');
  Serial.print(now.minute(), DEC);  
  Serial.print(':');
  Serial.print(now.second(), DEC);  
  Serial.println('\n');
}
void setRTC() {
  DateTime now = rtc.now();

  while (Serial.available() > 0) {

    read_string = Serial.readStringUntil('\n');
    read_string.trim();
    delay(100);


    if (read_string == "DT") {
      Serial.print("Insert Date: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), read_int, now.hour(), now.minute(), now.second()));
    }

    
    if (read_string == "MN") {
      Serial.print("Insert Month: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), read_int, now.day(), now.hour(), now.minute(), now.second()));
    }

    if (read_string == "YR") {
      Serial.print("Insert Years: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(read_int, now.month(), now.day(), now.hour(), now.minute(), now.second()));
    }

    if (read_string == "HR") {
      Serial.print("Insert Hours: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), read_int, now.minute(), now.second()));
    }

    if (read_string == "MT") {
      Serial.print("Insert Minutes: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), read_int, now.second()));
    }

    if (read_string == "SC") {
      Serial.print("Insert Seconds: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), read_int));
    }
  }
}

Now, please upload the program above, then open Serial Monitor. Then use the following command:

  • DT to change the Date
  • MN to change the Month
  • YR for changing Year
  • HR to change Hours
  • MT to convert Minutes
  • SC to change Seconds
and another command will appear, enter the time value. Then the RTC value will change.

Done.

Post a Comment for "How to Set RTC Via Arduino IDE Serial Monitor?"