This guide to building a stopwatch with Arduino provides: a parts list, code, pictures of the finish line, and STL files for 3D printing. Follow the steps within the post to build your own stopwatch finish line with Arduino. With an objective to create an electronic finish line for a local robotics team, our team of Application Engineering Technician Apprentices welcomed the challenge. We split up our project into stages: the idea, the planning, and the final construction.
The Idea
We collaborated as a group on what we wanted to be in our project. What exactly would we be building? Our assignment was to build a finish line with a working timer for the robotics team to race across. We decided to create an enclosure that hosts a sensor with wiring. Attached to the enclosure is a box that holds the Arduino, two buttons and our LCD display. Then, the user would push the start button and the timer would begin. At this moment, the robot would race across the floor until it reaches the finish line. The sensor would stop the timer and the final time would show on the LCD display.
The Planning
After we had a general idea of the project, we found the parts we wanted to use. Using Digi-Key’s website, we created a parts list. When picking an LCD display, we chose part NHD-0116AZ-FL-YBW. The parameters are 16 x 1 display format. This is important in the code we have attached for the timer. Please keep this in mind if you decide to use a different display, as you will need to change the code to fit the characters on the screen. In searching for an enclosure for the two push buttons and LCD display, we realized that it needed to be sturdy enough to handle a small amount of pressure while the user pushes the buttons. We decided that 3D printing the parts for our finish line project would be our best solution, but if you do not have access to one, you can make a case with parts from your local hardware store.
The Final Construction
When starting this project, you will need a parts list. Whether you use the list provided or your own, that is how the race starts. You will need a 3D printer to make the finish line case and starter box unless you are using parts from a hardware store. Once you have everything, you can start the engine and begin to put the pieces together.
The Parts
Starter Box
- 1 x Arduino Microcontroller
- 1 x LCD Display
- 1 x Power Cable
- 2 x Connector Plugs
- 1 x Breadboard
- 1 x White Button
- 1 x Black Button
- 4 x 1k Ohm Resistor
- 1 x Potentiometer
Laser Case
- 1 x Laser
- 1 x Battery
- 1 x Battery Holder/Switch
Photoresistor Case
- 1 x Photoresistor
- 2 x Connector Sockets
Misc.
- 1 x Glue
- 1 x 22 AWG Red Wire
- 1 x 22 AWG Black Wire
Assembly
We modified our code to accept the LCD of choice. Please use the code provided if you are using the LCD from our parts list.
If you need assistance setting up Arduino, please review this Digi-Key post: How to Get Started with Arduino.
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //assigning pins for LCD
double startTime; //declaring variables for time, buttons, laser, states
double finishedTime;
double elapsedTime = 0;
int start = 0; //start button state
int startBtn = 13; //start button pin
int photoresState = 0; //photoresistor state
int photoRes = 11; //photoresistor pin
int reset = 0; //reset button state
int resetBtn = 12; //reset button pin
void setup()
{
lcd.begin(8,2); //to start LCD. Our LCD's memory is stored the same as an 8*2 display even though it is 16*1
lcd.clear();
Serial.begin(9600);
pinMode(startBtn, INPUT); //declaring start button as input
pinMode(photoRes, INPUT); //declaring photoresistor as input
pinMode(resetBtn, INPUT); //declaring reset button as input
}
void loop() {
start = digitalRead(startBtn); //reading button states
photoresState = digitalRead(photoRes);
reset = digitalRead(resetBtn);
startTime = millis(); //declaring start as millis. Millis is a onboard clock for arduino
if (start == LOW) { //If start button isn't pressed, display "Press Start" on the LCD.
lcd.clear();
lcd.print("Press");
lcd.setCursor(0,1); //for a 16*1 lcd with 8*2 memory this will set the cursor at the 9th column
lcd.print("Start");
delay(100);
}
if (start == HIGH) { //If start button is pressed, start loop
lcd.clear();
while (photoresState == HIGH) { //while laser is hitting photoresistor continue loop
finishedTime = millis(); //delcaring finished to what millis is when the two above conditions are met
elapsedTime = (finishedTime - startTime) / 1000; //math to show the amount of time elapsed since button was pressed. Divided by 1000 to show in seconds
lcd.print(elapsedTime); //printing elapsed time
lcd.setCursor(0, 1); //for a 16*1 lcd with 8*2 memory this will set the cursor at the 9th column
lcd.print("secs"); //printing "secs" starting from the above set cursor
lcd.setCursor(0, 0); //returning cursor to first column
photoresState = digitalRead(photoRes); //reading current state of photoresistor
if (photoresState == LOW) { //if the laser is not hitting the photoresistor break
break;
}
}
}
if (photoresState == LOW) { //if the laser is not hitting the photoresistor -
lcd.clear();
delay(500);
while (reset == LOW) { //while reset button isn't pressed display time.
lcd.print(elapsedTime);
lcd.setCursor(0, 1);
lcd.print("secs");
lcd.setCursor(0, 0);
reset = digitalRead(resetBtn);
}
}
}
Put together the starter box. This includes the Arduino, two push buttons, 3D printed case, wiring, and LCD. The 3D printed case has cutouts for each of the push buttons and LCD. The Arduino is stored safely inside. Before inserting the parts, you will need to connect everything together. The following diagram is on a breadboard to show pin connections. The potentiometer exists to change the LCD contrast.
Arduino Pinouts:
Pin 13 = Start, or the white button.
Pin 12 = Reset, or the black button.
Pin 11 = Photoresistor.
The Body Pieces
After putting together the starter box, the laser needs to be connected. We used the cases to line up the laser and photo resistor. The laser was placed underneath the case and aimed directly at the other case.
After being lined up, the laser was attached to the case using hot melt. The wires of the laser went through a hole and was attached to a battery using solder. The battery was stored inside a smaller case with a switch for easy on and off access.
The photo resistor was attached to the other case using hot melt. The photo resistor needs to be visible through the hole so the laser beam can access it.
This will be your final step. Now you are ready to point the laser beam at the photo resistor and start racing! Any passage through the laser should stop the timer after pressing the white button to start. Make sure to reset the time by pressing the black button every time the laser trips. If the white button is pressed by accident, the laser will need to be manually tripped and then reset again with the black button.
Files
Parts List:
Finish Line Parts (8.9 KB)
Finish Line Code with Arduino:
Arduino Code (3.5 KB)
3D Printed Starter Box:
Starter Box (27.4 KB)
3D Printed Finish Line Case:
Finish Line Case (56.8 KB)