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 | maximintegrated.com

Buy DS3231:DS3231 | ebay.com

Datasheet:DS3231 Datasheet | maximintegrated.com

Datasheet:DS1307 Datasheet | maximintegrated.com

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:

https://github.com/PaulStoffregen/Time

https://github.com/JChristensen/DS3232RTC

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

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: DS3231TimeSync.zip (mediafire.com)

Download the source here: DS3231TimeSyncSRC.zip (mediafire.com)

Bitbucket repository: DS3231 RTC Time Sync (bitbucket.com)

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!

Share

    Recommendations

    • PCB Contest

      PCB Contest
    • Safe and Secure Challenge

      Safe and Secure Challenge
    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest

    8 Discussions

    0
    None
    ErikA45

    4 weeks 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!!

    1 reply
    0
    None
    LsHalloErikA45

    Reply 4 weeks 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.

    0
    None
    DraganC5

    3 months ago

    Hi,
    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?

    2 replies
    0
    None
    LsHalloDraganC5

    Reply 3 months 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.

    0
    None
    DraganC5LsHallo

    Reply 3 months 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 :)

    0
    None
    LsHalloAlexR400

    Reply 3 months ago

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

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