Clock on avr segment indicators. LED clock on a microcontroller

This alarm clock is based on a real-time clock chip, which allows it to operate from a backup power source in the absence of the main one. The set alarm time and operating mode are stored in the non-volatile memory of the microcontroller. Display mode - 24 hour. Contains imitation of “ticking” Time and operating modes are indicated using LED indicators.

Principle of operation

The basis of this clock is the DS1307 microcircuit - a real-time clock that exchanges information with the control controller via an I2C interface. Time indication is carried out through 4 7-segment indicators operating in dynamic mode. Entering and adjusting the time is carried out using 5 buttons: “+ minutes”, “+ hours”, “set”, “alarm” and “reset”. The alarm clock sound signal is output through a standard piezo emitter and is a signal with a frequency of 1 kHz with second pauses.

Atmega48 was chosen as the control microcontroller due to its availability and the presence of the necessary peripherals on board (even in excess). The DS1307 real-time clock is connected to the I2C hardware outputs of the control microcontroller. To operate the DS1307 in stand-alone mode (in the event of a power failure to the main controller), a 3V lithium battery is used for backup power, which will last for several years due to the low power consumption of the chip.

Let's take a closer look at the control program:

The program works on the principle of a flag-timer machine: all states and events are represented in the form of corresponding flags, executed in interrupts of the corresponding timer 1s, 1ms and 263.17ms. The program uses 2 hardware timers.

The clock chip is polled and buttons pressed at an interval of 263.17ms. The 1ms interval is used to generate the bell sound signal, and 1s is used to modulate it. The second interval also controls the blinking of the dot in the 2nd digit of the indicator, separating hours and minutes and also serving as the formation of a “tick”.
Let's consider schematic diagram hours.

Designations and denominations:
S4 - Clock increase
S3 - Increase minutes
S2 - Installation
S1 - Turn on the alarm
S5 - Reset

R6-R10 - 10k
R1-R5 - 510ohm

Supply voltage - 5 volts.

Setup and use

A correctly assembled watch does not require additional adjustment. You just need to set the current time and alarm.
Setting the current time is as follows:
1) Use buttons S1 and S2 to set the current time (the dot between the digits does not blink)
2) Start the clock with button S3
Setting the alarm:
1) Press S3 and make sure that the dot in the 1st digit lights up
2) Set the call time using the S1 and S2 buttons
3) Turn on the call using S4 button
Additional features:
Turn on ticking - hold S4 and press S2 until characteristic sounds appear. It turns off the same way.
Display minutes and seconds - hold S4 and press S1. If you press S3 after this, the seconds will be reset to 00. Return - the same combination.

Photos and videos of watches

The watch is assembled in a case made from non-working “electronics”.


This circuit was published back in 2008, on the website http://radiokot.ru/circuit/digital/home/33/, and despite obvious circuit errors, the circuit works, many have repeated it during this time, on the same site There is a large forum topic where this device is discussed. In addition to the author's firmware, there are also sets of firmware on the forum. Including for circuit options when using various indicators, both with a common cathode and with a common anode. Archive with tested firmware and their source codes at the end of the page.

I remembered this circuit when I needed a simple "primary clock". The circuit was repeated exactly, with the only change - instead of the indicator, a connector was installed to which the power part of the “big clock” would be connected. In order to make sure that the watch is working, the first indicator that comes to hand is connected: XYLNH 420401B-0 - I have no idea what this inscription on the case means, but the pinout indicator completely coincides with the CC56-12SRWA indicator recommended by the author, only blue glow.

Operating the clock is very simple: Use the S2 button to set the minutes, and the S3 button to set the hours. Reset button (S1), allows you to set the time to midnight, sometimes useful, although you don't have to set it. The dot separating hours and minutes blinks at a frequency of 1.25 Hz, that is, 0.4 seconds on, 0.4 seconds. No.

In general, the scheme works. But, if you plan to use it as it is, then I would still advise increasing the resistance of resistors R4-R11 to at least 330 Ohms, and placing transistors in the cathode control circuit. The ATtini2313 processor is certainly tenacious, but I don’t think it should be tormented in this mode.

