Path: Home => AVR-EN => Applications => clock60-4   Diese Seite in Deutsch: Flag DE Logo
clock60-4 logo

Clock with 60+4 LEDs with an ATmega48

60 red 3-mm LEDs mounted in a circle with a diameter of 97 mm, associated with additional four LEDs in the corners that show the status of the clock. All diodes are driven by CMOS shift registers and do not need resistors. The complete clock is in an ATmega48, that does all the processing.

Be careful! This works only with real CMOS-4094, not with 74HC4094 or with 74HCT4094!

Preliminary! Untested and incomplete!
The time is displayed as follows:

Adjusting the time and alarm time goes with two keys, one switch and a potentiometer:
  1. the switch turns the alarm on and off,
  2. the first key enters and leaves the adjust mode,
  3. if the alarm is off, the time (hours, minutes and seconds) are adjusted, if it is on, the alarm time is adjusted (hours, miinutes and melody) are adjustred,
  4. the second key functions as follows:
  5. all numbers are selected with a potentiometer, for a fast and reliable selection.
All drawings of this project can be downloaded as LibreOffice draw file here (including the PCB layout and the component placement drawing), all flow diagrams are as LibreOffice draw file here, all calculation tables are as LibreOffice Calc file here.

Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

2 Hardware

4094 shift register To drive 60 single LEDs plus the four status LEDs, no DIL-packaged AVR controller can be used (too few portpins). The hardware here extends one 8-bit-port to 64 bits by serially transferring the 64 bits into eight 8-bit CMOS shift registers.

The 4094 has eight shift registers S1 to S8. On positive edges of the clock input CLK shift register S1 transfers the logical state on the DATA input pin to its shift register output S1. Simultanously the outputs of all shift registers Sn are moved to the following shift registers S(n+1). After eight CLK shifts, the first data bit has reached S8.

Setting the strobe input pin STB high, moves the state of all shift registers to eight latches L1 to L8. As long as STB is high this latching goes on. When STB goes low, the previous state of the latch is kept.

Setting the output enable pin OE high, enables the output drivers of the 4094: the state of the eight latches is reflected on the output pins Q1 to Q8. As only the STB input pin high changes the latches, the output enable pin can be kept high when transfering the next eight data bits to the shift registers.

Note that the 4094 allows for cascading with its two extra output pins QS and Q'S. As cascading is not used here, their function is not described in detail.

Note that both the data sheets of Fairchild as well as that from Texas Instruments have serious errors. Both state that the Output Enable pin has to be high during data shifting. This is false, this pin exclusively drives the output buffers and has no influence on shifting and latching, as their correct functional diagram shows. The strobe pin STB is not edge-triggered, as Fairchild states, but static.

The use of the CMOS device has an additional advantage, too: The shift registers directly drive the LEDs with a limited current, so no 64 extra current-limiting resistors are necessary. But this has a disadvantage, too: the output currents for a LED in sink or source mode on 4094 output pins at 3.3 or 5V operating voltage are small. To drive higher LED currents, the CMOS has to be operated at higher voltages. Of course, those elevated voltages above +5V cannot be delivered by AVR port pins, so a one-way AVR-to-CMOS converter has to be attached that brings the AVR's data and control pins to the higher voltage level of the 4094s.

As there are many variations possible, the hardware comes in several different flavours:
  1. The most simple version is to operate the controller, the shift registers and all the LEDs with between 3.3 and 5.0 Volt. The LED current then is between 1.1 and 3.1 mA, suitable to drive 2-mA-LEDs.
  2. To drive the LEDs with elevated currents of between 5 and 20 mA, a higher operating voltage for the shift registers, which drive the LEDs, is required. This requires to shift the low operating voltage of the controller output signals to the elevated operating voltage of the shift registers. That is done with two standard Darlington arrays.
  3. The LEDs can be driven by the shift register output pins in common anode mode, where the CMOS output sinks current from the LED cathode. The shift registers can as well drive the LEDs in common cathode mode, where the CMOS output sources current into the anodes of the LEDs.
