Wednesday, March 20, 2019

Simple Digital Clock with PIC16F628A and DS1307 and 7-Segment LED display

In this new project I am again using PIC16F628A microcontroller. The goal is simple digital clock with 7-segment LED display and the clock will have no additional functionality - no alarm, no seconds digits, no date. The latter can be added in the software though. For the RTC chip I chose DS1307. For the LED display I used Kingbright CC56-21SRWA.

This is the schematic I concocted. The microcontroller works with it's internal oscillator at 4 MHz to save 2 extra pins. Reset pin (MCLR) is also used as input for one of the buttons. All segments are connected to PORTB and COMs are connected to PORTA. RTC chip is also connected to PORTA.
The schematic is extremely simple and I assembled it on a breadbord for a quick test:

It worked as expected. The refresh rate of the digits is about 53Hz and there is no visible flickering. Because of the multiplexing the digits are dimmer and to compensate this the current through segments must be higher. I tested it with different values for current limiting resistors R1-R7 and below 220 Ohm the microcontroller starts to misbehave - some of the digits start to flicker. 220 Ohm and above seems OK. The two dots in the middle are connected to the SQW pin of the DS1307. This pin is configured as 1 Hz square wave output. It is an open drain output, so in order to work it has to have pull-up resistor. I searched and didn't find anywhere information about current capability of this pin. I tested with 330 and 470 Ohm and it didn't burn :) To be on the safe side I left the 470 Ohm one - it's a little dimmer than the rest of the segments, but still well visible.
There are two buttons  for adjusting the time - one for the hours and one for the minutes.
There is one unused pin left - RB7, which can be used for additional functionality. For example a buzzer can be connected  and an alarm function can be added to the software.

The software is written and compiled with MikroC Pro and uses its build in software I2C library for communicating with RTC chip. If someone wishes to use MPLAB software for compiling the code he should write his own I2C functionality from scratch. 
Here is a short video demonstrating how it works:

Software code and HEX file: DigitalClock
If you use PICkit 3 standalone software to upload HEX code to the microcontroller, then in the "Tools" menu check the option "Use VPP First Program Entry".


  1. It worked right away, but it needed at least 6.5V for the digits to show up. This voltage is the absolute maximum for the MCU and close to maximum for the RTC. So, it would be nice to have this work with 5V. Did your circuit worked with 5V?

    1. Hi,
      The schematic work with 5V power supply. Check the values of the resistors R1-R7. If your 7-segment indicator is green maybe the forward voltage of the LEDs inside is greater and the resulting current trough them is smaller. If this is the case, you can try reducing the vaues of the R1-R7

    2. I am using the exact same display as yours (CC56-21SRWA) in RED color. I will try to reduce R1-R7 a bit to see what happens. Could you share the Eagle files or libraries for the parts? I would like to design a PCB for this.

  2. hi the code please?
    link not working

  3. could you teach me how to write and how to read it please