Arduino and DS3231 real time clock

Arduino with LCD and DS3231 real time clock

After the interfacing of DS1307 real time clock IC with Arduino now I’m going to use DS3231 instead of the DS1307. Related topic link is below:
Arduino real time clock with DS1307

The DS3231 is also a low cost, easy to use and highly accurate real time clock IC which counts seconds, minutes, houres, day of the week, date, month and year. A battery can be connected to the DS3231 to keep the time running in case of main power failure. The DS3231 don’t need any oscillator because it has a built-in one.

Hardware Required:

  • Arduino board
  • DS3231 RTC board
  • 1602 LCD screen
  • 2 x push button
  • 10K ohm variable resistor (or potentiometer)
  • 330 ohm resistor
  • 3V coin cell battery
  • Breadboard
  • Jumper wires

Arduino real time clock with DS3231 circuit:

Arduino DS3231 real time clock with push buttons circuit

In the circuit there are 2 push buttons (B1 & B2) connected to pins 8 and 9 respectively, the two push buttons are used to set time date parameters (minutes, hours, date, month and year). Button B1 selects the parameter and B2 increments the selected parameter.

Arduino real time clock with DS3231 code:
The DS3231 Arduino code is similar to the DS1307 code and it works with both RTC chips.
The Arduino code below doesn’t use any library for the DS3231 RTC, the Wire library is for the communication between the Arduino and the DS3231 using I2C protocol.

The DS3231 works with BCD format only and to convert the BCD to decimal and vise versa I used the 2 lines below (example for minute):
// Convert BCD to decimal
minute = (minute >> 4) * 10 + (minute & 0x0F);

// Convert decimal to BCD
minute = ((minute / 10) << 4) + (minute % 10);

void DS3231_display() : displays time and calendar, before displaying time and calendar data are converted from BCD to decimal format.
void blink_parameter() : this small function works as a delay except that it is interrupted by the buttons B1 (connected to pin 8) and B2 (connected to pin 9). When called and without pressing any button the total time is 10 x 25ms = 250ms. With this function we can see the blinking of the selected parameter with a frequency of 2Hz. So a delay of 250ms comes after the print of the selected parameter and after that delay a 2 spaces is printed which makes the parameter disappears from the LCD and another 250ms delay comes after the print of the 2 spaces.
The complete code is below.

The following video shows project simulation using Proteus:

Downloads:
To be able to simulate this example, Proteus needs the Arduino library which can be downloaded from the link below. After extracting the files (ARDUINO.IDX and ARDUINO.LIB) put it in the Library folder (ex: C:\Program Files\Labcenter Electronics\Proteus 8 Professional\LIBRARY):
Download

Arduino + DS3231 + LCD Proteus simulation file download:
Download


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

15 thoughts on “Arduino and DS3231 real time clock”

  1. Thank you so much for your kind words and heartfelt message. It’s truly inspiring to hear about your passion for exploring and experimenting with Arduino projects at the age of 82! Your lighthouse project with the elevator and the current clock project with servos sound fascinating and creative. It’s evident that you have a great enthusiasm for learning and applying new technologies.

  2. Dear mister,
    You have make a very fine program. On my age of 82 years I feel I like a little shild in concerning to you.
    I have used this program to make a lighthouse with an elevator that go’s up every our and go down every half hour.
    with so many sensors that I have used 2 NANO’s . Communication between them I used LDR’s one side and LED’s on the other side. It was a nice long program. The elevator works with a small stepper. Al this works verywell.
    Now I use your program to make a clock with 2 servo’s. One for the minutes and one for the hours.
    I use the minute-servo every 5 minutes till 30 minutes and than go’s back to 0.
    Allso the hours, every hour forward till 12 and than back to 0.
    I use sinds 2006 the arduino, but as I see on internet all the young peoples I fiel myself as a baby and will learn so mutch in a sort time that I have. But I’am very happy that I find very smart people like you on the internet.
    Thank you and again: Thank you very much for your terrific website.
    With our best regards, F. Muller, The Netherlands.

    1. Simple Projects

      Thank you so much Mr.Muller, you wrote a truly encouraging and motivating words, you are really building an interesting projects.
      Thanks again and best wishes from me.

    1. Assalam O Allaikum..!
      You can do it by adding a 3rd button at pin 10 and use the loop of increment but reverse the things like…. parameter–, and test conditions also I.e if parameter==255 then parameter = 23 or what so ever

  3. This one works also with my LCD type mentioned above. But the delay is to much long and not equivalent with the real time clock’s second speed.
    Please help me.

  4. This is what i looking for.
    I used Arduino nano and all ok.
    I had tried other (many)software but time and date lost every time that power is down.
    Great work,thanks!

  5. if(!digitalRead(8))
    { // If button (pin #8) is pressed
    i++; // Increament ‘i’ for the next parameter
    return parameter;
    }
    Sir can you please explain how this code runs when i value increments after 4. I mean after setting year how lcd goes on normal mode of date & time and blink effects disappears.

    1. Assalam-O-Allaikum..!
      Dear it returns to zero when after setting u press this button i.e. pin8…. because it remains in increment loop only till 4 when 5 it exits and if press again i resets to zero…. hope you will read code again very carefully…

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top