Skip to content

Commit 7d8252a

Browse files
Updated to version 1.1.1 - Improved time format support, added Python tools, and updated documentation
1 parent bac08cb commit 7d8252a

File tree

16 files changed

+533
-197
lines changed

16 files changed

+533
-197
lines changed

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# CHANGELOG - RTC_DS1307_Library
2+
3+
This file documents the most important changes in each version of the **RTC_DS1307_Library**.
4+
5+
---
6+
7+
## [1.1.1] - 2025-01-30
8+
### Improvements and New Features
9+
- Support for 12h/24h: You can now switch between time formats using the `SET_FORMAT` command.
10+
- Added two Python tools in `tools/`:
11+
- `rtc_sync.py`: Synchronizes the RTC with the system time (UTC or Local).
12+
- `local_test.py`: Detects the system timezone and displays UTC and local time.
13+
- Added datasheet:
14+
- `DS1307_Maxim.pdf`
15+
16+
### Bug Fixes
17+
- Fixed an issue with setting the year field when updating the date.
18+
19+
---
20+
21+
## [1.0.0] - 2025-01-27
22+
### Initial Release
23+
- Compatibility with **Arduino, ESP32, and ESP8266**.
24+
- Basic functionality:
25+
- Read and write time on the DS1307.
26+
- Configure and read internal RAM memory.
27+
- Control the clock output (`SQW`).
28+
- Support for synchronization with `TimeLib`.
29+
30+
---
31+
32+
Versioning Format:
33+
This changelog follows the [SemVer](https://semver.org/lang/en/) scheme with **MAJOR.MINOR.PATCH** versions:
34+
- `MAJOR`: Incompatible changes with previous versions.
35+
- `MINOR`: New backward-compatible features.
36+
- `PATCH`: Bug fixes or minor improvements.

README.md

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,87 @@
22

33
The **RTC_DS1307_Library** allows easy interaction with the DS1307 Real-Time Clock (RTC) module, offering advanced functions for managing time and additional configurations such as square wave output and NV-RAM.
44

5+
## What's New in v1.1.1
6+
7+
This release includes significant improvements and new features:
8+
9+
- **Added support for 12-hour and 24-hour time formats.** You can now switch between these formats using the `SET_FORMAT` command.
10+
- **Added two Python tools in the `tools/` directory:**
11+
- `rtc_sync.py`: Synchronizes the RTC with the system time (supports UTC and local time).
12+
- `local_test.py`: Detects the system timezone and displays UTC and local times.
13+
- **Updated documentation.** The repository now includes three versions of the DS1307 datasheet (`Dallas`, `Maxim`, and `Spanish`).
14+
515
## Features
16+
617
- Configuration and retrieval of time in Unix timestamp format.
18+
- Support for both **12-hour and 24-hour formats** with AM/PM indication.
719
- Reading and writing to the DS1307's battery-backed RAM.
820
- Control of square wave output with configurable frequencies (1Hz, 4kHz, 8kHz, and 32kHz).
921
- Support for synchronization with the [TimeLib](https://github.com/PaulStoffregen/Time) library.
10-
- Compatible with Arduino, ESP32, and ESP8266.
22+
- Compatible with **Arduino, ESP32, and ESP8266**.
1123

1224
## Installation
25+
26+
### **Option 1: Arduino Library Manager (Recommended)**
27+
28+
The library is available directly in the **Arduino Library Manager**.
29+
30+
1. Open the **Arduino IDE**.
31+
2. Go to `Sketch -> Include Library -> Manage Libraries...`.
32+
3. Search for **RTC_DS1307_Library**.
33+
4. Click **Install**.
34+
35+
### **Option 2: Manual Installation**
36+
1337
1. Download this repository as a ZIP file.
1438
2. Open the Arduino IDE.
1539
3. Go to `Sketch -> Include Library -> Add .ZIP Library...`.
1640
4. Select the downloaded ZIP file.
1741
5. The library is now ready to use.
1842

1943
## Basic Example
20-
This example demonstrates how to initialize the DS1307 module, configure it with the current time, and continuously read the time. It is ideal for verifying the basic functionality of the RTC and establishing a foundation for more advanced projects.
44+
45+
This minimal example demonstrates how to initialize the DS1307 module and retrieve the current time:
2146

2247
```cpp
2348
#include <Wire.h>
2449
#include <DS1307Lib.h>
25-
#include <TimeLib.h>
2650

2751
void setup() {
2852
Serial.begin(9600);
2953
Wire.begin();
30-
31-
if (!RTC.begin()) {
32-
Serial.println("DS1307 not detected");
33-
while (1);
34-
}
35-
36-
// Configure the RTC with the current time
37-
RTC.set(now());
54+
RTC.begin();
3855
}
3956

4057
void loop() {
41-
// Read and display the current time
42-
time_t currentTime = RTC.get();
43-
Serial.print("Current time: ");
44-
Serial.println(currentTime);
58+
Serial.println(RTC.get());
4559
delay(1000);
4660
}
4761
```
4862

49-
## Contributions
50-
If you encounter an issue or have ideas to improve this library:
51-
1. Create an **issue** in the repository.
52-
2. You can also submit a **pull request** with your suggestions.
63+
## New Commands
64+
65+
- **SET_FORMAT**: Switch between 12-hour and 24-hour formats.
66+
```
67+
SET_FORMAT 12 // Sets the RTC to 12-hour mode
68+
SET_FORMAT 24 // Sets the RTC to 24-hour mode
69+
```
70+
- **Python Tools for Synchronization**
71+
- `rtc_sync.py COM5 utc` Syncs the RTC with system UTC time.
72+
- `rtc_sync.py COM5 local` Syncs the RTC with system local time.
5373

5474
## License
75+
5576
This library is distributed under the GNU Lesser General Public License (LGPL) version 3. See the `COPYING.LESSER.txt` file for more details.
5677

5778
## Additional Resources
58-
- [DS1307 Datasheet](docs/DS1307_datasheet.pdf)
79+
80+
- [DS1307 Datasheet - Dallas](docs/DS1307_Dallas.pdf)
81+
- [DS1307 Datasheet - Maxim](docs/DS1307_Maxim.pdf)
82+
- [DS1307 Datasheet (Spanish)](docs/DS1307_Dallas_es.pdf)
5983
- Included examples:
6084
- `RTC_ClockOut.ino`: Configuring the square wave output.
6185
- `RTC_Ram.ino`: Reading and writing to the RTC's RAM.
62-
- `RTC_Time.ino`: Configuring and reading time.
86+
- `RTC_Time.ino`: Configuring and reading time.
87+
- `RTC_Full.ino`: Complete example with all features.
88+
File renamed without changes.
File renamed without changes.

docs/DS1307_Maxim.pdf

215 KB
Binary file not shown.

examples/RTC_ClockOut/RTC_ClockOut.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ void setup() {
6464
Serial.begin(9600);
6565
while (!Serial); // Wait for serial port to be ready (needed on boards like Leonardo)
6666

67-
// Set A3 pin as input
67+
// Set A3 pin as input for SQWOUT pin
6868
pinMode(A3, INPUT);
6969
// Set LED_BUILTIN as output
7070
pinMode(LED_BUILTIN, OUTPUT);
7171

72-
// Optionally synchronize with RTC; here we mostly focus on clock out usage.
73-
setSyncProvider(RTC.get);
72+
RTC.begin();
73+
if (timeStatus() != timeSet) {
74+
Serial.println("Unable to sync with the RTC");
75+
}
7476
}
7577

7678
// ---------------------- loop() ----------------------

examples/RTC_Full/RTC_Full.ino

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* - SET_UNIX: Set the RTC time using a Unix timestamp.
1616
* - GET_TIME: Retrieve the current time in human-readable format.
1717
* - GET_UNIX: Retrieve the current time as a Unix timestamp.
18+
* - SET_FORMAT: Command to switch between 12-hour and 24-hour formats.
1819
* - SET_CLOCKOUT: Configure the clock output pin with various settings.
1920
* - READ_RAM: Read a block of bytes from the DS1307's battery-backed RAM.
2021
* - WRITE_RAM: Write a block of bytes to the DS1307's battery-backed RAM.
@@ -59,6 +60,7 @@ void processSetTime(String params);
5960
void processSetTimeUnix(String params);
6061
void sendUnixTimestamp(String params);
6162
void sendCurrentTime(String params = "");
63+
void processSetHourFormat(String params);
6264
void processSetClockOut(String params);
6365
void processCheckRTC(String params);
6466
void processReadRAM(String params);
@@ -70,6 +72,7 @@ Command commandTable[] = {
7072
{"SET_UNIX", processSetTimeUnix},
7173
{"GET_UNIX", sendUnixTimestamp},
7274
{"GET_TIME", sendCurrentTime},
75+
{"SET_FORMAT", processSetHourFormat},
7376
{"SET_CLOCKOUT", processSetClockOut},
7477
{"CHECK_RTC", processCheckRTC},
7578
{"READ_RAM", processReadRAM},
@@ -82,17 +85,14 @@ const int commandCount = sizeof(commandTable) / sizeof(Command); // Number of co
8285
void setup() {
8386
Serial.begin(9600); // Set up serial communication at 9600 baud
8487
while (!Serial); // Wait for the serial port to be ready (necessary for boards like Leonardo)
85-
// Set pin A3 as input
88+
// Set A3 pin as input for SQWOUT pin
8689
pinMode(A3, INPUT);
87-
// Set the built-in LED pin as output
90+
// Set LED_BUILTIN as output
8891
pinMode(LED_BUILTIN, OUTPUT);
8992

90-
// Set up the synchronization function
91-
setSyncProvider(RTC.get);
93+
RTC.begin();
9294
if (timeStatus() != timeSet) {
9395
Serial.println("Unable to sync with the RTC");
94-
} else {
95-
Serial.println("RTC has been synchronized");
9696
}
9797
}
9898

@@ -138,7 +138,9 @@ void loop() {
138138

139139
// Auxiliary function implementations
140140

141-
// Processes the SET_TIME command (human-readable format)
141+
/**
142+
* SET_TIME - Sets time in human-readable format: "YYYY/MM/DD HH:MM:SS"
143+
*/
142144
void processSetTime(String timeString) {
143145
int year, month, day, hour, minute, second;
144146
if (sscanf(timeString.c_str(), "%d/%d/%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second) == 6) {
@@ -150,7 +152,9 @@ void processSetTime(String timeString) {
150152
}
151153
}
152154

153-
// Processes the SET_TIMEU command (Unix timestamp)
155+
/**
156+
* SET_UNIX - Sets time based on a Unix timestamp
157+
*/
154158
void processSetTimeUnix(String unixString) {
155159
unsigned long unixTime = unixString.toInt(); // Convert the string to a long integer
156160
if (unixTime > 0) {
@@ -205,20 +209,39 @@ void processCheckRTC(String params) {
205209
}
206210
}
207211

208-
// Sends the current time in a human-readable format
212+
/**
213+
* GET_TIME - Prints the current time in "YYYY/MM/DD HH:MM:SS"
214+
*/
209215
void sendCurrentTime(String params) {
210-
Serial.print(year());
211-
Serial.print("/");
212-
Serial.print(month());
213-
Serial.print("/");
214-
Serial.print(day());
215-
Serial.print(" ");
216-
Serial.print(hour());
217-
printDigits(minute());
218-
printDigits(second());
219-
Serial.println();
216+
RTC.readHourFormat(); // Update format configuration
217+
218+
Serial.print(year());
219+
Serial.print("/");
220+
Serial.print(month());
221+
Serial.print("/");
222+
Serial.print(day());
223+
Serial.print(" ");
224+
225+
int h = hour(); // Get the hour in 24h format
226+
227+
if (RTC.is12HourFormat) {
228+
RTC.isPMFlag = isPM(h); // Determine if it's AM or PM
229+
int hour12 = (h == 0) ? 12 : (h > 12 ? h - 12 : h); // Convert 0->12 AM, 13->1 PM, etc.
230+
231+
Serial.print(hour12);
232+
printDigits(minute());
233+
printDigits(second());
234+
Serial.println(RTC.isPMFlag ? " PM" : " AM");
235+
} else {
236+
Serial.print(h);
237+
printDigits(minute());
238+
printDigits(second());
239+
Serial.println();
240+
}
220241
}
221242

243+
244+
222245
// Sends the current time as a Unix timestamp
223246
void sendUnixTimestamp(String params) {
224247
time_t currentTime = RTC.get(); // Get the current time from the RTC
@@ -237,6 +260,23 @@ void printDigits(int digits) {
237260
Serial.print(digits); // Print the number
238261
}
239262

263+
264+
265+
/**
266+
* SET_FORMAT - Set hour format (12/24)
267+
* Usage: "SET_FORMAT 12" or "SET_FORMAT 24"
268+
*/
269+
void processSetHourFormat(String params) {
270+
if (params.equals("12") || params.equals("24")) {
271+
RTC.readHourFormat();
272+
RTC.is12HourFormat = params.equals("12");
273+
RTC.writeHourFormat();
274+
sendCurrentTime(); // Print the newly set time
275+
} else {
276+
Serial.println("ERROR");
277+
}
278+
}
279+
240280
/*
241281
processReadRAM:
242282
- Parameters: "ADDHex lengthDecimal"

examples/RTC_Ram/RTC_Ram.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ void setup() {
6464
Serial.begin(9600);
6565
while (!Serial); // Wait for serial port to be ready (needed on boards like Leonardo)
6666

67-
// A3 is used as input to read the SQW/OUT pin of the DS1307 (clock out signal).
68-
pinMode(A3, INPUT);
69-
// LED_BUILTIN is used here as an indicator for the clock out signal.
70-
// The loop() function will read A3 and mirror its state onto the LED.
67+
// Set A3 pin as input for SQWOUT pin
68+
pinMode(A3, INPUT);
69+
// Set LED_BUILTIN as output
7170
pinMode(LED_BUILTIN, OUTPUT);
7271

73-
// Optional: set up TimeLib's synchronization with DS1307
74-
// (Not strictly required for RAM operations, but included for consistency)
75-
setSyncProvider(RTC.get);
72+
RTC.begin();
73+
if (timeStatus() != timeSet) {
74+
Serial.println("Unable to sync with the RTC");
75+
}
7676
}
7777

7878
// ---------------------- loop() ----------------------

0 commit comments

Comments
 (0)