Creating an Arduino Stopwatch

Introduction: Creating an Arduino Stopwatch

This will use multiple different components as well as Arduino code in order to create a stopwatch, with LEDs to signal on and off state as well as a buzzer to indicate the off state.

Supplies

1x Arduino Uno R3

2x LED (one green, one red)

1x Slideswitch

1x Pushbutton

1x Buzzer

4x Resistor (2x 220ohms, 2x 1kohms)

1x LCD

1x Breadboard

Step 1: Setting Up the Circuit

Firstly, connect 5v power and ground to the positive and negative rails on the breadboard respectively. These are marked red for positive and black for negative on the breadboard.

Then, we will wire up the LCD. This one will be a little complicated, so we will do it first to get it out of the way. Connect the pins labelled GND, V0, and RW to ground. Connect the VCC pin to power. Wire the RS, E, and DB4 through DB7 to digital pins. I will be using digital pins 2, 3, 4, 5, 11, and 12.

Next, we will wire the switch and button. The switch will require that terminal 1 be connected to ground, terminal 2 to be connected to power, and the common terminal be connected to a digital pin. I will be using digital pin 8. The switch will be connected to ground through a 1kohm resistor. The button will require that terminal 1a be connected to ground and terminal 2a be connected to ground and a digital pin. I will use digital pin 10. The button will be connected to power through a 1kohm resistor.

Almost there. We will wire the LEDs now. Both LEDs will be connected to ground via a 220 ohm resistor, as well as connected to a digital pin each. I will use digital pin 7 for the green LED, and pin 9 for the red LED.

Lastly, the buzzer. Hook up the negative terminal to ground and the positive terminal to a digital pin. I use digital pin 6.

Step 2: The Code

The code should look something like this:

//include library for lcd display
#include <LiquidCrystal.h>

//initialize pins used by display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup()
{
lcd.begin(16,2); //define size of screen
lcd.print("Stopwatch");
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(10, INPUT);
pinMode(13, INPUT);
pinMode(6, OUTPUT);
}

void loop()
{
if (digitalRead(8) == HIGH){
digitalWrite(9, LOW);
lcd.setCursor(0,1);
lcd.print(millis() / 3600000); //calculate hours
lcd.print("h:");
lcd.print(millis() / 60000); //calculate minutes
lcd.print("m:");
lcd.print(millis() / 1000); //calculate seconds
lcd.print("s");
digitalWrite(7, HIGH); //when stopwatch is running, turn on green led
}

else{
digitalWrite(7, LOW); //turn off green led
digitalWrite(9, HIGH); //when stopwatch is not running, turn on red led
}

if (digitalRead(10) == LOW)
{
lcd.clear();
lcd.begin(16,2);
lcd.print("Stopwatch");
tone(6, 1000); //buzz the buzzer (WARNING LOUD)
delay(10);
noTone(6); //stop buzzing
}

}

This initializes the library that we have to import in order for the LCD to work, as well as which pins the LCD is using.

//include library for lcd display
#include <LiquidCrystal.h>

//initialize pins used by display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

This is everything we run once at the start of the program. We define the size of the screen and print something to start. We also set our pins as input or output.

void setup()
{
lcd.begin(16,2); //define size of screen
lcd.print("Stopwatch");
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(10, INPUT);
pinMode(13, INPUT);
pinMode(6, OUTPUT);
}

This part runs the calculations behind the timekeeping of the stopwatch, which uses a base count of milliseconds which it then converts into seconds, minutes and hours, and print it to the LCD. As well, when the stopwatch is running, the green LED is toggled on. This part of the code only runs if pin 8 is read as a high, or "on", in this scenario.

void loop()
{
if (digitalRead(8) == HIGH){
digitalWrite(9, LOW);
lcd.setCursor(0,1);
lcd.print(millis() / 3600000); //calculate hours
lcd.print("h:");
lcd.print(millis() / 60000); //calculate minutes
lcd.print("m:");
lcd.print(millis() / 1000); //calculate seconds
lcd.print("s");
digitalWrite(7, HIGH); //when stopwatch is running, turn on green led
}

This part defines what happens when the switch is not read as high. This will turn off the green LED and turn on the red LED, when the switch is read as a low, or "off". This essentially pauses the stopwatch.

else{
digitalWrite(7, LOW); //turn off green led
digitalWrite(9, HIGH); //when stopwatch is not running, turn on red led
}

This part is all the code necessary for resetting the stopwatch. What it does is clear the LCD, and redefine it's size as well as reprinting "Stopwatch". As well, I use the buzzer to denote that the reset has been successful.

if (digitalRead(10) == LOW)
{
lcd.clear();
lcd.begin(16,2);
lcd.print("Stopwatch");
tone(6, 1000); //buzz the buzzer (WARNING LOUD)
delay(10);
noTone(6); //stop buzzing
}

}

Step 3: Demonstration

Notes:

  • The simulation time is rather slow in Tinkercad. The stopwatch does correctly keep time.
  • The buzzer is rather loud. Be careful with how loud you play the video.

Be the First to Share

    Recommendations

    • Soup & Stew Speed Challenge

      Soup & Stew Speed Challenge
    • Micro:bit Contest

      Micro:bit Contest
    • Back to School: Student Design Challenge

      Back to School: Student Design Challenge

    Comments

    0
    JunkbotJosh
    JunkbotJosh

    Question 3 months ago on Step 3

    I thought the start/stop button would reset if I clicked it a third time but when I tried it out in Tinkercad, I had to press the button on the arduino, is that just Tinkercad or will it be like that in real life? if it will be like that, do you know how I could change it to make the start/stop button reset it aswell.