The following describes all different flavours.

2.1 Low-current LED hardware

Schematic of the low current clock60-4 This is the low current version. All is supplied by one operating voltage, which can be between 3.3 and 5.0 Volts.

An enlarged version of this schematic is available for download, if you click on this schematic here for displaying it in the browser or right-click on it and select Save-file-as for downloading it. The enlarged version also includes a possible supply schematic, which has an adjustable output voltage of between 3.3 and 5V.

The ATmega48 is clocked with a crystal. Nearly any crystal frequency is suitable and can be adapted by configuring the constant "clock" in the software.

The following hardware is attached to control the clock: Each bit of the eight shift registers drives one LED. The eight's bit of shift registers 7 and 1 drives the two AM and PM LEDs, the eight's bit of shift register 5 blinks a yellow LED if adjustment mode is active and the eight's bit of shift register 3 blinks a red LED if the alarm function is active.

The output port pins PD0 to PD7 (data bits) and PC3 to PC5 (control bits) drive the 4094s directly. Note that no inversion takes place in this version. The three control pins on PC3 to PC5 have positive signals, while the data on port D has to output inverted data due to the LEDs in sink mode. Two adjustable constants in the source code control the right signals.

The pulses on the control pins PC3 (CLK) and PC4 (STB) are roughly 1µs long to give the 4094s enough time to recognize. This time is ensured even at clock frequencies of up to 20MHz.

