I recently bought an Arduino Mega2560 beginner’s starter kit from MicroCenter and was looking for an interesting project to use it in. I wanted to use the 1602 LCD from the kit in the project, somehow. It occurred to me that I need a portable stopwatch for my Rubik’s Cube solving so that’s what I made today. It uses the Arduino for counting seconds, minutes and hours and displays the count on a 1602 (16 columns by 2 rows) LCD display. There is a pause button, for when you want to go get some coffee and a reset button for when you lose your focus and need to start over (which happens to me a lot when my kids are around…).

Parts list:

1 x Arduino (Uno or Mega or whatever you have is OK, but there are 8 pins + 5v and GND used here)

1 x1602 LCD display (any as long as it uses Hitachi compatible pins)

1 x potentiometer (I used a 50K Ohm one that came with my kit but usually 10K Ohms is recommended)

1 x breadboard with lots of holes

2 x 4-pin momentary switches (the kind that you click once and then they reset)

2 x 10K Ohm resistors (don’t ask me the colors for the bands; I am partially colorblind. No, really.)

1 x N4001 Diode (to protect the backlight pin on the LCD)

tons of jumper wires (about 20 actually)

The Arduino sketch:

#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // input pins: int pausePin = 22; int resetPin = 8; // output pin: int ledPin = 13; // time counting things: unsigned long previousMillis = 0; long interval = 1000; // one second // pin state trackers: int resetButtonState = 0; int pauseButtonState = 0; int ledState = LOW; //de-bounce the switches: int lastPauseButtonState = LOW; int lastResetButtonState = LOW; long lastPauseDebounceTime = 0; long lastResetDebounceTime = 0; long debounceDelay = 50; // setup the pause tracker and stopwatch seconds counter boolean isPaused = false; unsigned long secs = 0; unsigned long mins = 0; unsigned long hrs = 0; void setup() { lcd.begin(16, 2); // the LCD has 16 columns and 2 rows lcd.setCursor(1,0); // put the cursor at the first column on the first row lcd.print("Stopwatch:"); pinMode(ledPin, OUTPUT); pinMode(resetPin, INPUT); pinMode(pausePin, INPUT); Serial.begin(9600); // for logging on the PC } void loop() { Serial.print(" - Hrs: "); Serial.print(hrs); Serial.print(" - Mins: "); Serial.print(mins); Serial.print("Secs: "); Serial.print(secs); Serial.print(" --- pause state: "); Serial.print(pauseButtonState); Serial.print(" --- isPaused: "); Serial.print(isPaused); Serial.print(" --- reset state: "); Serial.println(resetButtonState); pauseButtonState = digitalRead(pausePin); if (pauseButtonState != lastPauseButtonState) { lastPauseDebounceTime = millis(); } if ((millis() - lastPauseDebounceTime) > debounceDelay) { if (pauseButtonState == HIGH) { Serial.println("******************* PAUSE ***************************"); if (isPaused == true) { // start again isPaused = false; } else { //pause now isPaused = true; } } } resetButtonState = digitalRead(resetPin); if (resetButtonState != lastResetButtonState) { lastResetDebounceTime = millis(); } if ((millis() - lastResetDebounceTime) > debounceDelay) { if (resetButtonState == HIGH) { Serial.println("^^^^^^^^^^^^^^^^^^^^^^^^ RESET ^^^^^^^^^^^^^^^^^^^^^^^^^^"); secs = 0; mins = 0; hrs = 0; isPaused = false; lcd.setCursor(1,1); lcd.print(" "); } } if (isPaused == false) { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval) { secs = secs + 1; if (secs == 60) { secs = 0; mins = mins + 1; } if (mins == 60) { mins = 0; hrs = hrs + 1; } lcd.setCursor(1,1); String theTime = String(hrs); if (mins >= 10) { theTime = theTime + ":" + String(mins); } else { theTime = theTime + ":0" + String(mins); } if (secs >= 10) { theTime = theTime + ":" + String(secs); } else { theTime = theTime + ":0" + String(secs); } lcd.print(theTime); // flash the LED for status... previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(ledPin, ledState); } delay(10); lastPauseButtonState = pauseButtonState; lastResetButtonState = resetButtonState; } }

