Interface DS3231 RTC module with PIC and Arduino

Introduction

The DS3231 RTC module integrates DS3231 Real Time Clock (RTC) IC which has an internal crystal and a switched bank of tuning capacitors. The temperature of the crystal is continuously monitored, and the capacitors are adjusted to maintain a stable frequency. Compared to other RTC solutions, this DS3231 module drifts less than a minute per year, even in extreme temperature ranges. This makes DS3231 module suits very well for time critical applications that cannot be regularly synchronized to an external clock.

The I2C interface to this module is very straightforward and virtually identical to the register address of the popular DS1337 and DS1307 RTCs, which means the existing code for Arduino, Basic Stamp, Cubloc, and other controllers should work with no modification.

In this tutorial, users will be guided to interface the DS3231 RTC module with both PIC and Arduino. The platform to be used for the PIC interface is the SK40C board with PIC16F887 while the platform to be used for Arduino interface is the BBFUINO board, which is Arduino compatible. Hence, this tutorial is divided into two parts as listed below:

Part I : Interface DS3231 RTC module with SK40C + PIC16F887

Part II: Interface DS3231 RTC module with BBFUINO

Hardware Required:

Electronic Components:

Part I: Interface DS3231 RTC module with SK40C + PIC16F887

  • Enhanced 40 pins PIC Start-Up Kit (SK40C) – 1
  • IC PIC 16F887(IC-PIC-16F887) – 1
  • LCD (16×2) (DS-LCD-162A) – 1
  • Straight Pin Header (Male) 1×40 Ways (CN-PH-M140S) – 1

        

                 SK40C                            IC-PIC-16F887                                 UIC00B

    

       DS-LCD-162A                 CN-PH-M140S

Part II: Interface DS3231 RTC module with BBFUINO

  • Breadboard Friendly (Arduino Compatible) (BBFUINO) – 1
  • USB to UART Converter (UC00A) – 1
  • USB MiniB Cable (2.0) (WR-USB-M) – 1
  • LCD (8×2) (DS-LCD-082A) – 1
  • Straight Pin Header (Male) 2×40 Ways (CN-PH-M240S) – 1
  • Straight Female Header 2×10 Ways (CN-PH-F210S) – 1

        

            BBFUINO                              UC00A                                     WR-USB-M

        

           DS-LCD-082A        CN-PH-M240S                  CN-PH-F210S

 Applicable to Part I and Part II

  • DS3231 RTC Module (BB-3231RTC) – 1
  • Breadboard GL-12 (C&D) (BD-BB-0617) – 1
  • Male to Male Jumper Wire (WR-JW-MM65) – 1 pack
  • Adapter 12V 2A (AD-12-2) – 1

        

        BB-3231RTC                     BD-BB-0617                              WR-JW-MM65



         AD-12-2

 Tools and Equipment:

Applicable to Part I and Part II

  • Soldering Iron
  • Solder Lead
  • Solder Paste (optional)

Software Required:

 Part I: Interface DS3231 RTC module with SK40C + PIC16F887

 Part II: Interface DS3231 RTC module with BBFUINO

  • Arduino IDE, latest version can be downloaded from Arduino official website.
  • Time Library for Arduino, can download here.
  • DS1307 Library for Arduino, can download here.

 Related Materials:

 Part I: Interface with SK40C + PIC16F887

 Part II: Interface with BBFUINO

Applicable to Part I and Part II

  • User manual and other related materials of BB-3231RTC, can be downloaded from BB-3231RTC.

    *Users are encouraged to gone through the user manuals of products stated above for more information on how to use them.

Procedure:

 Part I: Interface DS3231 RTC module with SK40C + PIC16F887

  1.  Firstly, install a 2×16 LCD onto the SK40C board by following the steps listed below:
    • Cut out 1×16 ways pin header (Male) from the 1×40 ways pin header (Male).
    • Solder it to 16-pins connection pad of the 2×16 LCD as shown in Figure 1 below:

   

Figure 1: Solder 1×16 ways pin header (Male) to LCD

    •  Then, solder the resulted LCD onto the place reserved for LCD on SK40C board.

    

Figure 2: Reserved place for LCD on SK40C board

  1. Front View


Rear view

