A_2: Arduino-based Digital Stopwatch with Lap Timing and Data
Storage
Experiment 1: Interfacing an I2C LCD with Arduino
Introduction
In this lab, you will learn how to interface an I2C (I²C or Inter-Integrated Circuit) LCD display
with an Arduino microcontroller. LCD displays are widely used for visualizing data and messages
in various electronic projects. Interfacing an I2C LCD with Arduino simplifies the connection and
reduces the number of pins required, making it an efficient choice for your projects.
Objectives
Understand the basics of LCD displays and their importance in microcontroller projects.
Learn how to interface an I2C LCD display with an Arduino.
Write and upload Arduino code to display text and custom characters on the LCD screen.
Materials
Arduino Uno or similar board.
I2C LCD module (e.g., 16x2 or 20x4 LCD with I2C backpack).
Jumper wires.
Breadboard (optional).
Procedure
Step 1: Setting Up the Hardware
1. Connect the I2C LCD module to the Arduino:
Connect the VCC (power) and GND (ground) pins on the LCD module to the 5V
and GND pins on the Arduino, respectively.
Connect the SDA (data) and SCL (clock) pins on the LCD module to the
corresponding pins on the Arduino. On most Arduino boards, SDA is A4, and SCL
is A5.
2. Verify your connections carefully to ensure they are correct.
Step 2: Installing Libraries
1. Open the Arduino IDE
2. Install the LiquidCrystal I2C library
To easily control the I2C LCD, you will need to install the “LiquidCrystal I2C” library.
Open the Arduino IDE.
Go to “Sketch” -> “Include Library” -> “Manage Libraries...”
In the Library Manager, type “LiquidCrystal I2C” in the search bar.
Click "Install" to install the library.
Step 3: Writing the Arduino Code
1. Open a new Arduino sketch
Open the Arduino IDE.
Click "File" -> "New" to create a new sketch.
2. Write the code to initialize and display text on the LCD
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (0x27 for a 16x2 display)
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight (if available)
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Hello, World!");
void loop() {
// Your code goes here
}
Make sure to modify the LiquidCrystal_I2C constructor with the correct address
(0x27 is common, but it may vary depending on your module).
2. Upload the code to the Arduino
Connect your Arduino to your computer using a USB cable.
Select the correct Arduino board and port in the "Tools" menu.
Click the "Upload" button (arrow icon) to upload the code to the Arduino.
Step 4: Testing
1. After uploading the code, you should see the text “Hello, World!” displayed on your
LCD.
2. Experiment with the LCD functions and display your own messages and data.
Conclusion
In this lab, you successfully interfaced an I2C LCD display with an Arduino and displayed text
on the screen. You also learned how to install libraries and write Arduino code for controlling the
LCD. LCD displays are versatile and can be used to enhance various Arduino projects by
visualizing information effectively.
Experiment 2: Designing an Automatic Stopwatch with Arduino and I2C LCD
Introduction
In this lab, you will learn how to design and build an automatic stopwatch using an Arduino
microcontroller and an I2C (I²C or Inter-Integrated Circuit) LCD display. A stopwatch is a valuable
tool in various applications, from sports timing to industrial processes. In this project, you will
create a digital stopwatch that starts, stops, and resets automatically.
Learning Objectives
Understand the principles of digital timing and event tracking.
Interface an I2C LCD display with an Arduino.
Write Arduino code to implement a digital stopwatch with start, stop, and reset functions.
Materials
Arduino Uno or similar board.
I2C LCD module (e.g., 16x2 or 20x4 LCD with I2C backpack).
Push-button switches (at least two).
10kΩ resistors (2).
Jumper wires.
Breadboard (optional).
Procedure
Step 1: Setting Up the Hardware
1. Connect the I2C LCD module to the Arduino:
Connect the VCC (power) and GND (ground) pins on the LCD module to the 5V
and GND pins on the Arduino, respectively.
Connect the SDA (data) and SCL (clock) pins on the LCD module to the
corresponding pins on the Arduino. On most Arduino boards, SDA is A4, and SCL
is A5.
2. Connect the push-button switches:
Connect one terminal of each push-button to a digital pin on the Arduino (e.g., Pin
2 for Start/Stop and Pin 3 for Reset).
Connect the other terminal of each push-button to the ground (GND) through a
10kΩ resistor.
Step 3: Writing the Arduino Code
1. Open a new Arduino sketch:
Open the Arduino IDE.
Click “File” -> “New” to create a new sketch.
2. Write the code for the automatic stopwatch:
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (0x27 for a 16x2 display)
const int startStopButtonPin = 2; // Pin for the Start/Stop button
const int resetButtonPin = 3; // Pin for the Reset button
unsigned long startTime = 0; // Variable to store the start time
unsigned long elapsedTime = 0; // Variable to store the elapsed time
boolean timing = false; // Flag to indicate if timing is active
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight (if available)
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print(“Press Start”);
pinMode(startStopButtonPin, INPUT_PULLUP); // Set the Start/Stop button pin as input
pinMode(resetButtonPin, INPUT_PULLUP); // Set the Reset button pin as input
void loop() {
// Check if the Start/Stop button is pressed
if (digitalRead(startStopButtonPin) == LOW) {
if (!timing) {
startTime = millis(); // Start timing
timing = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timing...");
} else {
elapsedTime += millis() - startTime; // Stop timing
timing = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
delay(100); // Debounce
// Check if the Reset button is pressed
if (digitalRead(resetButtonPin) == LOW) {
elapsedTime = 0; // Reset the elapsed time
timing = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
delay(100); // Debounce
// Display the elapsed time on the LCD
lcd.setCursor(0, 1);
lcd.print(“Time: “);
lcd.print(elapsedTime / 1000); // Convert milliseconds to seconds
lcd.print(“s”);
}
1. Upload the code to the Arduino:
Connect your Arduino to your computer using a USB cable.
Select the correct Arduino board and port in the "Tools" menu.
Click the "Upload" button (arrow icon) to upload the code to the Arduino.
Step 4: Testing
1. After uploading the code, press the Start/Stop button to start and stop the stopwatch.
2. Press the Reset button to reset the stopwatch to zero.
3. Observe the elapsed time displayed on the LCD screen.
4. Experiment with the stopwatch and explore how it behaves when you start, stop, and
reset it.
Conclusion
In this lab, you successfully designed an automatic stopwatch using an Arduino and an I2C LCD
display. You learned how to interface push-button switches, control timing, and display
information on the LCD. Digital stopwatches are useful in various applications, including timing
experiments, races, and other time-sensitive tasks.
Experiment 3: Designing an Automatic Stopwatch with Lap Timing and Data Storage
Introduction
In this lab, you will learn how to design and build an automatic stopwatch using an Arduino
microcontroller and an I2C (I²C or Inter-Integrated Circuit) LCD display. This stopwatch will have
additional features such as lap timing and the ability to store recorded lap times even after the
Arduino is reset or powered off. Lap timing is useful for tracking multiple time intervals, and data
storage ensures that recorded times are not lost.
Learning Objectives
Understand the principles of digital timing, lap timing, and data storage.
Interface an I2C LCD display with an Arduino.
Write Arduino code to implement a digital stopwatch with start, stop, reset, lap timing, and
data storage functions.
Materials
Arduino Uno or similar board.
I2C LCD module (e.g., 16x2 or 20x4 LCD with I2C backpack).
Push-button switches (at least three).
10kΩ resistors (3).
Jumper wires.
Breadboard (optional).
Procedure
Step 1: Setting Up the Hardware
1. Connect the I2C LCD module to the Arduino:
Connect the VCC (power) and GND (ground) pins on the LCD module to the 5V
and GND pins on the Arduino, respectively.
Connect the SDA (data) and SCL (clock) pins on the LCD module to the
corresponding pins on the Arduino. On most Arduino boards, SDA is A4, and SCL
is A5.
2. Connect the push-button switches:
Connect one terminal of each push-button to a digital pin on the Arduino (e.g., Pin
2 for Start/Stop, Pin 3 for Reset, and Pin 4 for Lap).
Connect the other terminal of each push-button to the ground (GND) through a
10kΩ resistor.
Step 2: Installing Libraries
1. Open the Arduino IDE:
If you haven’t installed the Arduino IDE, you can download it from the official Arduino website
(https://www.arduino.cc/en/software).
2. Install the LiquidCrystal I2C library and EEPROM library:
If you haven’t already installed these libraries (used in the previous lab (experiment)), follow
the same steps mentioned in the previous lab manual to install the “LiquidCrystal I2C” and
“EEPROM” libraries.
Step 3: Writing the Arduino Code
1. Open a new Arduino sketch:
Open the Arduino IDE.
Click “File” -> “New” to create a new sketch.
2. Write the code for the automatic stopwatch with lap timing and data storage:
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal I2C library
#include <EEPROM.h> // Include the EEPROM library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (0x27 for a 16x2 display)
const int startStopButtonPin = 2; // Pin for the Start/Stop button
const int resetButtonPin = 3; // Pin for the Reset button
const int lapButtonPin = 4; // Pin for the Lap button
unsigned long startTime = 0; // Variable to store the start time
unsigned long elapsedTime = 0; // Variable to store the elapsed time
boolean timing = false; // Flag to indicate if timing is active
unsigned long lapTimes[10]; // Array to store lap times (adjust the size as needed)
int lapCount = 0; // Variable to keep track of lap count
int eepromAddress = 0; // Start writing to EEPROM address 0
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight (if available)
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print(“Press Start”);
pinMode(startStopButtonPin, INPUT_PULLUP); // Set the Start/Stop button pin as input
pinMode(resetButtonPin, INPUT_PULLUP); // Set the Reset button pin as input
pinMode(lapButtonPin, INPUT_PULLUP); // Set the Lap button pin as input
// Load lap times from EEPROM
for (int i = 0; i < 10; i++) { // Adjust the loop based on the number of laps you want to store
lapTimes[i] = EEPROMReadInt(eepromAddress);
eepromAddress += sizeof(int);
void loop() {
// Check if the Start/Stop button is pressed
if (digitalRead(startStopButtonPin) == LOW) {
if (!timing) {
startTime = millis(); // Start timing
timing = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Timing...”);
} else {
elapsedTime += millis() - startTime; // Stop timing
timing = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
delay(100); // Debounce
// Check if the Lap button is pressed
if (digitalRead(lapButtonPin) == LOW && timing) {
// Record the lap time
lapTimes[lapCount] = millis() - startTime;
// Write the lap time to EEPROM
EEPROMWriteInt(eepromAddress, lapTimes[lapCount]);
eepromAddress += sizeof(int);
lapCount++;
// Display the lap time on the LCD
lcd.setCursor(0, 2 + lapCount); // Adjust the row based on your LCD size
lcd.print(“Lap”);
lcd.print(lapCount);
lcd.print(“:”);
lcd.print(lapTimes[lapCount - 1] / 1000); // Convert milliseconds to seconds
lcd.print(“s”);
delay(100); // Debounce
}
// Check if the Reset button is pressed
if (digitalRead(resetButtonPin) == LOW) {
// Reset lap times and clear EEPROM
lapCount = 0;
for (int i = 0; i < eepromAddress; i++) {
EEPROM.write(i, 0);
eepromAddress = 0;
// Reset timing
elapsedTime = 0;
timing = false;
// Clear the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
delay(100); // Debounce
// Display the elapsed time on the LCD
lcd.setCursor(0, 1);
lcd.print(“Time: ”);
lcd.print(elapsedTime / 1000); // Convert milliseconds to seconds
lcd.print(“s”);
}
// Custom function to read an integer from EEPROM
int EEPROMReadInt(int address) {
byte lowByte = EEPROM.read(address);
byte highByte = EEPROM.read(address + 1);
return (highByte << 8) | lowByte;
// Custom function to write an integer to EEPROM
void EEPROMWriteInt(int address, int value) {
byte lowByte = (byte)(value & 0xFF);
byte highByte = (byte)((value >> 8) & 0xFF);
EEPROM.write(address, lowByte);
EEPROM.write(address + 1, highByte);
1. Upload the code to the Arduino:
Connect your Arduino to your computer using a USB cable.
Select the correct Arduino board and port in the "Tools" menu.
Click the "Upload" button (arrow icon) to upload the code to the Arduino.
Step 4: Testing
1. After uploading the code, press the Start/Stop button to start and stop the stopwatch.
2. Press the Lap button to record lap times. The lap times should be displayed on the
LCD.
3. Press the Reset button to reset the stopwatch to zero and clear the recorded lap times.
4. Observe that the recorded lap times are stored in EEPROM and are retained even
after resetting the Arduino.
5. Experiment with the stopwatch and explore how it behaves when you start, stop,
reset, and record lap times.
Conclusion
In this lab, you successfully designed an automatic stopwatch with lap timing and data storage
using an Arduino and an I2C LCD display. This enhanced stopwatch is useful for tracking multiple
time intervals and ensures that recorded times are not lost even after power cycles. These features
make it versatile for various timing applications, including sports, experiments, and data
collection.