Library 15- DS1307 Real Time Clock for STM32F4

In my 15th library, I will show you, how to operate with DS1307 Real Time Clock I2C module from maxim integrated.

DS1307 STM32F4xx resultThe DS1307 serial real-time clock (RTC) is a lowpower, full binary-coded decimal (BCD) clock/calendar plus 56 bytes f NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12- hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

I have DS1307 Shield from ebay. They are almost free and you get battery too.

Do not use this library for future projects. Use internal RTC on STM32F4xx instead.

DS1307 features

  • Real time clock
  • Counts seconds, minutes, hours, day in a week, date, month and year
  • I2C interface
  • External oscillator 32.768kHz required
  • Binary-coded numbers
  • Square wave output pin



  • Set and get date and time from DS1307
  • Set/Get date/time from DS1307 separatelly
  • Enable/disable square wave output pin
  • Version 1.1 – July 30, 2014
    • Square wave output pin can be enabled
  • Version 1.2 – September 21, 2014
    • You can now check on iintialization if device is connected


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx I2C
  • TM
    • TM DELAY
    • TM I2C
    • defines.h
DS1307 STM32F4xx Description
VCC 5V Positive voltage
GND GND Ground
SDA PB7 I2C1, PinsPack 1
SCL PB6 I2C1, PinsPack 1

In case, you want custom I2C pins, you can set it in defines.h file (for corresponding pins look at my I2C library):

Datetime struct

There is a struct for date and time you are working with. It is used to set and to get time to/from DS1307.

Write date/time

You can write all (date and time) to DS1307 or you can write each thing separatelly.

Read date/time

You can read all (date and time) from DS1307 or you can read each thing separatelly.


DS1307 has one pin, which can be used to output frequency. This frequency can be 1Hz, 4096Hz, 8192Hz or 32768Hz. Also, pin can be set high or low. Pin has open-drain circuit, that means you need external pull up resistor to get pin working properly.

For this purpose I made 2 new functions

To use this functions, you also need enumeration typedef

Why use this pin? If you set this pin to 1Hz, then you can with your software update clock, no need to constantly check DS1307’s registers, because this takes loong time for one STM32F4xx device. Seconds are updated on falling edge of SQW/OUT pin, if it is set to 1Hz output.


Project available on Github, downlaod library here.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...