Table of Contents
Overview:
In this simple Arduino project, you will learn how to build a stopwatch by interfacing an OLED display with Arduino and push buttons. Here we are using 3 push buttons to start, stop/pause and reset the timer of the stopwatch. We are making the stop watch to measure from milliseconds to minutes by using a special function millis().
Millis() function is powerful and measures even milliseconds with 100% accuracy. So lets implement this function to measure the time elapsed between start and stop. In this project we are using OLED display but we can even use LCD display or 7 segment displays to display the time.
Required components:
Product Name | Quantity | ![]() | ![]() |
---|---|---|---|
Arduino Microcontroller | 1 | https://amzn.to/3pxTUcK | https://amzn.to/3KpUQry |
0.96" OLED display with I2C | 1 | https://amzn.to/34f0GNi | https://amzn.to/35HZK4E |
Tactile Push Button | 1 | https://amzn.to/3uc4gRh | https://amzn.to/3ubrLdc |
5V power supply (Micro USB or External). | 1 | https://amzn.to/3s1a8g3 | https://amzn.to/364yInH |
Few Connecting Wires | https://amzn.to/3H2BV4e | https://amzn.to/3J0WVu2 |
Circuit diagram of Arduino Stopwatch with OLED display
Now connect all the required components using the below circuit diagram.
You can use any Arduino microcontroller boards such as Arduino UNO, Arduino PRO mini, Arduino Mega to make this, we choose to use Arduino NANO to make the project compact and easy.
As you can see from the above circuit, OLED display power pins VCC and GND are connected 5V and GND pins of Arduino NANO. The I2C pins of OLED display SDA and SCL are connected to A4 and A5 of Arduino respectively.
Next the first pin of push button is connected to digital D6 of Arduino and second pin of push button is connected to GND pin of Arduino. Here we used push button without resistor as Arduino pins has internal pullup resistor which we can activate through the code.
If you want to use LCD display check this tutorial: Connect LCD display to Arduino
Program code for Arduino stopwatch:
After connecting everything its time to upload the Program code to Arduino using Arduino IDE, connect Arduino to a PC where Arduino IDE is installed. Choose the board as Arduino NANO or which ever you use and select the correct port from the Tools menu. Install required libraries from the built in Library manager or you can download the latest version from the below links
Required Libraries:
Next copy the below code and paste it in IDE workspace and press upload button. That’s it the code will be uploaded.
// Tested and compiled with no errors // Arduino Stopwatch with OLED display // with Start, Stop and Reset button // source - www.circuitschools.com #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <Fonts/FreeSans9pt7b.h> #include <Fonts/FreeSans12pt7b.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // SCL A5 // SDA A4 #define OLED_RESET 0 // GPIO0 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { pinMode(6,INPUT_PULLUP); //Switch display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) display.display(); display.clearDisplay(); display.setFont(&FreeSans9pt7b); display.setTextColor(WHITE); display.setCursor(0, 25); display.println("Circuit Schools"); display.display(); delay(2000); } int poz=1; int broj=1; int kretanjeY=30; int sec1=0; int min1=0; unsigned long msec=0; unsigned long mili=0; int pres=0; int fase=0; int start=0; unsigned long tim=0; void loop() { display.clearDisplay(); if(digitalRead(6)==0) { if(pres==0) { fase=fase+1; pres=1; if(fase>2) fase=0; } }else{pres=0;} if(fase==0) { display.setFont(&FreeSans9pt7b); display.setFont(); display.setCursor(33, 15); display.println("STOPWATCH"); display.setFont(&FreeSans12pt7b); display.setFont(); display.setCursor(30,35); display.print("Press Start"); sec1=0; min1=0; tim=0; mili=0; msec=0; start=0; } if(fase==1) { display.clearDisplay(); display.setFont(&FreeSans12pt7b); display.setFont(); display.setCursor(37,0); display.print("Stopwatch"); display.setFont(&FreeSans9pt7b); if(start==0) { start=1; tim=millis(); } msec=(millis()-tim); min1=msec/60000; if((msec/1000)>59) { sec1=(msec/1000)-(min1*60); }else{ sec1=msec/1000; } mili=(msec%1000)/10; display.setCursor(42,30); if(min1<=9) { display.print("0"); display.print(min1); }else {display.print(min1);} display.print(":"); if(sec1<=9) { display.print("0"); display.print(sec1); }else {display.print(sec1);} display.setFont(&FreeSans12pt7b); display.setCursor(50,57); if(mili<=9) { display.print("0"); display.print(mili); }else {display.print(mili);} } if(fase==2) { display.clearDisplay(); display.setFont(&FreeSans12pt7b); display.setFont(); display.setCursor(52,0); display.print("Time:"); display.setFont(&FreeSans9pt7b); display.setCursor(42,30); if(min1<=9) { display.print("0"); display.print(min1); }else {display.print(min1);} display.print(":"); if(sec1<=9) { display.print("0"); display.print(sec1); }else {display.print(sec1);} display.setFont(&FreeSans12pt7b); display.setCursor(50,57); if(mili<=9) { display.print("0"); display.print(mili); }else {display.print(mili);} } display.display(); }
Output and testing
After uploading the code OLED display show “Press start” as shown in the below output image.
Now click on the push button to start the stopwatch timer, the timer runs. To stop and check the elapsed time click the push button again, it displays the elapsed time. To reset the timer click the button again, to start again press the button again.
If you like this project please subscribe to our YouTube Channel “Circuit Schools“ to encourage us to publish more interesting projects. If you have any doubts write to us from below comment section.
Hi there iv’e built the stopwatch. circuit it works great could you tell me how to remove the RESET function as i just wats an elapsed timer thanks
Regards Harry
What are the following variables :
int poz=1;
int broj=1;
int kretanjeY=30;