Figure 3: Solder LCD onto the SK40C board

    •  Solder the LCD B/L connection (JP8) on SK40C board for LCD backlight purpose.


Figure 4: LCD backlight connection on SK40C board

 2. Plug the PIC16F887 and 20 MHz crystal provided with SK40C board onto the SK40C as shown in Figure 5 below.


Figure 5: Plug in 20 MHz crystal and PIC16F887 to the SK40C board

 3.Next, solder the 1×7 ways pin header (Male) provided to the DS3231 RTC module (BB-3231RTC).


Figure 6: Solder pin header (Male) to DS3231 RTC module

4. Insert the coin battery provide with the module into the battery holder at the back side of the DS3231 RTC module.

        

Figure 7: Insert coin battery into battery holder of the DS3231 RTC module

5. Plug the resulted SK40C board and DS3231 RTC module into the breadboard in orientation as shown in Figure 8 below


Figure 8: SK40C board and DS3231 RTC module on the breadboard

6. Then, construct the connection between SK40C with PIC16F887 with DS3231 RTC module using the male-to-male jumper wires based on the schematic provided below.


Figure 9: Schematic of connections between SK40C with PIC16F887 and DS3231 RTC module

*RC3 pin of PIC16F887 is multipurpose as SCK and SCL

*RC4 pin of PIC16F887 is multipurpose as SDI and SDA

7. The resulted circuit is shown in Figure 10 below.


Figure 10: The interface circuit for DS3231 RTC module with SK40C board with PIC16F887

8. Download the source code zip file named “DS3231 (PIC16F887)” through the “Attachments” section at the bottom of this tutorial.

9.Unzip the folder downloaded. The resulted folder contains MPLAB project files, source codes, hex file and so on.

10.Locate the DS3231(PIC16F887).hex file and load it into the PIC16F887 on SK40C board using UIC00B programmer through software, PICKit2. Please refer to user manuals of SK40C and UIC00B for information on how to load program into microchip through using software, PICKit2.

11.The resulted circuit can be either powered using the UIC00B programmer by setting VDD in the software, PICKit2 or connect an AC-to-DC adapter (12V) to the DC jack socket of the SK40C board.

12. As the circuit is power up, the LCD will show the time and the date is actually start counting from the default time 00:00:00 and date 01/01/01 of the DS3231 RTC module.


Figure 11: DS3231 starts counting from default time and date

 13. The time and date for the DS3231 RTC module can be configured by following steps listed below:

  • Press the SW1 to enter the time and date adjustment mode.’
  • The first parameter selected to be adjusted is the hour. Press SW2 to adjust it.
  • As the parameter selected is finish adjusted, press the SW1 to move to the next parameter.
  • Repeat the adjustment step until all six parameters are adjusted to the desired values.

 *The six parameters in sequence are hour, minute, second, day, month and year.

*After finish adjusted the last parameter (“year” in this case), pressing the SW1 will exit the time and date adjustment mode. In the default mode, the current time and date will be read from DS3231 RTC module and printed on the LCD screen.

*Please refer to comments written in the source codes for more information.

15. The resulted working circuit with time and date of the DS3231 RTC module adjusted is shown in Figure 12 below.


Figure 11: Time and date of DS3231 RTC module is adjusted to desired values

 The Part I is considered finish here.

 Part II: Interface DS3231 RTC module with BBFUINO

1.  Referring to “Hardware Installation” section in the user manual of BBFUINO, install the 2×8 LCD onto the BBFUINO board.

Figure 13: 2×8 LCD on BBFUINO board

 2. Next, please refer to Step 3 and Step 4 in Part I (Interface DS3231 RTC module with SK40C + PIC16F887) for the DS3231 RTC module preparation.

 3. Then, plug the resulted BBFUINO board and DS3231 RTC module into the breadboard in orientation as shown in Figure 14 below.


Figure 14: BBFUINO board and DS3231 RTC module on the breadboard

4. Then, construct the connection between BBFUINO board with DS3231 RTC module using the male-to-male jumper wires based on the schematic provided below.

Figure 15: Schematic of connections between BBFUINO board and DS3231 RTC module

*A4 pin of BBFUINO is multipurpose as SDA

*A5 pin of BBFUINO is multipurpose as SCL

