Stopwatches are time measurement devices. What does this have
to do with AVRs? Aren't those swiss made mechanical devices
with the three knobs better than a micro? Nevertheless, here
is the necessary theory to do it with a micro. And even some
1 What it needs to have an own stopwatch design
Now, we need
a clock generator to measure times, and
a display to read the measurements results, and
a few knobs or keys to start, stop and restart the
an AVR (or whatever micro you prefer).
2 A closer look on these components
2.1 Clock generators
AVRs all have an internal clock generator (only a few very old
ones don't). This usually works roughly at 1.0 MHz.
"Roughly" means that this is not really exact,
more the "near-by" type of accuracy: guaranteed
at +/- 10%, depending from operating voltage and temperature.
+/- 10% means: if you measure one minute, the result could
be by six seconds smaller or larger than expected. If you
do some extra work in holding the operating voltage within
a small range and by adjusting the calibration byte) you can
reduce those inaccuracies down to 2% (which still means
that your minute is by 1.2 seconds inaccurate. Those who
do not need more accuracy, e.g. for cooking eggs, is
done with the 10%. For a stopwatch measuring times of a
100 m hurdle run on three tracks you are done with
the 2% because inaccuracies are all the same for the four
runners. But if you want to compare those times with those
of thousands of other races, it needs to be more exact.
More exact is a crystal oscillator. Crystals on the market
have inaccuracies of between 30 and 50 ppm, so with
this we are at 0.005% inaccuracy. Over one minute that means
3 ms inaccuracy, close enough for the amateur's needs.
Those who still want to have it more exact, can
put the crystal into an oven that holds its temperature
exactly at 40°C (or any temperature above that to be
compatible with maximum ambient temperatures in other
measure the exact frequency of hundreds of such crystals
with an ultra-fine counter and hand-select the few exemplars
with the nearest of what is imprinted on them,
think about the accuracy that can be reached in detecting
events with photo sensors or light barriers or of hearing
pistol shots in 100 m distance and the delay between
those audio signals and subsequent key closure acts.
For fanatic accuracy fans: time measuring is always as exact
as the weakest chain link in the whole chain.
Which crystal to select? Because the more exact stopwatch
has to measure 10 ms (normal requirement) or 1 ms
(fanatic requirement), the crystal's frequency has to be
divisable without remainder by 100 or 1,000. Because
timers in an AVR have divider rates of 8 or 64, this also
haS to fit into the frequency. When using CTC with a timer
(Clear Timer on Compare), divider rates between 1 and 256
(with an 8 bit timer) or between 1 and 65,536 (with a
16 bit timer) has to fit in between. And: if you operate
the stopwatch with batteries the clock frequency shall not
be too high to reduce the power consumption of the AVR.
With an 8 bit timer crystal frequencies of 2.0,
2.048 or 8.0 MHz are convenient. With 2.048 MHz
your timer can divide by a prescaler of 8 and then by 256
and you are at 1,000 Hz, so you do not need CTC and
the timer overflow can interrupt. With a 16 bit timer
additionally a crystal with 4.0 MHz can be used
(prescaler 8, CTC at 499).
With an external crystal (or crystal oscillator) to be
attached, one third of the AVR types are not compatible
with this requirement, because they do not allow that.
Unless you do not need a display which 50,000 people
in a sporting stadium can see, only LCDs come into question.
For a battery operation LCDs are a must. As those are
available mostly in 4.5 V and above (only a few are
at 3.3 V) your battery has to be 3 times 1.5 V
AA or AAA size or 4 times 1.2 V rechargeables.
In case of the stadium size display: use two micros. The
first for measuring only and the second for display only.
And in between those two add two or three lines for
An LCD needs four or eight data bus connections and two
or three control connections between the micro and the
LCD (see more theory on that under
this page here). That makes
a minimum of six and a maximum of 11 portpins.
To the left the available AVR types for the minimum version,
to the right those for the maximum version are listed.
Both are based on the internal RC oscillator clock.
Hint: The selections were made with the software described
and is available for free download and use.
Here the same list but with an external crystal or crystal
oscillator as clock. That reduces the possible types largely
to the ones listed here.
2.3 Knobs and keys
Each stopwatch needs some keys. At minimum those are:
the reset key, by which the time can be cleared
(alternatively you can remove the battery, and
a Start/Stop key to start and stop measuring.
If the stop watch shall be able to store more than one
time info (e.g. for comparison), an additional
"Store" key is required. If it should be able
to measure times on up to four different tracks
simultaneously, you need further N keys. A minimum of
two and a maximum of 10 pins for keys so are necessary.
This limits the number of possible devices further.
Note for C-freaks: From five tracks on and above your
favourite type is not among the available AVRs for this
Because all keys toggle (some not immediately but later
on when corrosion has done its work), protection against
this is a must, either by hard- or by software. Hardware
protection against toggling e.g. looks like that:
When tied to ground with the key, the input pin
immediately shows low. When released, the voltage on
the capacitor of 1 µF is slowly charged
with the internal pull-up resistor of 50 kΩ.
It needs roughly 45 ms to reach a high level
(3.0 V at 5.0 V operating voltage). If
the operating voltage is 3 V, the load process
lasts slightly longer (roughly 55 ms).
During that time period, further toggle pulses unload
the capacitor again and restart the charging period,
but do not lead to low-high signals. That suppresses
all signals before and during the load period, but
does not lead to delays in detecting key closures
This simple solution has some serious disadvantages:
The capacitor unloads immediately over the key
on closure. This causes a very high peak current.
Increased ageing and corrosion of the key contacts
are the consequence, those can even fail
When the supply voltage is turned off, the
voltage on the VCC pin is dropping very fast. The
voltage on the capacitor is still high and exceeds
the VCC voltage. The capacitor then will supply
current to the input pin and, by that, can destroy
this sensible CMOS pin.
To avoid those consequences the following can be done:
Between the electrolytical capacitor and the key
a resistor limits the unload current. The resistor
should not be too large to not cause delays above
1 ms between key closure and voltage drop
below the High==>Low voltage level.
100 Ω are sufficient.
Between the capacitor and the port input pin a
Schottky diode blocks reverse current flow into
the pin. The reverse current is small enough to
not harm the CMOS pin.
The necessary additional hardware should make clear
that a software solution is the simpler and easier
alternative to hardware debouncing with an electrolytic
Software toggle protection is easy in a stopwatch
because the software needs to look at all the inputs
every 10 resp. 1 ms. Times in between those
periods do not matter, so no external interrupts
(INTn or PCINTn) are necessary. So key events can
be included in the time measurement scheme. And:
key toggle protection can also be included there.
2.4 Which AVR?
With a 4 bit LCD data bus and up to four keys the ATtiny24
is a convenient selection and has all necessary pins.
Such a project can use the ATtiny24-LCD module described
Control of the LCD can use the include software decribed
there, but adds a few modifications. The example in
is driven with this LCD software.
If the LCD should be driven in 8 bit mode or if more
keys than four are required, either
ATmega8 or ATmega8A, or
ATmega48(A, PA) or ATmega88(A, PA)
in a 28 pin casing come into question. Those who need
more than six keys or need a RS232 or an I2C
interface (e.g. for an external display) change to a
40 pin type such as ATmega16 or ATmega8515.
a four-channel stopwatch with ATmega8 is described.
Two projects that should cover 90% of all needs.
Praise, error reports, scolding and spam please via the