Build a Clock Gizmo
|
Build a high tech microcontroller based clock, learn a unique method for multiplexing LEDs and have fun with this eye catching conversation piece.
Based on a PIC16F84, this project displays time on 27 LEDs, drives a speaker to provide tick-tock sound, and hour chime. How do you do all this with just an 18 pin microcontroller? Read on!
|
|
It seems as though there are as many types of clocks as there are people who care what time it is. The Clock Gizmo is one more time-keeping device, but it has some interesting properties.
Some of the design goals of the Clock Gizmo included:
- Provide a unique way of displaying time.
- Use as few components as possible.
- Provide as much functionality as possible.
- Be battery powered, and operate for as long as possible.
Unique way of displaying time.
The first design goal was achieved by displaying time using 27 LEDs. The LEDs are arranged in columns that show hours, tens of minutes, minutes and an AM/PM indicator.
The typical way to drive an LED is for an output to source or sink current to drive the LED, usually through a current limiting resistor. This works well for small numbers of LEDs, but when the count goes up, using individual output pins becomes difficult. For example if we need to drive 27 LEDs, we need 27 output pins.
When the LED count is greater than a few, typically another technique is used, time-multiplexing. Using this scheme LEDs are arranged in a matrix. For example if we need to drive 27 LEDs we could arrange the LEDs to form a 5x6 matrix. The LEDs are driven by scanning either a column at a time or a row at a time. If the scanning occurs quickly enough, the eye does not see the scanning. This techniques reduces the number of output pins to 11, 5 rows by 6 columns.
Time multiplexing has an advantage over direct drive in that typically less power is consumed, because not all of the LEDs can be on at once, only a row or column. This is important, particularly when one of the design goals is to operate from batteries.
Another multiplexing scheme, not as commonly used, can further reduce the outputs needed to drive 27 LEDs to just 6!
The Clock Gizmo uses a variation of time multiplexing, but exploits the fact that LEDs are after all, diodes. To understand this try a simple experiment, connect an ordinary LED to a battery, and a current limiting resistor. Connect the Cathode to negative and the Anode to positive. Note that the LEDs lights up. Now reverse the polarity of the LED. Notice that it does not light up. If you connect a second LED with opposite polarity, you can choose which LED lights up based on the polarity.
This is the basic technique used in the project. You can imagine how connecting LEDs in this manner could reduce the outputs needed to drive 27 LEDs from 27 down to 14, but how do we get down to the claimed 6 outputs?
To do this we have to do some tricking things. Referring to the schematic, (figure 1) you notice that pairs of LEDs are connected in series across the output pins. If you set one of the output pins to a logic high, and another to logic low, at least one of the LEDs should be able to light, yet there are multiple paths for current to low, which would seem to allow other LEDs to light as well. They do not however because there is not enough voltage drop across the LEDs that are in series to allow them to light up.
To make all of this work we need one more ingredient. We need the ability to “turn off” the output pins for LEDs we do not wish to drive. Setting the output to a logic high or low is not good enough, either of these state might allow some un-intended LED to light. We need to “tri-state” the output pin. In this condition, the output pin does not sink or source current.
To make all of this work, we need output drivers that can source and sink enough current to light an LED, and we need to be able to tri-state the outputs.
This multiplexing technique does have a draw-back, it is difficult to light more than one LED at a time. For the Clock Gizmo this turns out to be an advantage. By having only one LED on at a time, power consumption is lowered. Since the Clock Gizmo needs at most 4 LEDs to appear on at the same time, time multiplexing is possible without flicker.
Fewest components
There are any number of ways that this multiplexing scheme could be implemented. Since one of the design goals was to use the fewest possible components, the Clock Gizmo project uses a MicroChip PIC16F84.
Referring to figure 1, the UFO circuit employs a MicroChip PIC 16F84 microcontroller (IC1), operating from a 32 Khz clock. This device has only 18 pins, two of which are power and ground. Obviously directly driving 27 LED discreetly was not going to happen, even the matrix approach would be a stretch. This device has 13 very flexible I/O pins, most of them have several purposes, plus they can be high/low/tristate.
Functionality
|
The software that is the heart of the Clock Gizmo project. The software must set the I/O pins correctly to enable the proper LED. In addition the software performs several other interesting activities to make the result more interesting.
|
Click on image for a larger view
|
Keeping Time
The Clock Gizmo uses a 32,768 Khz crystal as a time-base. This was chosen for two reasons:
It is easy to divide this frequency down to the 60 Hz for keeping time.
It also allows the microcontroller to consume very little power.
The software configures an internal timer to provide an interrupt every second. This is used to keep track of time, and to drive the speaker with a tick-tock sound. The speaker is also used to chime the hour when the hour rolls over.
Switch Debounce
To set the time the Clock Gizmo has a couple of switches. These switches are scanned periodically to see if their state has changed. Unfortunately, Mechanical switches like those used here, do not open and close as cleanly as we would like. They tend to "bounce" or open and close before settling into a state. A bounce might last from 5 to 30 mS! The process of scanning might detect these “bounces” and treat them as multiple switch closures, not what we want.
To avoid this situation the Clock Gizmo uses a technique called "debouncing". This amounts to scanning for a switch state change, then waiting for a period and testing to see if the switch has changed. If it has, it is assumed to be bouncing, so we start over. If the switch state has not changed, we assume that the switch is stable.
If this process were done independently for each switch, even with only two switches things get complicated, fortunately there is a way that we can debounce all of the switches as once, using some Boolean arithmetic:
- Read the switches, store them in memory as current.
- Test the current state against the previous state by an exclusive-or of the current and previous states, the result is what changes, save this in memory as Delta.
- The exclusive-or indicates changes, it does not tell if the change was an open or closure of the switch. To determine that the switch is closed we perform a logical and of the Delta and the Previous state. This tells us that a switch that was closed has been debounced and has been released, a qualified switch closure.
- Finally we save the current state in memory as Previous.
The process is repeated every periodically.
The switches provide the mechanism for setting time. The switches operate as shown below:
| Switch functions |
| SW1Press | SW2 Function |
| 1 | Adjust Hours - Press SW2 until the desired hour LED is on. |
| 2 | Adjust Tens of Minutes - Press SW2 until the desired Tens of minutes LED is on. |
| 3 | Adjust Minutes - Press SW2 until the desired of Minutes LED is on. |
| 4 | Adjust AM/PM - Use SW2 to toggle the AM/PM indicator. |
| 5 | Toggle Sounds - Use SW2 to toggle the tick-tock and chime sounds. A tick indicates that sound is enabled. |
| 6 | Normal operation - In normal mode SW2 has no affect. |
Construction and Checkout
The UFO is simple enough that it can be constructed on a piece of perfboard using standard construction techniques, keep in mind that the technique used here to interconnect LEDs, can be a bit complicated to layout.
When assembling the board, double-check the polarity of IC1, it is best to use a socket for this component. Also the LED polarity is very important. A reversed LED will not cause a complete failure, but will cause multiple LEDs to light, and can be difficult to troubleshoot.
Before installing IC1, it must be programmed. Get the Firmware
After you have completed assembly, it's a good idea to double check for bad or missed solder joints, and reversed polarity of components. If all is well, it's time to “let the smoke out”.
Now that you have completed construction, you are ready to see it work. You will need a source of 4.5 volts, a set of 3 ordinary 1.5 volt AAA batteries connected in series will do nicely.
With power applied, the 1 hour LED should light, this indicates that the circuit is receiving power. If for some reason the LED is not on, remove power and proceed to the troubleshooting section.
Troubleshooting
The most likely problem is some component is reversed. Remove power and carefully check for a polarized component that is not correct.
It's also a good idea to check all of you solder joints for joints that are not good, or pins that might have been missed. It's easy to do.
Another common problem, is reversing an LED. The typical failure, is that multiple LEDs light when only one should. This can be difficult to troubleshoot, the best thing to do is to check the polarity of each LED.
Operation
There is not much to using the Clock Gizmo, it is intended to operate for many hours without changing batteries. It is intended to sit on a desk as a conversation piece.
Further experiments
What other interesting things can you do with the technique? You are not limited to 27 LEDs, the technique can be extended to larger arrays of LEDs, can you figure out how to do it?
PARTS LIST FOR THE UFO
| SEMICONDUCTORS |
| IC1 | PIC16F84 MicroChips microcontroller |
| LED1-LED27 | Red Light Emitting Diode |
| RESISTORS |
| R1-R6 | 50-ohm 1/8-watt |
| R7-R8 | 20k-ohm 1/8-watt |
| CAPACITORS |
| C1,C2 | 4.7-uf 16-WVDC, tantalum |
| C3,C4 | 100-pf 16-WVDC, disk |
| ADDITIONAL PARTS |
| B1 | Battery holder |
| X1 | 32 Khz crystal |
| S1,S2 | Single-pole, momentary contact, PC mount |
| SP1 | PCB mount speaker |