5. The resulted circuit is shown in Figure 16 below.

Figure 16: The interface circuit for DS3231 RTC module with BBFUINO board

 6. The interfacing circuit is considered done here. Now, it’s time to program BBFUINO for interfacing with the DS3231 RTC module.

7. Firstly, download the driver of UART to USB Converter (UC00A) and install it according to the installation guides.

8. After the USB driver is successfully installed, with the UC00A still connected to the computer through USB miniB cable, the COM port assigned to UC00A can be checked through the Device Manager under the Ports (COM & LPT) category. In this case, the COM port is COM 6.

Figure 17: COM port assigned for UC00A

 9. Next, download Arduino Time library and DS1703RTC library zip files through the “Sofware Required” section, Part II.

 10.Unzip both libraries zip file downloaded. Copy the resulted folders (“Time” folder and “DS1307RTC” folder) to the “libraries” folder of the installation directory of the Arduino IDE as shown in Figure 18 below. These two libraries are required for the Arduino source codes for interfacing DS3231 RTC module with BBFUINO.


Figure 18: Copy “Time” and “DS1307RTC” libraries to the libraries folder of the Arduino IDE installation directory

11. After that, download the Arduino source code zip file named “RTC3231_Arduino_Program” through the “Attachments” section at the bottom of this tutorial.

 12. Unzip the source code zip file. The resulted folder contains two source code folders, “RTC3231_Arduino” folder and “SetTime” folder. Inside the “RTC3231_Arduino” folder, contains RTC3231_Arduino.ino file while inside the “SetTime” folder contains SetTime.ino file.

13. Firstly, open the SetTime.ino in the Arduino IDE. By referring to the “Getting Started” section in the user manual of BBFUINO, upload the program into the BBFUINO board through Arduino IDE using UC00A programmer.

*The SetTime.ino is the program to set the DS3231 RTC module with time and date at the moment the code compiled and upload into the BBFUINO.

 14. As the “SetTime” program is successfully uploaded, with the UC00A programmer still connected with the BBFUINO board, open the serial monitor in the Arduino IDE. The starting time and date set for the DS3231 module will be shown in the serial monitor as shown below. The DS3231 RTC module is start counting from this set time and date.

 

Figure 19: Open Serial Monitor in Arduino IDE


Figure 20: Starting time and date set for DS3231 RTC module is shown in Serial Monitor

 15. After that, open the RS3231_Arduino.ino in the Arduino IDE. Upload the program into the BBFUINO board through Arduino IDE using UC00A programmer.

*The DS3231_Arduino.ino is the program to read time and date continuously from DS3231 RTC module and print them on the LCD attached on the BBFUINO board.

 16. As the “DS3231″program is successfully uploaded, the LCD on the BBFUINO will be keep updated by the time and date read from DS3231 RTC module.


Figure 21: The resulted working circuit interfacing DS3231 RTC module with BBFUINO board

*The completed interfacing circuit can be either powered up by the UC00A or connect BBFUINO board with a 12V AC to DC adapter through the DC jack socket on it, but not both at the same time.

 The Part II is considered finish here.

The tutorial of interfacing DS3231 RTC module with PIC and Arduino is considered end here. Have fun with DS3231 RTC module with PIC or Arduino as the real time clock in your daily life. See ya!

Attachments:


Buy

2 thoughts on “Interface DS3231 RTC module with PIC and Arduino

  1. When I try to compile either Arduino program, I get the following erros

    SetTime:10: error: ‘tmElements_t’ does not name a type
    SetTime.ino: In function ‘void setup()’:
    SetTime:20: error: ‘RTC’ was not declared in this scope
    SetTime:20: error: ‘tm’ was not declared in this scope
    SetTime.ino: In function ‘bool getTime(const char*)’:
    SetTime:53: error: ‘tm’ was not declared in this scope
    SetTime.ino: In function ‘bool getDate(const char*)’:
    SetTime:70: error: ‘tm’ was not declared in this scope
    SetTime:72: error: ‘CalendarYrToTm’ was not declared in this scope

    I went to the origional 1.0.3 version of the IDE and get the same thingon both the Set time and the other example.

    any ideas ?

    Thanks
    Eddie Turner, kc4awz

Leave a Reply

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