Setting the DS3231 RTC (Real Time Clock) Accurately, Fast and Automated Using Java (+-1s)

Introduction: Setting the DS3231 RTC (Real Time Clock) Accurately, Fast and Automated Using Java (+-1s)

About: I may be a perfectionist, but I'm not ambitious enough to finish all my projects perfectly.

This Instructable is going to show you how to set the time on a DS3231 Real Time Clock using an Arduino and a small Java application that uses the serial connection of the Arduino.

The basic logic of this program:

1. The Arduino sends a serial request.

2. The Java program looks what day/month/... the arduino requested and sends the relevent data back.

3. After all needed information is gathered the arduino stores the data in the DS3231.

The DS3231 RTC Module

The DS3231 RTC Module is a relatively cheap RTC module with a high accuracy. (a few mins a year)

There are different varieties whose basic functionality is the same with only minor differences.

Detailed comparison can be found here: DS323x Comparison |

Buy DS3231:DS3231 |

Datasheet:DS3231 Datasheet |

Datasheet:DS1307 Datasheet |

Parts Needed

1x Arduino (Nano, Uno, MEGA, etc.)

4x Jumper Wires

1x DS3231 Module (DS1307 Module)

Step 1: Wiring

Wire the DS3231 as shown in the image above. Of course you can do it without the use of a breadboard, but it will be a bit messier. I've used an Arduino Nano for demonstration but an Uno or a MEGA should work aswell. (Others might also work but i'm to lazy to confirm)

  • Arduino -> DS3231
  • A4 -> SCL
  • A5 ->SDA
  • 5V -> VCC
  • GND -> GND

Step 2: The Arduino Code

Download the .ino file from below and upload it to your Arduino (in my case a Nano).

The sketch needs the following libraries installed to work correctly:

A tutorial on how to upload code to your arduino can be found here (

In the Arduino IDE verify that the code was submitted correctly. (Done uploading)

Your Arduino is now ready to receive time information.

Download the code here: (

Download the source here: (

GitHub repository: DS3213 RTC Time Sync (

Bitbucket repository: DS3231 RTC Time Sync (

Step 3: The Java Application

1. Run "DS3231Sync.jar" downloaded before by double clicking it.

2. A small dialog will open asking you for the port of your arduino.

Should you be uncertain about the port used by your arduino take a look in the Arduino IDE on the bottom right.

(See images above)

3. Another dialog will open and ask you for a time zone offset.

The offset is relative to the local time of your computer. To set the RTC to your local time just enter +00:00.

It is critical that you enter the time zone offset in a specific format!

The general format is +hh:mm or -hh:mm. Always include leading zeros!

(E.g.: -03:00; -06:00; +09:00; +02:30)

4.The Java Application will launch a small window with console output.

Keep an eye for confirmation messages like: "Time set finished"

When the time set process finished the code will print the current time on the DS3231 every 5 seconds. You can confirm that the time is set correctly.

Should you not get any output after ~20s push the reset button on your arduino and wait again.

If it is still not working verify the sketch on the Arduino and/or upload it again. (Important: Close the Port in the Java application before uploading to the Arduino; Otherwise it will block the serial port!) Then reset the connection in the Java program.

(The Java program is using LocalDateTime to get the current date.)

The time is now set. Have fun!

Shoud you have any unsolvable trouble setting the time please comment below and share your log.txt (in the same folder as the jar) with me. Thanks a lot!

Be the First to Share


    • Leather Challenge

      Leather Challenge
    • Remote Control Contest

      Remote Control Contest
    • Pets Challenge

      Pets Challenge



    1 year ago

    For anyone doing this in 2021 or later, on Windows 10: You must have JDK 8u251. Any earlier and the LocalDateTime function may not be available and any later the program will crash due to the libraries native .dll files not being compatible with that version. I had to actually use the source code and build my own project with said code on Eclipse (Also must use a compatible version, due to the VM, with JDK8). This was a really great (unintentional if I may say) way for me to learn and understand Java classpaths and native file linking a library. I hope to rewrite the code soon with a different library that is supported on the most current version of Java; if/when I do so, I will post back here and also credit LsHallo for his original work.


    3 years ago

    Hi, when setting up for ESP8266 (adding ESP8266WiFi.h, WiFiClient client; and wifi credentials) I get a compile error for RTC.set(now). Can you adapt this program so it would run on a ESP8266? Thanks!!


    Reply 3 years ago

    Unfortunately since the command that gives you the error is from a library which seems to be not compatible with the ESP8266. You will need to use a DS3231 compatible ESP8266 library and adapt my code. 'Now' is the current date and time that is send to the RTC with the command that gives you the error. The exact command may vary with your used library.


    3 years ago

    I tried this on my Arduino using Mac, but it looks like Serial is not opened, even I do use that port for grbl and it is working. Port is /dev/cu.wchusbserial1410. Do you know if maybe that java app has some issues with these china clones of arduino on mac?


    Reply 3 years ago

    I am using clones myself and never had any problems. Though I only use windows computers.
    Make sure to close all instance of the arduino software because it might block the serial port.

    Sometimes only a reboot can free up a serial port that is "stuck" somewhere.


    Reply 3 years ago

    Hi LsHallo,
    I resolved the issue by starting jar from CLI and inspecting the Java logs. Issue was that I did not had librxtxSerial.jnilib and RXTXcomm.jar. I downloaded files, put them into Java extensions folder and it is working like a charm.
    Thanks a lot for creating this useful piece of software :)


    Reply 3 years ago

    Thanks for pointing out. I will add a section lining this out.

    DIY Hacks and How Tos

    Welcome to Instructables. Thanks for sharing the great project. I hope that you will post more of your projects in the future.