“This Arduino based digital clock is synchronized by the 60Hz power line. It has a simple and inexpensive common anode 4 digit 7 segment display which shows hours and minutes. It uses a cross over detector to detect when the incoming 60Hz sine wave crosses the zero voltage point and derives a 60 Hz square wave.
Over short time periods the frequency of the incoming sine wave from the power line may vary very slightly due to load, but over long periods of time it averages to 60Hz very precisely. We can take advantage of this to derive a timing source to synchronize our clock.
There are two versions of the circuit depending on whether you want to use a transformer with a center tap or one without, in either case the circuit operation is almost identical. For this build I used a wall adapter (no center tap) that outputs 12V AC. I will use this design (Digital Clock1 Circuit Diagram) for the circuit description. Note that it is important to use a wall adapter that outputs 12V AC not 12V DC so that we can tap into the AC sine wave for timing. You could probably also use a transformer that outputs 9V AC, remove R19 and get it to work also, but 12V is very commonly available. This is how the circuit works:
120V AC at 60Hz is converted to 12V AC by transformer TR1. This is fed to diode D4 and rectified so that only +ve voltage is fed to, and smoothed to approximately DC with ripple, by the capacitor C3. The voltage on C3 is fed to the 7805 voltage regulator (U6) via resistor R19. R19 is used to reduce the voltage on C3 which in my case was measured at approximately 15VDC. This can be regulated by the 7805 but with this level of input the 7805 must drop approximately 10VDC and as a result gets quite hot. By using R19 to drop the voltage to about 10VDC we prevent U6 from heating up too much. So this is not an efficient power conversion technique, but it works for our purposes. NOTE: use at least a 1/2W resistor or more here. The circuit draws about 55 ma, so power dissipation in R19 is about 1/3W based on P=I*2R or P = 55ma x 55ma x 120 ohms = 0.363W. Next U6 outputs pure 5V DC with C4 and C5 on the output to filter any noise on the 5V power line. This 5V DC powers all ICs on the board. From TR1 we also take a sample of the unfiltered AC signal and feed it into potentiometer RV1 which is used to adjust the level fed to the cross over detector. R18 and R17 form a voltage divider to further reduce the level of AC voltage coming in. Remember this is coming in at 12V AC and we need to reduce it to less than 5 V so that it will work with our cross over detector which is only powered by 5VDC. R15 and R16 provide current limiting while D1 and D2 are intended to prevent overdriving of op-amp U5. In the configuration shown U5’s output on pin 1 will alternate between +5V and 0V each time the incoming sine wave changes from positive to negative. This generates a 60 Hz square wave which is fed to the microcontroller, U4. The program loaded on U4 then uses this 60Hz square wave to increment the clock every minute and hour. How this is done will be discussed in the section on the software program and in the software comments. U7 the 74HC595 shift register is used because we have a limited number of digital pins on the microprocessor, so it is used to expand the number of outputs. We use 4 digital pins on the microprocessor but can control 7 segments on the display via the 74HC595. This is accomplished by shifting predetermined patterns of bits, stored in the microcontroller, and which represent each digit to be displayed, into the shift register. The display used here is common anode, so we need to invert the signal levels coming out of the the 74HC595 in order to turn on a segment. When a segment should be turned on the the signal coming out of the 74HC595 output pin will be at +5V, but we need the pin it is feeding on the display to be at 0V in order to turn on that display segment. So to do this we need hex inverters U2 and U3. Unfortunately one inverter IC can only handle 6 inversions so we need two of them even though on the second one we only use one of the 6 gates. Wasteful unfortunately. You might ask why not use a common cathode type display here and eliminate U2 and U3? Well the answer is you can, I just happen to have a common anode type in my parts supply. If you have or want to use a common cathode type display just eliminate U2 and U3 and rewire Q1 - Q4 so that the transistor collectors are connected to the display pins and the transistor emitters are connected to ground. Q1 - Q4 control which of the four 7-segment displays is active. This is controlled by the microcontroller, via the pins connected to the base of transistors Q1 - Q4. The increment and set buttons will be used to manually set the correct clock time when it comes to actually using the clock. When the Set button is pressed once the Increment button can be used to step through the hours shown on the display. When the Set button is pressed again, the increment button can be used to step through the minutes shown on the display. When the Set button is pressed for a third time, the time is set. R13 and R14 pull the microcontroller pins associated with these buttons low when not in use. Note that here we have taken U4 (Atmega328p) off the typical Arduino UNO prototype board and put it on prototype board with the rest of our circuit. To do this we must at a minimum provide crystal X1 and capacitors C1 and C2 to provide a clock source for the microcontroller, tie pin 1, the reset pin, high and provide 5VDC power.”