The functional diagram of shifting the bits to the shift registers This is how the controller ATmega48 handles the serial shifting. In eight registers the 64 LEDs are either set one (if the LEDs are in common anode mode) or to zero (if the LEDs are in Common cathode mode). LEDs that should be one are reversed by the software (see the software section below.

The 64 bits in the eight registers are then copied to the port D. Packages of eight bits are packed and transferred in parallel to the eight 4094s. As LED L1 is not necessarily attached to Q1 of the 4094-1, this transfer can be done in several modes. Each package of eight bits is clocked into the shift registers of the 4094s. After all 64 bits have been shifted, a pulse on the strobe input pin transfers the shift register content to the latches in the 4094s. After having done this, the output enable pin can be activated (set high), to display the bit state.

LED currents versus operating voltage of the 4094 Note that in that case the LED current is solely controlled by the operating voltage. The diagram shows how large the output current through an attached LED is at different operating voltages. Two cases are shown: sinking current with the anode of the LED on the positive operating voltage and sourcing current with the cathode of the LED on the negative operating voltage (GND).

As can be seen, the LED current in sink mode is slightly higher than in source mode. This contrasts to measurments I made with a 4011 earlier, where the source mode delivered slightly higher currents. But the differences are small (1.74 vs. 1.54 mA/V) and are not decisive.

Both curves are quite linear above 5 V, only at lower operating the voltages are a little bit higher. So you can use the formulas given here. For more accurate results in the low-voltage range see the original data in the LibreOffice calculation file (sheet "CMOS-drive-current_4094").

Voltages versus desired LED current LED currents at common voltages The table shows how large the voltage of the CMOS has to be to get a desired current through the LEDs, the other table provides currents at desired voltages. Only currents below 5 mA are suitable for the low-current version. All higher currents are for the high-current version (see schematic below).

Sourcing the LEDs from the CMOS shift register output pin For 2-mA-LEDs an operating voltage of 4.24 Volts is at least necessary, if the LED current is sinked by the outputs of the shift registers (as is shown in the above schematic). If the LEDs are reversed and source current from the output pins of the 4094s - which is shown in the left schematic, along with a current-limiting N-JFET -, the numbers to the left in the table apply. As can be seen, the voltages in source mode are a little bit higher than in sink mode.

If you want to adjust the current, use a 3.3V regulator and adjust its Ground pin with a trim resistor. In that case, make sure that the resulting operating voltage does not exceed the 5.25V limit of the controller. The top left part of the larger schematic shows such a solution that allows to control the LED currents via the operating voltage.

2.2 High-current LED hardware

Schematic of the elevated current clock60-4 This version allows elevated LED currents above 5 mA, by applying higher voltages on the shift registers that drive the LEDs.

The differences then are the two Darlington arrays ULN2001. Those translate the low-voltage data- and control-signals of the controller to the elevated voltages of the shift registers. As the Darlington arrays invert the data- and control-bits, the software has to do the correct inversion on the data and control output pins of the ATmega48. Just set the constant cUln2001 to one, re-assemble and burn the flash.

To protect the LEDs and the shift registers from over-currents optional P-JFETs limit the total current that can be drawn. Depending from the J176's current at zero-Gate voltage and the desired upper case of the LED current. Use one, two or three of those in parallel. If the three J176, that drive 30mA, are not enough: use the alternative PNP current regulator instead.

Just right-click on the schematic to download a complete schematic, with all LEDs and 4094s.

LED currents versus desired operating voltages The resulting currents at the different operating voltages of the 4094 are estimated in this table. Note that numbers in red exceed the maximum power dissipation limits of the 4094, so do not apply these voltages over longer times (e.g. for longer than one second). Or consult this chapter for a transformer-orientated calculation.

Maximum power of the 4094s with all LEDs on If at higher voltages the following question turns up: what would be if all LEDs are active (e.g. when applying the operating voltage and either turning all LEDs on or having the shift registers all in an active state. In that case eight LEDs are driven by each 4094 on its eight output pins.

The table shows at which operating voltage the thermal limit of the 4094 is exceeded. At voltages of 10V and higher the thermal limit is reached, if eight LEDs are driven. It is a good idea to apply a PNP current limiter for that case, too, and to adjust it to 8 * 11.6 mA = 93 mA. That is clearly enough for five LEDs with roughly 20 mA each, but protects the 4094s from thermal death cases.

All other components are the same as in the low-current version.


Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

2.3 Clocking the ATmega48

The device can be clocked in two different ways:
  1. with the internal RC, with an accuracy of roughly 3% at an operating voltage of 3.3 Volt, which means an accurracy of +/- 43 minutes per year and requires a monthly time adjustment,
  2. with an external crystal: if so equipped and if the internal clock fuse has been set, the crystal determines the accuracy. The time adjustment is only necessary once per year (or if the time zone changes).
Crystals for the device The table to the right shows, for all crystals on the market and for the 1 MHz internal RC oscillator, what has to be done if
  1. an 8-bit Timer/Counter, or
  2. a 16-bit Timer/Counter
is used for clocking. Displayed are the prescaler values, the CTC-divider to be applied and, if necessary, a down-counter value that has to divide the result further down to get the 1 Hz clock signal. In case of the 8-bit-TC either no or up to three bytes of the downcounter are necessary, the 16-bit-TC mostly needs no additional downcounter and at maximum one 8-bit software counter.

As can be seen nearly all available crystals are suitable for that purpose, except for four unusual crystal frequencies.

Please note that

Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

2.3 Power supply considerations

Transformers and their voltages under load As the number of potential variations is very large, no specific transformer can be recommended to supply the clock. Here are some considerations.

In a low-current environment, and if we feed the 4094s with a maximum operating voltage of +5V by using 2-mA-LEDs, the maximum LED current is around 8 mA, with some mA current for the ATmega48 and all the other stuff, we'll need at maximum 15 mA at 5V. So the transformer of choice would be a 0.33 or 0.35 VA type with 6 V. As we see from the dark-blue and the magenta curve, these provide enough power. If you plan to use up to 5V, use four Schottky diodes (single coil transformers) or two Schottky diodes (double coil transformer), so the 6V transformer provides enough extra voltage for the low-drop voltage regulator.

In the diagram, dotted lines indicate a double-coiled transformer (e.g. 2 * 6 V) with a two-diodes-rectifier, the ones for 0.33 and 0.35 VA are single-coiled transformers. As the voltage at small current consumption is far above the 5 V, we'll need a regulator. As the 6V transformer provides voltages of 7.5 V or higher, when the current is not too large, this is not necessarily a low-voltage-drop type regulator.

The 6V transformers all provide up to 12 V and, depending from the current consumption, voltages drop down to 9 V at 40 mA (e.g. four LEDs with 10 mA). These transformers with one or two 6V coils fit perfectly with the 10 mA-LED. Of course, the ULN2001 is necessary for translation of the controller's data and control signals at its low operating voltage. No current limiting is necessary then, because a 6V transformer and the 4094s cannot deliver too much LED current (the specified max. current of a 2mA-LED is above 20mA).

If you need more LED current, such as 20 mA for 10mm-LEDs, you'll need a (2*) 7.5 or 9V transformer. These deliver up to 18V for the 4094s, and do not drop too low when consuming up to 100 mA. Make sure that the transformer's power is large enough and has an extra reserve, so that the voltage does not drop too low at higher current consumption. A 1VA type with only one coil already drops too low, so that the LED currents drop too low, if more than two LEDs are on. The 1.8VA version with 2 coils is better suited for that.

LED currents with different transformers 4094 thermal power with different transformers These two diagrams show the effect of voltage drops for a number of different transformers, if a different number of LEDs (between 2 and 8) is switched on. The left diagram shows the current per LED, the right diagram the resulting thermal power that the 4094 has to dissipate.

The more LEDs are on, the lower is their current. Transformers with a double coil have a slightly higher current. Even the 2x9V in 1.8VA (or higher) do not exceed the current limit for LEDs, so you can forget the J176 current limiter (if not for other purposes). Even eight LEDs can be on with all transformers that were considered here.

The thermal power of the 4094s only slightly exceeds the specified 700mW limit if a 9V transformer is used and five or more LEDs are on. If you can be sure that the eight-LEDs-on-scenario will never happen (or at maximum only over a few milli-seconds), you can't kill a 4094 with those transformers.

Do not use transformers with 12V or higher, their peak voltage at low currents is far above the 18V specified as maximum for the 4094s.

The 4094 and LED supply do not need a voltage regulator, because currents change only once per second, and all LEDs are all switched active after the data transfer has been totally completed: when the STROBE is switched on. So voltage changes due to changing numbers of active LEDs can not intercept with the data transfer. As the strobe input is not edge-triggered, voltage changes following strobe activation is irrelevant: the content of the 4094 latches do not change.

And: you do not see a difference of driving a LED with 15 or 20mA, the difference in visual intensity is small enough so you do not need a regulator - unless you have only 12V transformers in your eletronic spare part box - or if you measure the LED intensity with a sensitive measuring device - such as a digital camera - instead of your own dumb eyes.

So, using the raw rectified transformer voltage for driving the 4094s and the LEDs has the advantage that elevated currents reduce voltages and reduce the heat load of the 4094s.

Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

3 Mounting

For this setting a single-sided PCB has been designed. Note that many connections have to be added by enameled copper wire on the soldering side, namely the data and control pns of the eight 4094s and the two capacitors of the crystal (if used).

Chapter 3.3 provides some hints for alternatives.

3.1 PCB layouts

This is a PCB layout. Click with the right mouse button on it and select "Save target as ..." to download an enlarged version of this gif file.

PCB of the clock Note that the eight DATA lines and the three control signals (STB, CLK and OE) are not connected. Those have to be added with solderable enameled copper wire on the soldering side of the PCB. Those pin connections are enlarged and named.

Note also that the two ceramic caps also need to be soldered on the soldering side, because there was not enough space for those near the crystal.

The 10-pin connector connects all external components that are not on the PCB. It has no drilled holes but is soldered directly on the soldering side. The 10-pin cable would otherwise cover some of the LEDs. Note that the two resistors and capacitors, that reduce toggling of the two keys, are external, too. Both are directly soldered to the keys.

External power supply can also be fed through the 10-pin connector. If you want to use a single power supply, just leave the 3.3V regulator and its two capacitors off and connect both the 9V- and the 3.3V-line to the external power supply (max. 5V).

3.2 PCB component placement

Component placement on the PCB This is the placement of the components on the PCB. Note that some of the components are either unnecessary, depending from the variation to be selected.

Two bridges (black lines) have to be soldered, as well the two ceramic caps for the crystal (on the soldering side, blue lines).

3.3 Variations

3.3.1 Low current version

The low-current-version uses one single power supply for the LEDs and for the controller. If that shall be made, the following hints apply:

3.3.2 Source current version

If you want to supply the LEDs from the 4094s in Source mode, do the following:
Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

3.3.3 Use of a different crystal

The reserved place on the PCB for the crystal is rather small. After soldering the second ULN2001 and its socket as well as the 100nF cap on the ATmega48's VCC and GND pins, mount the crystal with its full length connectors by bending the one pin for the XTAL1 connection towards the respective borehole (leave the one for XTAL2 in its straight form). If necessary prolong the crystal pin for XTAL2 before soldering the crystal. Make sure that the crystal casing does not touch ULN2001's or ATmega48's pins (use a piece of a plastic band or shrink sleeving of the crystal).

3.3.4 Elevated LED currents limiting

The schematic of the Elevated Current version above shows three P-JFETs that limit the current of the LEDs. If you do not need current limiting, you can simply bridge those JFETs. If you want to use those: here is the data for it.

Current limiting by J176 These are the maximum currents that one, two or three J176 allow at operating voltages between 3.3 and 18 Volts allow (assumed that the 4094 outputs deliver that current). The P-JFETs are connected parallel, so the more JFETs the higher the allowed current. If the CMOS output does not drive enough current (e. g. at 3.3 or 5 V), the limitation are the CMOS, not the J176s. If you want to limit the current per LED to 2 mA, which can be recommended for battery operation, and if you use the respective LED type for that, just limit the current to 8 mA. At operating voltages above 9V just apply one J176. If you apply three J176s, your current per LED is roughly 7 mA (if your operating voltage is larger than 15V).

If the three P-JFETS do not allow enough LED current (and if the 4094 can drive more current than necessary for the four LEDs that are on at maximum), you can use the transistor driver that is displayed as alternative in the Elevated-Current-schematic. The current limit here is I = 650 (mV) / 15 (Ω) = 43 mA or for 10 mA each if four LEDs are on. If only two LEDs are currently on (5*Hours = Minutes = Seconds plus one of the AM/PM LEDs), the 20mA should not damage the LEDs. Use 60mA if you want to go to the limit of low-power LEDs (650 / 60 = 10Ω). Even higher currents are possible with smaller resistors, but those are on your own risk. And the 4094s might not deliver that, even at 18 Volt operating voltage. Or might reach their power limit of 700 mW (at 18V those are roughly 40mA).

Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

3.4 Parts list

Parts list for the clock60-4 Here is the list with all necessary parts. The list is sub-divided for all variations.

Part names are for Reichelt, prices are of 03.01.2022. The cost share of all components that are greater than 1% of the total are given. As can been the LEDs, the 4094s and the transformer dominate costs.

The total price is around 30 Euros, so the project is in a range to serve as a birthday or Christmas gift.


Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

4 Operation

Flow of the key adjustment Operation involves the two keys K1 and K2 attached to PC2 (PCINT10) and PC1 (PCINT9). Both have a hardware debouncing mechanism, so software debouncing is not necessary.

The adjustment uses the following rules:
  1. In normal mode pressing key K1 starts the adjustment.
  2. If the switch S1 (alarm) is off, the time is adjusted. If it is closed, the alarm time is adjusted.
  3. Adjustment starts with the hours, then the minutes. If in time adjust mode, the seconds adjustment is last, if in alarm time mode the melody selection is the last stage.
  4. The key K1 steps one stage back. If in hour adjustment mode, adjustment is skipped and time resp. alarm time are kept as they are.
  5. The key K2 steps one stage forward. If in the seconds or melody adjustment stage, the complete set of adjusted parameters is written and adjustment mode is left.
Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

4.1 Time adjustment

Pressing key K1 while the switch S1 (Alarm) is open starts the time adjustment. The yellow LED L40 starts blinking to signal adjustment mode.

First the hours are adjusted. The current hour display is on and the adjusted hour blinks. Note that during the adjustment period the normal clock operation goes on, so that the fixed hour might change in between.

To adjust the hours rotate the potentiometer. The AM and PM LEDs allow to adjust the 24 hours. If the blinking small red LED disappears, it is exactly adjusted to the current hours (or 12 hours before or after). If you press the key K1 again, the adjustment mode is switched off and all adjustments are lost. Pressing the key K2 instead Pressing key K1 falls back to the hour adjustment, key K2 stores the adjusted minutes and proceeds to the seconds adjustment.

If the seconds adjustment is concluded with the key K2 the complete stored time overwrites the current time and leaves the adjustment mode.

4.2 Alarm time adjustment

The same algorithm applies if the alarm switch is on during the intial key K1 start. Then the alarm time's hours and the alarm time's minutes can be adjusted. Instead of the seconds adjustment the melody by which the alarm wakes you up is played and can be adjusted with the potentiometer. Only after the complete melody has been played the newly adjusted melody starts. Therefore it might take some time to adjust the melody, depending from the duration of your melodies.

Confirming all three parameters with key K2 finally sets the adjusted alarm time, ends the adjustment period and enters alarm mode.

If the alarm time is reached, the selected melody is played. By switching S1 off the alarm is immediately ended. If S1 remains activated, the melody is restarted in each minute. After 10 restarts, the alarm is switched off.

4.3 Snooze function

If adjustment mode is inactive while the alarm switch is active then pressing key K2 advances the alarm time temporarily by five minutes. If the alarm time is reached and the alarm is switched off, the previously adjusted alarm time is restored.

If you want to know the currently active alarm time, just enter key 1 once. The hour is visible. If you enter key 2 once the minute gets visible. Then enter key 1 once or twice to leave the adjustment mode without changing.

5 Software

5.1 Download of the assembler sources

$$TBD$$

5.2 Fuse settings

Before or after flashing you'll have to set the orginal fuses (picture left) to the fuses on the right to activate the crystal.

Original fuses of the ATmega48A Fuses of the ATmega48A for low frequency crystal If your programming device cannot clock the ISP with 8kHz or lower, you can't use a 32kHz crystal.


Page top Hardware Mounting Operation Software
Low current High current Clocking PCB Parts lists Assembler source How it works

5.3 How it works: General

All procedures are performed inside the diverse interrupt service routines. If not activated by an activated interrupt the CPU is put to sleep, to minimize power consumption. The sleep share is roughly 95%, leaving enough time even at slow 32.768 kHz clock.

The whole timing depends from the selected clock rate. In the following it is assumed that a 32.768 kHz crystal is used to clock the ATmega48, but other clock rates are mentioned, if relevant.

5.4 How it works: Time display

The time display in normal mode uses 62 LEDs: 60 for seconds/minutes/hours plus two for AM and PM. As all are driven and controlled by 4094s, their bits determine which of the 62 LEDs are on and which are off.

It would be possible to not build up the playground bits once per second, but to leave the playground static and to use it as a seconds counter. But this would have had immense consequences: