CIPAD: Comment réaliser un chronomètre & un minuteur Cyclique avec Affichage LCD I2C 2004

3 min read 1 hour ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to create a stopwatch and a cyclic timer using an Arduino and an I2C 2004 LCD display. This project is perfect for beginners looking to enhance their programming skills and understand how to work with timers and displays in Arduino.

Step 1: Gather Your Materials

Before diving into the coding and assembly, make sure you have the following components ready:

  • Arduino board (e.g., Arduino Uno)
  • I2C 2004 LCD display
  • Jumper wires
  • Breadboard (optional)
  • A computer with Arduino IDE installed
  • Access to the Wokwi project link for reference: Wokwi Project

Step 2: Set Up the Hardware

Follow these steps to connect your LCD display to the Arduino:

  1. Connect the I2C Display:

    • VCC pin on the LCD to 5V pin on the Arduino.
    • GND pin on the LCD to GND pin on the Arduino.
    • SDA pin on the LCD to A4 pin on the Arduino.
    • SCL pin on the LCD to A5 pin on the Arduino.
  2. Check Connections:

    • Ensure all connections are secure. Loose wires can cause issues with the display.

Step 3: Install Required Libraries

To control the LCD display, you need to install the appropriate libraries in the Arduino IDE:

  1. Open the Arduino IDE.
  2. Navigate to Sketch > Include Library > Manage Libraries.
  3. Search for "LiquidCrystal I2C" and install it.

Step 4: Write the Code

Now it's time to write the code for the stopwatch and cyclic timer. Start with the following code snippet:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust I2C address if necessary

unsigned long previousMillis = 0;
int seconds = 0;

void setup() {
  lcd.begin();
  lcd.backlight();
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 1000) { // Check if 1 second has passed
    previousMillis = currentMillis;
    seconds++;
    // Display time
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Time: ");
    lcd.print(seconds);
    lcd.print(" s");
  }
}

Practical Tips

  • Adjust the I2C address in the code if your LCD does not display correctly.
  • Use lcd.clear() cautiously; frequent clearing can lead to flickering.

Step 5: Upload the Code

  1. Connect your Arduino board to your computer using a USB cable.
  2. In the Arduino IDE, select the correct board and COM port.
  3. Click on the Upload button to transfer the code to the Arduino.

Step 6: Test Your Stopwatch

Once the code is uploaded:

  1. Open the Serial Monitor (set to 9600 baud) to see debug information, if added.
  2. Observe the LCD display; it should show the elapsed time in seconds.

Step 7: Implement the Cyclic Timer Feature

To implement a cyclic timer, modify the existing code to include timer settings. You can create functions to start, stop, and reset the timer.

Example Code for Cyclic Timer

void startTimer(int duration) {
  // Timer logic here
}

void resetTimer() {
  seconds = 0;
}

Conclusion

You've successfully created a stopwatch and cyclic timer using an Arduino and an I2C LCD display. This project not only enhances your programming skills but also gives you practical experience with hardware components.

Next steps could include customizing the timer's duration or adding buttons for user input. Experiment with the code to make the project your own!