Initial hardware setup:

Don’t connect the Arduino to power (your PC) until this is all setup. Drop the LCD onto the breadboard. I put mine over on the right side. Drop the potentiometer onto the breadboard away from the LCD. Jump wire from the 5V pin on the Arduino to the “+” rail on the breadboard. Jump wire from the GND pin on the Arduino to the “-” rail on the breadboard. Jump wire from the + rail to the left-most pin on the pot. Jump wire from the – rail to the right-most pin on the pot. Jump wire from pin “Vss” or #1 on the LCD to the – rail on the breadboard. Jump wire from pin “Vdd” or #2 on the LCD to the + rail on the breadboard. Jump wire from pin “V0” or #3 on the LCD to the middle pin on the pot. Diode from the + rail to pin “A” or #15 on the LCD. Jump wire from pin “K” or #16 to the – rail on the breadboard. Plug in the Arduino to your computer. (That’s right no sketch yet at this point) The LCD should be backlit if you did the “A” and “K” pins correctly. Adjust the pot’s knob. You should be able to turn it all the way down and then see a row of boxes on the LCD. If not, stop here and fix that. GIYF. Unplug the Arduino. Jump wire from pin “RW” on the LCD to GND on the breadboard. Jump wire from pin “RS” on the LCD to pin #12 on the Arduino. Jump wire from pin “E” on the LCD to pin #11 on the Arduino. Jump wire from pin #2 to pin “DB7” on the LCD. Jump wire from pin #3 to pin “DB6” on the LCD. Jump wire from pin #4 to pin “DB5” on the LCD. Jump wire from pin #5 to pin “DB4” on the LCD. Plug in the Arduino. Publish the sketch to your Arduino and watch it run. If everything is just right, you’ll see “Stopwatch:” and the time counting up on the LCD display. If not, pull your hair out and troubleshoot here before moving on. Curse a lot, it helps.

Add the switches to the board:

Drop the two 4-pin switches onto the breadboard, spanning the middle valley. Name one of them reset and one pause in your mind. Keep them separate. Jump wire from the reset switch on the same side as the other components to pin #8 (or whatever you like) on the breadboard. Jump wire from the pause switch on the same side as the other components to pin #22 (or whatever you like) on the breadboard. Jump wire from the reset switch on the opposite side of the breadboard from the other components to the + rail on the breadboard. Jump wire from the pause switch on the opposite side of the breadboard from the other components to the + rail on the breadboard. 10K Ohm resistor from the other corner of the reset switch to another line on the opposite side of the breadboard from the other components. 10K Ohm resistor from the other corner of the pause switch to another line on the opposite side of the breadboard from the other components. Jump wire from from the reset switch’s resistor’s second line to GND on the breadboard. Jump wire from from the pause switch’s resistor’s second line to GND on the breadboard. Whew, that’s it!

Run it!

With all of that wiring, connecting the LCD to the pot and he Arduino, and the switches wired to their own digital pins on the Arduino and to power and ground, you’re all set.

When you plug the Arduino back in, load the program if it’s not already there and voila! (Hopefully) The screen will show the stopwatch and start counting up. When you press the Reset button, the time should reset to zero. When you press the Pause button, the time should freeze, until you press Pause again.

Troubleshooting

I had trouble with the lCD not displaying the text I sent it at first. I found it was very lossly connecting to the breadboard, so I had to jiggle it and remove/replace it a few times until I could feel it going into the holes on the board properly and then it worked from there. If you have trouble, try this and then check online for other solutions. I rely on the kind folks at http://reddit.com/r/arduino for lots of help and you can too.

Conclusion

If you try this out, let me know how it goes in the comments. Let me know too if you see any really significant improvements I could make. Thanks for reading this far.

🙂