Simple Stopwatch Using Arduino

Simple Stopwatch Using Arduino

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

  1. LCD Display: Connect the LCD pins to the Arduino according to the table above. Use a 10kΩ potentiometer to adjust the LCD’s contrast.
  2. Push Buttons: Connect the first button (for start/stop) to Pin 8 and the second button (for reset) to Pin 9.
  3. 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:

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the provided code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino.
  5. 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.

Timer-Based LED Blinker Using Arduino

Timer-Based LED Blinker Using Arduino

The objective of this project is to create a Timer-Based LED Blinker using Arduino. By using the millis() function, we will create a non-blocking code that controls the blinking of an LED without the use of the delay() function, allowing other code to run simultaneously.

Fundamental Programming Concepts

  • Timers (millis()): Tracks time since the program started, allowing for timed actions without blocking the program.
  • Digital Output (digitalWrite()): Sends signals to control the LED by turning it on or off.
  • Non-Blocking Code: Ensures that the program doesn’t pause while waiting for events like blinking, enabling multitasking.

Requirement Components

To complete this timer-based LED blinker using Arduino project, you will need:

  • Arduino Uno Board
  • LED
  • 220Ω Resistor
  • Breadboard
  • Jumper Wires
  • USB Cable (for connecting Arduino to your computer)

Circuit Diagram

Circuit Connection

Component Arduino Pin
LED (Anode) Pin 13
LED (Cathode) GND
220Ω Resistor Between LED anode and Pin 13

How to Connect the Circuit

  1. LED: Connect the anode (longer leg) of the LED to Pin 13 on the Arduino and the cathode to GND using a 220Ω resistor.
  2. Power and Ground: Ensure the breadboard is connected to the Arduino’s 5V and GND pins for a stable power supply.

Explanation of Circuit

  • The LED is connected to Pin 13 on the Arduino, which allows the board to control the blinking using digitalWrite().
  • The millis() function will track the time elapsed since the Arduino was powered on and control when the LED should turn on or off.

Programming Section for Timer-Based LED Blinker Using Arduino

Arduino Syntax

Topic Name Syntax Explanation
digitalWrite() digitalWrite(pin, value) Sets a digital pin to HIGH or LOW to turn the LED on or off.
millis() millis() Returns the number of milliseconds since the Arduino started running the current program.
if statement if (condition) Executes code based on whether a condition is true or false.

Arduino Code:

Here’s the Arduino code to create a timer-based LED blinker using millis():

const int ledPin = 13;    // Pin connected to the LED
unsigned long previousMillis = 0;  // Store the last time the LED was updated
const long interval = 1000;  // Interval for blinking (1 second)
void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}
void loop() {
  unsigned long currentMillis = millis();  // Get the current time
  // Check if the time interval has passed
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;  // Save the current time as the last time
    // Toggle the LED state
    if (digitalRead(ledPin) == LOW) {
      digitalWrite(ledPin, HIGH);  // Turn the LED on
    } else {
      digitalWrite(ledPin, LOW);   // Turn the LED off
    }
  }
}

Steps to Upload Code:

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the provided code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino.
  5. Watch the LED blink on and off at 1-second intervals using the millis() timer.

Check Output

Once the code is uploaded, the LED connected to Pin 13 should blink at 1-second intervals. The millis() function will ensure that the code is non-blocking, allowing other processes to run simultaneously if needed.

Troubleshooting Tips

  • LED not blinking? Double-check the LED’s connections to ensure it is connected correctly to Pin 13 and GND.
  • No output? Verify that the code is uploaded correctly and that the Arduino is powered on.
  • Flickering LED? Ensure the time interval and wiring are correct. You can adjust the interval by modifying the interval variable in the code.

Further Exploration

  • Multiple LEDs: Add more LEDs with different intervals and control them using millis() to create complex blink patterns.
  • Variable Blink Rate: Use a potentiometer to control the blink rate dynamically by adjusting the interval based on the potentiometer’s analog input.
  • LCD Display: Display the elapsed time on an LCD along with the blinking LED.

Note

This project introduces the concept of using timers (millis()) to create a non-blocking LED blinker. Unlike delay(), which halts the program, millis() allows multitasking by keeping the program running while still tracking the time.

FAQ

Q1: What is the difference between delay() and millis()?
delay() pauses the entire program for a specified time, while millis() continues to run the program while tracking time, allowing you to perform other tasks in the background.

Q2: How does millis() work in this project?
The millis() function returns the time elapsed since the program started. By comparing the current time to the last time the LED state was updated, you can create a timed blinking effect without pausing the program.

Q3: Can I change the blink rate?
Yes, you can adjust the blink rate by changing the interval variable in the code. For example, changing the interval to 500 will make the LED blink every 0.5 seconds.

Q4: Can I control multiple LEDs using millis()?
Yes, you can control multiple LEDs by creating separate millis() intervals for each LED and toggling them independently.

Q5: Why is it important to use non-blocking code?
Non-blocking code ensures that your program can perform multiple tasks simultaneously. It is crucial for more advanced projects where timing and multitasking are needed.