What it shows
The millis() timer counts milliseconds since the board started. Storing the start time and subtracting it from the present value gives elapsed time, which can be shown to a hundredth of a second.
The science
millis() returns the number of milliseconds since reset, increasing about every thousandth of a second. When Start is pressed the sketch stores the present value; elapsed time is the present value minus the stored one. The buttons use INPUT_PULLUP, so a pressed button reads LOW. A short delay or a check of the time since the last press (debouncing) prevents one press being counted several times because the contacts bounce.
Materials
Cost: under Rs 1,500. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- An Arduino Uno or compatible board
- A 16 x 2 I2C LCD
- Three push buttons (start/stop, lap, reset)
- A breadboard and jumper wires
- A power bank or USB cable
- A cardboard case
Safety
For this project: USB power only.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Connect the LCD I2C module to 5 V, GND, A4 (SDA) and A5 (SCL).
Connect the Start/Stop button between pin 2 and GND, the Lap button between pin 3 and GND and the Reset button between pin 4 and GND.
Set the three pins to INPUT_PULLUP in setup.
Make a variable for the running state and the start time; on Start press, store millis() and set running to true.
While running, display minutes, seconds and hundredths on the first LCD line, updating every 50 ms.
On Lap, copy the present time to the second line without stopping the clock.
On Stop, freeze the display; on Reset, clear all values; add a 200 ms debounce on every button.
Compare against a phone stopwatch over one minute and find the error.
What you should see
The stopwatch shows running time to a hundredth of a second, with lap times on the second line, and matches a phone within about a quarter of a second per minute.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What does millis() return?
The number of milliseconds since the Arduino started.
2What is switch bounce?
The contacts rattle when pressed, giving several false presses in a few milliseconds.
3Why use INPUT_PULLUP?
The internal resistor holds the pin high so no external resistor is needed.
4Why is the board clock not perfect?
It uses a ceramic resonator whose frequency drifts a little.
Take it further
Use two infrared sensors as start and stop gates to time a toy car down a ramp.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Robotics
Arduino and Coding for Robots Study Book
The complete study notes, practice questions and glossary for Arduino and Coding for Robots
