Digital Clock using PIC Microcontroller and the DS1307 Real Time Clock – MikroC

Digital Clock with PIC Microcontroller & DS1307 RTC

Digital Clock using PIC Microcontroller and the DS1307 Real Time Clock – MikroC

Digital Clock using PIC Microcontroller and the DS1307 Real Time Clock Circuit Diagram

Figure 1: Circuit Diagram of Digital Clock using PIC Microcontroller and DS1307 RTC

A Digital Clock can be made easily by using PIC Microcontroller and the DS1307 real time clock with an LCD or seven segment display.
The DS1307 is a low power serial real time clock/calendar from Maxim Integrated with full binary coded decimal (BCD) clock/calendar plus 56 bytes of Non Volatile Static Random Access Memory. 
Data and Address are transferred serially through a bidirectional I2C bus. The RTC provides year, month, date, hour, minute and second information. The end date of months is automatically adjusted for months fewer than 31 days including leap year compensation up to year 2100. It can operate either in 24-hour format or 12-hour format with AM/PM indicator. 
DS1307 comes with built-in power sensing circuit which senses power failures and automatically switches to back up supply. Timekeeping operation continues while the part operates from the backup supply. 
The DS1307 RTC uses an external 32.768kHz Crystal Oscillator and it does not requires any external resistors or capacitors to operate.

The PIC 18F2620 has a built-in I2C (Inter-Integrated Circuit) bus, any suitable PIC Microcontroller with I2C bus can be used as well for this project.
Although a digital clock can be built without an external real time clock chip, by using only internal PIC timer, The DS1307 RTC and simillar chips makes the software easier as it takes care of all calendar functions, adjustment of for months with fewer than 31 days, accounting for leap years and other functions which could have make the code more complex. The other advantage of using the DS1307, it comes with built-in power sensing circuit which senses power failures and automatically switches to back up supply so it can keep the real time information when the main circuit loses power. 
Before continuing, be sure to read this article:

Interfacing the DS1307 real time clock with PIC microcontroller.

 Project Circuit diagram

As shown on the circuit diagram above, the DS1307 is connected to PORT C of the PIC which has built in I2C bus (pin 14 and 15 of the 18F2620). Connect two pull up resistors (R1 and R2) for the bus to work. 
Connect a 3V backup battery (B1) to pin VBAT (pin 3) of the DS1307 to backup the device data but if this is not required, this pin can be grounded.
On our circuit, we are using internal oscillator of the PIC and the MCLR is disabled. If an external oscillator is needed, it can be connected to pins 9 and 10 and if the MCLR is needed to reset the PIC, it can be connected to positive supply via a 10K resistor.
Three push buttons connected to PORT C as well are used to set the date and time, once the SET button is pressed, the device enters the setup mode. Pressing the UP button will increment the the Hour and pressing the DOWN button will decrement.
Pressing the SET button again will move the cursor to Minutes, pressing again to seconds than to day and so on.
A 16 x 2 lines LCD display is connected to PORT B.

To learn more how to use an LCD, refer to the article: 

Interfacing LCD Display with PIC microcontroller.

The I2C debugger is shown on the circuit for simulation purpose only. The MCLR pin is disabled in software.

Watch video Tutorial part 1:


There are many DS1307 breakout boards that you can buy to simplify the construction of this project. You can also save lot of money by designing your own PCB for this project instead of buying a breakout board since electronic components are really cheap.

Buy Real Time Clock Chip/Module from our Online Store

As for the PCB, you don’t have to pay a fortune these days for a quality professional PCB, the same for SMD component assembly if you need to miniaturize your board.

There are many PCB companies in the world that can make you good quality PCBs, One of those companies we can recommend is PCBWay a china based PCB manufacturer specializing in PCB prototyping, small volume production and PCB assembly service all under one roof with more than a decade of experience.

As they are specializing in prototyping and small orders, you can get PCBs even on small numbers for a relatively reasonable price for just $5 USD you can get 10 high quality PCBs (size: 100mm x 100mm or smaller). New members receive $5 bonus which can be used to get your first order completely free.

You can pick between these solder mask colours at no extra cost: Green, Red, Yellow, Blue, white and Black. You can also get the price instantly with the online Instant quote as shown on figure 2 below.

PCBWay Home pageFigure 2: PCBWay Instant Quote

To learn more about their services and place an order online, please click on the link below:


Project Code

MikroC Pro for PIC  compiler is used to write the code. MikroC Pro for PIC provides built-in libraries for I²C devices. DS1307 works as a slave device on the I²C bus. 
Registers containing the date and time information can be obtained by implementing a START and followed by device identification address. Then each registers can be accessed sequentially by using its address until a STOP condition is executed. 
Device Address of the DS1307 is 0x68 = 1101000 (page 12 of datasheet). 
More details can be found from the DS1307 Datasheet.
DS1307 RTC is fully Binary Coded Decimal (BCD) clock/calendar. So the data read from DS1307 should be converted to required format according to our needs and data to be written to DS1307 should be in BCD format as well.
MikroC Library functions to display characters to LCD need Character or String Data. So data to be displayed in the LCD Screen should be or converted to String. 
Addition and Subtraction cannot be directly applied on BCD when updating the time and date. The functions to update data should convert it first from BCD to Decimal and vice versa when needed.
The Bcd2Dec() function convert BCD to Decimal so we can do some basic calculations and the Dec2Bcd() function convert Decimal to BCD so we can write to the DS1307 Register in BCD.

Hour, Minute, Second, Date, Month and Year data are stored in DS1307 in separate 8-bit registers in BCD format as shown in the DS1307 Datasheet on page 8. We read the data of these registers to access time/date. The define MSB() converts most significant 4 bits to corresponding character and the define LSB()  converts least significant 4 bits to corresponding character.  When writing to the hour register, Bit 6 is used to select the 24-hour or 12-hour mode selection bit. When this bit is made high (1), 12-hour mode is selected and Bit 5 will represent AM/PM (Logic High represents PM).

Watch video Tutorial part 2:

Full Project Source Code:

You can download the full project files (MikroC source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files). 

Download Digital Clock MikroC Source Code

Download  Digital Clock Proteus Schematic 

Share this post

has been added to your cart:
Select your currency