And a few words about MK programming. I used the programmer AVR ISP mkII programmer with the AVRDUDE_PROG 3.2 program

This article describes the design of digital hours on the Attmega8 microcontroller, which are equipped with a stopwatch, alarm clock, and countdown timer. The watch has a day and date display function with the ability to display the date and time combined. There is automatic switching between summer and winter time, as well as leap year accounting.

The display is built on six 7-segment LED indicators with brightness adjustment. The watch is also equipped with battery backup.

Description of the microcontroller clock design

As mentioned above, the watch has a six-digit display, consisting of two three-digit T-5631BUY-11 displays, operating in multiplex mode. The indicator anodes are grouped by category and are switched using transistors T1...T6.

The cathodes are grouped into segments and are powered directly from the IO1 Attmega8 microcontroller. The multiplexing frequency is 100Hz.

The clock is controlled by a low-frequency quartz crystal X1 with a frequency of 32768 Hz. By activating the CKOPT bit, which allows the use of internal 36pF capacitors for quartz, there is no need to use external capacitors.

If you have problems starting the generator, you can try connecting 2 22pf capacitors. For even greater clock accuracy, you can completely turn off the internal capacitors (reset the CKOPT bit) and leave only the external ones.

The piezo emitter REP1 emits an alarm sound and signals the end of the timer. During the sound signal, logic 1 appears at pin 16 (port PB2). This signal can be used to control any load.

The watch is controlled by three buttons - minutes, hours and mode. The buttons are connected through resistors that protect the ports of the Attmega8 microcontroller. The circuit is powered by a 5 volt source (7805). Current consumption mainly depends on the number of active indicators, as well as on the degree of brightness adjustment.

At maximum brightness, current consumption reaches 60 mA. The watch is equipped with a backup battery. While running on battery power, the watch enters an economy mode in which the display is turned off. Also in this mode, the buttons are not active, except when it is necessary to turn off the sound signal.

The backup voltage is from 3 to 4.5 V. This can be one 3V battery, three 1.2V NiMH or NiCd batteries, or one Li-Pol or Li-Ion battery (3.6 to 3.7V). The current consumption from a 3V battery is only 5...12mA. The battery life of the watch in economy mode using a 3V CR2032 battery with a standard capacity of 200mAh should theoretically be enough for about 2.5 - 3 years.

The software for the microcontroller is at the end of the article. The configuration bits must be set as follows:

Clock management

The clock is controlled using TL1-minute, hour-TL2 and TL3-mode. The hour and minute buttons are used in clock mode to assign hours and minutes. In other modes they have different functions. The mode button switches between various modes, of which there are 8 in total:

Mode 1 - Clock

In this mode, the display shows the current time in the format “HH.MM.SS”. The clock button is used to set the clock. Minutes button to set the minutes. When pressed, the seconds are reset.

Mode 2 - Enabling daylight saving time and setting the year

Here you can turn on or off the automatic transition between summer and summer. winter time and set the year. The data is in the following format “AC ‘RR” (AC – automatic time, space, last two digits of the year).

Mode 3 - Countdown timer

This mode allows you to organize a countdown from a given value to zero. After this time has elapsed, a beep will sound and LED1 will light up. The beep can be stopped by pressing the Mode button. The data is in the following format: "HH.MM.SS". Maximum possible meaning is 99.59.59 (almost 100 hours).

Mode 4 – Combined information output

In this mode, the following are displayed alternately:

  1. current time in the format "HH.MM.SS"
  2. date in the format “AA.DD.MM.”

Each format is displayed for 1 second. In this mode, the Hour and Minute buttons are used to adjust the brightness of the display (Hours-, Minutes+). The brightness changes logarithmically in 6 steps: 1/1, 1/2, 1/4, 1/8, 1/16 and 1/32nd. Default is 1/2

Mode 5 - Setting the day of the week and alarm mode

In this mode, you can set the day of the week - from Monday to Sunday (displayed as Mon, Tue, Wed, Thu, Fri, Sat, Sun), turn on the alarm and select its operating mode. The data is in the following format: “AA AL._” (day of the week, space, AL., alarm setting).

The clock button sets the day of the week. The minute button is used to turn on/off the alarm sound and select its operating mode: “AL._” = alarm is not active, “AL.1” = alarm beeps 1 time (then automatically switches to “AL._” position), “ AL.5" = alarm sounds only on weekdays (Mon-Fri, except Sat-Sun), "AL.7" = alarm rings every day

Mode 6 – Setting the day of the week and date

The clock button allows you to set the day of the month. The minute button allows you to set the month.

Mode 7 - Stopwatch

The stopwatch allows you to measure time with an accuracy of 0.1 seconds. The maximum measurement time is 9.59.59.9 (almost 10 hours). The data is in the following format "H.MM.SS.X". The minute button is used to start and stop the stopwatch. The clock button is used to reset.

Mode 8 - Alarm clock

This mode is used to display and set the alarm time (ALARM). The data is in the following format "HH.MM.AL". The Minutes button sets the alarm minute, the Clock button sets the alarm hour.

Below is a diagram of a similar watch that has an indicator with a common cathode

(downloads: 812)

Circuit and program of a very simple clock on an AVR microcontroller using a DS1307 real-time chip

Good afternoon, dear radio amateurs!
Welcome to the website ““

Today, dear radio amateurs, we bring to your attention a very simple clock circuit on the AVR microcontroller And real time clock with serial interface I2C DS1307.

The design is assembled on an ATyni26 microcontroller (this particular MK was just at hand). But you can use any other MK, the main thing is that it has 13 free inputs - 11 for displaying the current time on a four-digit seven-segment LED indicator and 2 outputs for the time setting and correction buttons.

Clock diagram:

The following details are used in the diagram:
- Microcontroller – ATyni26 in DID package
– Real time clock – DS1307 in DIP package
– Quartz – 32.768 kHz, with an input capacitance of 12 pF (can be taken from motherboard computer), the accuracy of the clock depends on this quartz
– backup power supply DS1307 – 3 volt lithium cell CR2032
– 4-digit seven-segment LED indicator – FYQ-5641UB-21 with a common cathode (ultra-bright, blue light)
– all transistors are NPN structures, you can use any (KT3102, KT315 and their foreign analogues), I used BC547S
– microcircuit voltage stabilizer type 7805
– all resistors with a power of 0.25 watts
– polar capacitors for an operating voltage of 50 volts
The current consumption of the device is up to 30 mA.
To power the structure, you can use any unnecessary Charger from your phone or a suitable power supply with an output voltage of 7-9 volts.
The microcontroller communicates with the DS1307 clock via the I2C bus and is organized by software.
The backup battery for the DS1307 clock does not need to be installed, but in this case, if the mains power fails, the current time will have to be set again.
The printed circuit board of the device is not given; the design was assembled in a case from a faulty mechanical watch. The LED (with a blinking frequency of 1 Hz) serves to separate the hours and minutes in the design.

Program operation.
The clock frequency of the microcontroller is 1 MHz (factory setting, FUSE bits do not need to be touched or installed). The program size is 1 kilobyte.
When the program starts:
- launching timer T0 with a preset frequency CK/8 and calling an interrupt on overflow (with this preset frequency, the interrupt is called every 2 milliseconds)
– initialization of ports (ports PA0-6 and PB0-3 are configured for output, PA7 and PB6 for input)
– initialization of the I2C bus (pins PB4 and PB5)
– at the first start, or restart in the absence of backup power to the DS307, bit 7 (CH) of the zero register of the DS1307 is checked and the current time is reset to the initial setting. In this case, button S1 is for setting the time, button S2 is for moving to the next digit. Set time - hours and minutes are written to the DS1307 (seconds are set to zero), and the SQW/OUT pin (7th pin) is configured to generate rectangular pulses with a frequency of 1 Hz
– global interrupt enabled
– the program goes into a loop with polling of key S2
When the T0 timer counter overflows, the program proceeds to servicing the interrupt (every 2 ms):
– the current time is read from DS1307 and written to four SRAM variables (tens of hours, units of hours, tens of minutes, units of minutes)
– the current time output subroutine dynamically displays the current time on the LED indicator
– when you press the S2 button, the program disables the global interruption and goes into the time correction subroutine (the S1 and S2 buttons set tens and units of minutes, then, from 0 seconds, pressing the S2 button records the updated time in DS1307, allows the global interruption and returns to the main program ).

The DS1307 clock used in the circuit allows you to display seconds, minutes, hours, day of the week, date and year.
If you use an LCD display in the circuit instead of LED indicators, for example WH0802 (two-line, with eight characters per line) or similar, then you can organize a full-fledged clock with a full display of the current time, and power the device from galvanic cells or batteries.

ATyni26 microcontroller pinout:

DS1307 pin layout:

Typical connection diagram D1307:

I offer two very simple circuits for self-assembly, namely a clock on a PIC and AVR microcontroller. The basis of one circuit is the AVR Attiny2313 microcontroller, and the other is PIC16F628A

These clock circuits on a microcontroller will greatly help novice radio amateurs understand the issues of operation and programming of microcontrollers.

Let's take a closer look at this simple diagram: Power can be supplied either from three AA batteries or from


The Attiny2313 microcontroller is clocked by 16 MHz quartz. As a time counter, the internal circuit of the microcontroller uses a 16-bit timer with a divider by 256. As soon as the internal counter counts to 625, an interrupt occurs. Therefore, we will have interruptions 100 times per second.

The time interval is taken into account in global variables, and each interrupt requires the millisecond value to be increased by one. As soon as the number of milliseconds reaches 100, you need to increase the value of seconds by one and reset the milliseconds to zero. And so on in accordance with the same algorithm up to tens of hours, which are reset only when the value reaches 24 and without increasing the next digit.

In accordance with this principle, we create the current time value recorded in global variables. Now we need to visually display this data. Since the microcontroller has a limited number of ports, we will use such a feature as the inertia of the sa15-11gwa digital segment indicator. The cathodes of its indicators are connected in parallel, and the anodes have separate control, which allows you to display a number on any of the four indicators at any time.

By quickly switching the microcontroller port to which all the cathodes are connected and quickly switching the anodes, it creates the illusion that all four digits are displayed in the segment indicator, although in fact only one of the segments is working. If the current time is 11:57, then first we display number one on the first clock indicator, after 1 ms we display number 1 on the second indicator, after another 1 ms we display 5 on the 3rd indicator, after 1 ms we display 7 on the 4th indicator and so on cyclically, updating each indicator after 1 ms.

The state of the clock control buttons is polled at the end of each display cycle about 40 times per second.

Download the PCB drawing made in the program and the firmware for the microcontroller from the link above. and directly about the intricacies of the firmware, read here.

This design, although based on a different type of microcontroller, is no less complex than the previous circuit.


The operating algorithm of the firmware is also simple; the archive contains very detailed comments on the program code. Two toggle switches kn1 and kn2 are designed to correct the time - hours and minutes. The accuracy of the watch depends on the frequency of the quartz used.

Structurally, the clock is made on two printed circuit boards located side by side at an angle of 90 degrees. The indicator is located on the first board, and all the other electronics are on the second. Backup power is provided by three batteries placed in a holder made from an old Chinese lighter with an LED. For mains power alternating current Any 5V and 150mA current will do.


The archive, which you can download from the green link, contains the layout of both printed circuit boards in the Sprint Layout program and the firmware for the PIC microcontroller with the source code of the program for MP_LAB IDE, with detailed comments.

And the program itself can also be found here

This design has digital correction of the accuracy of the stroke, as well as a built-in thermometer, which alternately displays temperature readings on the LED display with the exact time. The clock design uses a non-volatile microcontroller memory that saves settings and settings even when the external power is lost.


To control the anodes of LED indicators, transistor switches are used standard scheme inclusions.

When you turn it on for the first time, an advertising screen appears on the display for one second. Then the time is displayed. Pressing the SET_TIME button switches the indicator in a circle from the main clock mode:

