mKgDnSpHf

DS3231 Real-Time Clock (RTC) Module with EEPROM

 
Overview
  Back in 2022, I actually built a DS3231 board. My soldering skills weren't great then, so I used a lot of through-hole components;
                                         Figure 1 shows the DS3231 module drawn in 2022.
Two years later, in 2024, my soldering skills were barely passable, and I happened to have some stock available at the end of June, so I started this project. The board's dimensions are 50x25mm. The backup battery uses the most common CR2032 battery on the market. It's not too small, but it's not too big either; it can be used on breadboards, perforated boards, or directly connected via jumpers. The circuit diagram is very simple. An AT24C02 memory is added to the IIC bus to store some operational information. To prevent address conflicts with other EEPROMs, R6, R7, and R8 are shorting blocks used to change its communication address. Please pull them high or low as needed. Later, I plan to see if I can use it to make a custom time switch.
                                      Figure 2. The second DS3231 module I drew in 2024.
  I included several programs for this module, including a program to measure the address of IIC devices to determine the addresses of all devices on the current IIC bus, and an Arduino library file for DS3231 read/write that I wrote in 2022. Since I am using an STM32duino, you need to set the IIC bus to pins PB6 and PB7 for testing. Two more points to note: the DS3231 should not be powered by more than 3.3V, as this may cause it to burn out. Also, my board's SQW pin is not pulled up. If you need to use SQW to light up the LED, you need to add an external pull-up resistor and reduce the resistance of R9 (you can see that I added a resistor on my board, from 3.3V to SQW. This way, the SQW pin can drive the LED).
  The following is a program introduction:
1. Why did I write this library: Actually, in the video I introduced about the DS1307, the author wrote a library file for the DS3231. Originally, I also wanted to utilize this feature, but it has two serious problems. First, it returns a string value, while sometimes we need to compare times, splitting the string and converting it to a number. This requires code, which is inconvenient. Furthermore, the way foreigners set the time and format is different from Chinese habits, making it easy to make mistakes; I frequently set the time incorrectly myself. The second problem is more critical: I use STM32duino a lot, and these boards throw errors when using libraries written by foreigners, making this RTC unusable on STM32F. Actually, my main goal is to learn how to write libraries, as it greatly simplifies my code and is an important way to understand Arduino. Contributing to others is also a reason; I've always used open-source libraries for free, so I should contribute my own library so everyone can use them too! Therefore, I decided to write this library myself to gain a fresh understanding of this chip.
                                                   Figure 3. Back of the module
. 2. Executable commands in this library file:
Write time ds3231.SetTime1(a,b,c); This command is responsible for writing the set time into the DS3231 register area; a, b, and c are integer variables, corresponding to hours, minutes, and seconds respectively.
Write time ds3231.SetTime2(d,e,f,g); This command is responsible for writing the set time into the DS3231 register area; d, e, f, and g are integer variables, corresponding to year, month, day, and week respectively;
Write SQW frequency blink ds3231.sqw(h); This command is responsible for setting the DS3231 SQW pin to output a frequency pulse. When h=0, no output is performed; when h=1, a 1Hz pulse is output. Other frequency pulses are not performed, and in fact, other frequency pulses are not used.
Read time ds3231.YEAR(); This command reads the year from the internal register area of ​​the DS3231, an integer variable.
`ds3231.MOUNTH()` reads the month (integer variable) from the internal register of the DS3231.
`ds3231.DATE()` reads the day (integer variable) from the internal register of the DS3231.
`ds3231.WEEK()` reads the week (integer variable) from the internal register of the DS3231.
`ds3231.HOUR()` reads the hour (integer variable) from the internal register of the DS3231.
`ds3231.MIN()` reads the minute (integer variable) from the internal register of the DS3231.
`ds3231.SEC()` reads the second (integer variable) from the internal register of the DS3231.
Read the temperature using `ds3231.temp()`; this command reads the temperature from the internal register of the DS3231, a floating-point variable.
                                           Figure 4. The module is being tested on a self-made Nucleo-48 Kawasaki mirror.
3. This library is based on the Wire library and requires the Wire library to be used. However, the Wire library is usually included with the Arduino IDE. Compatibility has been tested with STM32duino, ATMEGA8A series, and ordinary Arduino, and all can operate normally.
Test method: Burn the demo in the attachment to the MCU. After opening the serial port assistant, you will receive the following message, indicating a successful test.
                              Figure 5. Message returned by the MCU to the serial port assistant (temperature/year/month/day/hour/minute/second).
I'm going to participate in the CW32 training camp later. I hope the project in the training camp will be successful on the first try! See you in the next open-source project! Thank you for watching.
 
参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2026-03-27 03:37:40

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号