Skip to content Skip to sidebar Skip to footer

RTC DS3231 Arduino Using Rinky Dink Library

RTC Using  Rinky Dink Library

Update:
The program code in this article have a bug and I updated this article. Please read here or here.

RTC DS3231 Arduino Using Library RinkyDink – RTC is the main component of timing, whether it is used on wall clocks, computers, cellphones, watches, servers, gps, power meters and others. All information on digital devices that take time, generally using RTC.

But why should we use RTC as timing? Can't we make a manual program by using delay as a 1 second delay?

The answer is that first, RTC has a high degree of precision in timing calculations. Both RTCs have consistent timings that don't have to be reset when a device's power suddenly goes out.

If you use Arduino, by taking advantage of the delay for a 1 second delay, when suddenly the Arduino power goes out, the Arduino starts the time from 0 again. Not very good for Timing.

One of the most widely used RTC is the DS3231. Base on datasheet, This RTC made by MAXIM INTEGRATED has a Real time feature to count seconds, minutes, hours, date, month and year valid up to 2100.
  • Accuracy ±2 ppm from 0°C to +40°C 
  • Accuracy ±3.5ppm from -40°C to +85°C 
  • Accuracy Digital temperature sensor output ±3°C 
  • I2C communication (400kHz) 
  • Battery backup voltage 3.3V


Library DS3231

To operate this RTC using Arduino, we need a library, friends. Many have made a library for this RTC DS3231.

In this article, I only use a library made by Henning Karlsen, the owner of the RinkyDinkElectronics.com site.

If you want to see the website, please click  here. Download the Arduino DS3231 RTC library below:

Download Libary DS3231 Rinky Dink


If you have, enter the Library DS3231 library that you have downloaded to the Arduino IDE by:
  • Open Arduino IDE
  • Click Sketch > Include Library > Add .ZIP Library
  • Look for the DS3231.zip Library that friends have downloaded
  • Click OK

Arduino DS3231 Circuit

Make the Arduino DS3231 RTC circuit as shown below:



Get Data Program Code

The following is a basic program that can display the day, date and time. Please upload the program below.

#include <DS3231.h>

DS3231  rtc(SDA, SCL);
String hari, Senin;

void setup()
{
  Serial.begin(115200);
  //hilangkan komentar di while jika menggunaka Arduino leonardo
  //while (!Serial) {}
  rtc.begin();
}

void loop()
{
  if (rtc.getDOWStr() == "Monday") {
    hari = "Senin";
  }
  if (rtc.getDOWStr() == "Tuesday") {
    hari = "Selasa";
  }
  if (rtc.getDOWStr() == "Wednesday") {
    hari = "Rabu";
  }
  if (rtc.getDOWStr() == "Thursday") {
    hari = "Kamis";
  }
  if (rtc.getDOWStr() == "Friday") {
    hari = "Jumat";
  }
  if (rtc.getDOWStr() == "Saturday") {
    hari = "Sabtu";
  }
  else if (rtc.getDOWStr() == "Sunday") {
    hari = "Minggu";
  }


  // Menampilkan hari
  Serial.print(hari);
  Serial.print(" ");

  // Menampilkan tanggal
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Menampilkan Waktu, jam menit detik
  Serial.println(rtc.getTimeStr());

  delay (1000);
}

Then open the serial monitor on the Arduino IDE. The result will look like in the image below.


Set the time as you wish

In the picture above, the data obtained is Sunday, the 1st of the 1st month of 2000. The hour is 1 minute. But how do we organize the data according to the current time?

For example, let's set it on Wednesday, the 13th of the 3rd month of 2019. And 14 o'clock at the time this article was written. The trick is to use the following command:

rtc.setDOW(WEDNESDAY);  // rtc.setDOW(hari) untuk mengatur hari
rtc.setTime(0, 0, 0);   // rtc.setTime(jam,menit,detik_ untuk mengatur dalam format 24 jam
rtc.setDate(0, 0, 0);   // rtc.setDate(tanggal,bulan, tahun) untuk mengatur tanggal

An example of the program is as follows:

#include <DS3231.h>

DS3231  rtc(SDA, SCL);
String hari, Senin;

void setup()
{
  Serial.begin(115200);
  //hilangkan komentar di while jika menggunaka Arduino leonardo
  //while (!Serial) {}
  rtc.begin();

  rtc.setDOW(WEDNESDAY);  // rtc.setDOW(hari) untuk mengatur hari
  rtc.setTime(14, 0, 0);   // rtc.setTime(jam,menit,detik_ untuk mengatur dalam format 24 jam
  rtc.setDate(13, 3, 2019);   // rtc.setDate(tanggal,bulan, tahun) untuk mengatur tanggal

}

void loop()
{
  if (rtc.getDOWStr() == "Monday") {
    hari = "Senin";
  }
  if (rtc.getDOWStr() == "Tuesday") {
    hari = "Selasa";
  }
  if (rtc.getDOWStr() == "Wednesday") {
    hari = "Rabu";
  }
  if (rtc.getDOWStr() == "Thursday") {
    hari = "Kamis";
  }
  if (rtc.getDOWStr() == "Friday") {
    hari = "Jumat";
  }
  if (rtc.getDOWStr() == "Saturday") {
    hari = "Sabtu";
  }
  else if (rtc.getDOWStr() == "Sunday") {
    hari = "Minggu";
  }


  // Menampilkan haru
  Serial.print(hari);
  Serial.print(" ");

  // Menampilkan tanggal
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Menampilkan Waktu, jam menit detik
  Serial.println(rtc.getTimeStr());

  delay (1000);
}

Then the results that appear as in the image below::


That's a tutorial on how to use the Arduino DS3231 RTC, if you have any questions, suggestions, please write them in the comments column. Happy learning, happy working.

If you want to try the adafruit RTClib library, I've made a tutorial and the program code. You can read the article How to Use the Arduino RTC DS3231 Using the Adafruit Library.
Thank you for visiting Chip Piko's website. Hope it is useful.

Post a Comment for "RTC DS3231 Arduino Using Rinky Dink Library"