The objective of this project is to build a Simple Stopwatch using Arduino, where the elapsed time is displayed on an LCD display. Using buttons for start/stop and reset, the stopwatch will track the time using the millis() function, demonstrating how to use timers in Arduino without blocking code execution.
Fundamental Programming Concepts
- Timers (millis()): Tracks the elapsed time without halting the program.
- Digital Input (Button): Allows the user to control the start, stop, and reset functions of the stopwatch.
- LCD Display: Displays the time elapsed in a human-readable format.
Requirement Components
To complete this simple stopwatch using Arduino project, you will need:
- Arduino Uno Board
- LCD Display (16×2)
- Push Buttons (2)
- 10kΩ Potentiometer (for LCD contrast adjustment)
- Breadboard
- Jumper Wires
- USB Cable (for connecting Arduino to your computer)
- 220Ω Resistor (optional, for LCD backlight control)
Circuit Diagram
Circuit Connection
Component | Arduino Pin |
LCD RS | Pin 12 |
LCD E | Pin 11 |
LCD D4 | Pin 5 |
LCD D5 | Pin 4 |
LCD D6 | Pin 3 |
LCD D7 | Pin 2 |
Button 1 (Start/Stop) | Pin 8 |
Button 2 (Reset) | Pin 9 |
Potentiometer (middle) | LCD V0 (contrast) |
Potentiometer (side 1) | 5V |
Potentiometer (side 2) | GND |
LCD VSS, RW, K | GND |
LCD VDD, A | 5V |
How to Connect the Circuit
- LCD Display: Connect the LCD pins to the Arduino according to the table above. Use a 10kΩ potentiometer to adjust the LCD’s contrast.
- Push Buttons: Connect the first button (for start/stop) to Pin 8 and the second button (for reset) to Pin 9.
- Power: Ensure the Arduino is powered via USB, and connect the 5V and GND lines to the breadboard.
Explanation of Circuit
- The LCD display is used to show the elapsed time of the stopwatch.
- Two push buttons allow the user to start/stop the stopwatch and reset the time. The buttons are connected to Pin 8 (start/stop) and Pin 9 (reset).
- The millis() function is used to track the elapsed time without pausing the program, ensuring the stopwatch works smoothly.
Programming Section for Simple Stopwatch Using Arduino
Arduino Syntax
Topic Name | Syntax | Explanation |
digitalRead() | digitalRead(pin) | Reads the state of the button (HIGH or LOW). |
digitalWrite() | digitalWrite(pin, value) | Sends a HIGH or LOW signal to control outputs. |
millis() | millis() | Returns the number of milliseconds since the program started. |
lcd.print() | lcd.print(data) | Prints the specified data to the LCD display. |
if statement | if (condition) | Executes code based on whether a condition is true or false. |
Arduino Code:
Here’s the Arduino code to build a simple stopwatch using millis(), buttons, and an LCD display:
#include <LiquidCrystal.h>
// Initialize the LCD (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int startStopButton = 8;
const int resetButton = 9;
bool running = false; // Stopwatch state (running or stopped)
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
unsigned long currentMillis;
void setup() {
// Initialize LCD and buttons
lcd.begin(16, 2); // 16x2 LCD
pinMode(startStopButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
lcd.print("Stopwatch Ready"); // Initial display
delay(2000);
lcd.clear();
}
void loop() {
// Check for Start/Stop Button Press
if (digitalRead(startStopButton) == LOW) {
delay(200); // Debouncing
if (!running) {
startTime = millis() - elapsedTime; // Start from where we left off
running = true;
} else {
elapsedTime = millis() - startTime; // Save the time when stopped
running = false;
}
}
// Check for Reset Button Press
if (digitalRead(resetButton) == LOW) {
delay(200); // Debouncing
elapsedTime = 0;
running = false;
}
// Calculate and Display Time
if (running) {
currentMillis = millis() - startTime;
} else {
currentMillis = elapsedTime;
}
int seconds = (currentMillis / 1000) % 60;
int minutes = (currentMillis / 60000) % 60;
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.setCursor(6, 0);
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
delay(100); // Small delay to prevent flickering
}
Steps to Upload Code:
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and select the correct Board and Port.
- Copy and paste the provided code into a new sketch.
- Click the Upload button to transfer the code to your Arduino.
- Use the buttons to start/stop and reset the stopwatch, observing the time on the LCD.
Check Output
Once the code is uploaded, the LCD will display “Stopwatch Ready” at first. Press the Start/Stop button to begin the stopwatch. The Reset button will reset the elapsed time to zero. The elapsed time will be displayed on the LCD in minutes and seconds.
Troubleshooting Tips
- LCD not displaying correctly? Double-check the connections and ensure the potentiometer is properly adjusted for contrast.
- Button presses not working? Verify the button wiring and make sure you’re using INPUT_PULLUP to properly register button presses.
- Inconsistent timing? Ensure the use of millis() for accurate time tracking instead of delay(), which can interfere with the stopwatch’s timing.
Further Exploration
- Add Hours: Extend the code to display hours if the stopwatch runs for more than 60 minutes.
- Add More Buttons: Implement a lap time feature with additional buttons to save and display lap times.
- Buzzer: Add a buzzer to sound when the stopwatch reaches a certain time limit.
Note
This project demonstrates how to build a simple stopwatch using millis() for non-blocking time tracking. Using buttons for start/stop and reset, and an LCD display to show the time, this project introduces key concepts for building interactive Arduino-based systems.
FAQ
Q1: How does millis() work in this project?
The millis() function returns the number of milliseconds since the program started. By subtracting the start time from the current time, we can calculate how long the stopwatch has been running.
Q2: Can I add more buttons for different functions?
Yes, you can add more buttons for additional functions like saving lap times, adding pause, or other custom features.
Q3: Why use millis() instead of delay()?
millis() allows the program to track time without blocking code execution, meaning other parts of the program (like button presses) can still function without interruption.
Q4: Can I use a 20×4 LCD instead of 16×2?
Yes, you can use a larger LCD by adjusting the lcd.begin() function to match the size of your display.
Q5: Can I display the time in a different format (like milliseconds)?
Yes, you can modify the code to display milliseconds or any other time format by adjusting the calculations and display logic.