Absolutely, in all cells, holding down the PLUS/MINUS buttons performs accelerated installation. If the settings were changed by the user, then after 10 seconds the new values ​​will be stored in the non-volatile memory of the microcontroller and will be read. When flashing the MK firmware, set it as follows:


You can evaluate the external design of the device from the photographs below; the stitching and additional files for the design can be downloaded from the link just above.


Shift schedules are implemented in the alarm clock firmware: 4/5 (four on the fifth) – 4 days in 1 shift, 1 day off, 4 days in 2 shifts, 1 day off, 4 days at night, 1 day off; – day, night, 2 days off; On weekdays – Mon-Fri - working days, Sat-Sun - Closed; ( Holidays are not taken into account); Daily.

The user himself selects the type of alarm schedule and sets any alarm time. In the 4/5 and day, night, 2 weekend options, you must additionally select the current shift.

In addition, the following functions are implemented in the MK firmware: Transition to the summer-winter period; Time adjustment; Accelerating alarm signal; Displaying zero in the hour and date digits

The clock circuit is based on the DS1307 clock chip and the MEGA8 microcontroller. The circuit (placed in the archive with MK firmware and printed circuit board drawings) is designed for the use of seven-segment digital indicators with a common anode for a voltage of 5V. (ATTENTION! For simplicity, ballast resistors are not shown in the diagram. They need to be installed on each segment of indicators. There are 112 pieces in total. The nominal value is calculated according to the documentation. I used segment indicators like fys15011 and fyd-5622. If you use more powerful ones, then most likely without Additional transistor switches are not needed.

The printed circuit board drawing was developed for an existing box from an old broken watch. You can connect a low-power load to the Alarm connector, say a musical card, and use jumper JP1 to disconnect the internal beepper. The microcontroller can be flashed directly on the board, which greatly simplifies configuration in case of modification of the design.

Setting the clock
To do this, you need to enter the parameter setting mode:
Parameter-Value-Save in memory
P.01 - CLOCK [-]
P.02 - MINUTES [-]
P.03 - DAY [-]
P.04 - MONTH [-]
P.05 - YEAR [-]
R.06 - Alarm type [+] (1-4/5; 2-5/8; 3-railway schedule; 4-daily)
P.07 - CHANGE [+]
P.08 - Bud.1.HH [+]
P.09 - Bud.1.MM [+]
P.10 - Bud.2.CHH [+]
P.11 - Bud.2.MM [+]
P.12 - Bud.3.CHH [+]
P.13 - Bud.3.MM [+]
P.14 - Adjustment (D.H) [+]
P.15 - Summer/winter period [+]
P.16 - Accelerating beeper [+]
P.17 - Display leading zero in hour digit [+]
P.18 - Display leading zero in date digit [+]

Alarm Setting: Button On/On Alarm. - On/On is carried out, in this case: With alarm type 1: Alarm 1 - 1st shift; Bud.2 - shift; Bud.3 - 3rd shift;
Shift schedule: 1,2,3,4 - first shift; 5 - day off; 6,7,8,9 - second shift; 10 - day off; 11,12,13,14 - 3rd shift; 15.16 - day off; Then the days repeat.
With the first type of alarm 2: Alarm 1 - sets the alarm time; Bud.2, Bud.3 - does not work; Shift schedule: Weekdays.
With the third type of alarm: Alarm 1 - the time of day is set; Bud.2 - sets the time at night; Bud.3 - does not work;
Shift schedule: – day, night, 2 days off;. When the alarm type is 4 Alarm 1, Alarm 2, Alarm 3, the time is set; If you plan to use only one alarm clock, set the times of three to the same time.
With shift schedule: Daily. If you press the Alarm Off buttons. in parameter setting mode, the settings will be exited without saving.
Adjustment: When making adjustments, the following method is used: +/- Ch.D, where: Ch is the number of seconds adjusted per hour (max 9). D - seconds adjusted per day. ATTENTION! No adjustments are made when the power is turned off. When you turn it on, check that the time is correct.