Arduino RTC Time Setting with Pushbutton & LCD Display | Debounce Code Tutorial

 

Arduino RTC Time Setting with Pushbutton & LCD Display | Debounce Code Tutorial







Here is an example Arduino code that sets the time in an RTC (such as the DS3231) using a pushbutton with debouncing, and displays the current time on a 16x2 LCD. The code uses the RTClib library for handling the RTC module and the LiquidCrystal library for displaying the time on the LCD. The button allows you to cycle through setting the hours, minutes, and seconds.

Components:

  • RTC Module (DS3231): Used for keeping track of time.
  • 16x2 LCD: Used to display the current time and time-setting menu.
  • Pushbutton: Used to cycle through hours, minutes, and seconds for setting the RTC.

Circuit:

  • RTC Module (DS3231):
    • VCC → 5V
    • GND → GND
    • SDA → A4
    • SCL → A5
  • 16x2 LCD (with I2C interface):
    • VCC → 5V
    • GND → GND
    • SDA → A4
    • SCL → A5
  • Pushbutton:
    • One pin → Pin 2
    • Other pin → GND

Arduino Code:

//////////////////////////////////////////////////////////////////

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

RTC_DS3231 rtc;  // Instantiate RTC object
LiquidCrystal_I2C lcd(0x27, 16, 2);  // LCD with I2C address 0x27, 16x2 size

const int buttonPin = 2;  // Pin for the push button
int buttonState = 0;      // Current state of the button
int lastButtonState = 0;  // Previous state of the button
unsigned long lastDebounceTime = 0;  // Last time the button state was changed
unsigned long debounceDelay = 50;    // Debounce delay in milliseconds

int timeSetState = 0;  // 0 = hour, 1 = minute, 2 = second

void setup() {
  Serial.begin(9600);
 
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting the time...");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));  // Set RTC to compile time if lost power
  }

  pinMode(buttonPin, INPUT_PULLUP);  // Set pushbutton pin to input with internal pull-up
  lcd.begin(16, 2);                  // Initialize the LCD
  lcd.print("Setting RTC...");
  delay(2000);
  lcd.clear();
}

void loop() {
  int reading = digitalRead(buttonPin);

  // Check if button state has changed (debounced)
  if (reading != lastButtonState) {
    lastDebounceTime = millis();  // Reset debounce timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
     
      if (buttonState == LOW) {  // Button pressed (active low)
        updateTime();
      }
    }
  }

  lastButtonState = reading;
 
  // Display current time on the LCD
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  if (now.minute() < 10) lcd.print("0");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  if (now.second() < 10) lcd.print("0");
  lcd.print(now.second(), DEC);

  delay(1000);  // Update every second
}

void updateTime() {
  DateTime now = rtc.now();

  if (timeSetState == 0) {
    // Set the hour
    int newHour = (now.hour() + 1) % 24;  // Increment hour
    rtc.adjust(DateTime(now.year(), now.month(), now.day(), newHour, now.minute(), now.second()));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Hour set to: ");
    lcd.print(newHour);
    delay(1000);  // Show for 1 second
  } else if (timeSetState == 1) {
    // Set the minute
    int newMinute = (now.minute() + 1) % 60;  // Increment minute
    rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), newMinute, now.second()));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Minute set to: ");
    lcd.print(newMinute);
    delay(1000);  // Show for 1 second
  } else if (timeSetState == 2) {
    // Set the second
    int newSecond = (now.second() + 1) % 60;  // Increment second
    rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), newSecond));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Second set to: ");
    lcd.print(newSecond);
    delay(1000);  // Show for 1 second
  }

  // Move to the next part of time to set (hour -> minute -> second)
  timeSetState = (timeSetState + 1) % 3;
}

////////////////////////////////////////////////////////////////


Key Concepts:

  1. RTC Setup:

    • The RTC is initialized using the RTClib library. If the RTC module has lost power, it will automatically set the time to the compile time of the sketch.
  2. Debouncing the Push Button:

    • The pushbutton is debounced using the debounceDelay to prevent multiple reads from one press.
    • The button cycles through setting hours, minutes, and seconds. Every time the button is pressed, it increments the corresponding part of the time.
  3. LCD Display:

    • A 16x2 LCD is used to display the current time. It updates every second to show the current time in the format HH:MM:SS.
    • When adjusting the time, the LCD will show the updated time part (hour, minute, or second).
  4. Time Update:

    • The updateTime() function checks the timeSetState to determine which part of the time (hour, minute, or second) should be updated.
    • Each time you press the button, it increments the hour, minute, or second by one.

Hardware Connections:

  • RTC Module (DS3231):
    • VCC → 5V
    • GND → GND
    • SDA → A4
    • SCL → A5
  • 16x2 LCD (I2C):
    • VCC → 5V
    • GND → GND
    • SDA → A4
    • SCL → A5
  • Push Button:
    • One side → Pin 2
    • Other side → GND

This code will allow you to incrementally adjust the hour, minute, or second by pressing the button. The LCD will display the current time and provide feedback when you adjust the time.


Follow Manmohan Pal for more tech tutorials! 1) How to Install Arduino IDE & Drivers + Upload Your First Code | Beginner's Guide by Manmohan Pal video Link: https://youtu.be/HzhfrlgtWr8 Blog: https://arduinobymanmohan.blogspot.com/p/how-to-install-arduino-ide-drivers.html 2) How to Add LCD to Arduino Project: Connections, Libraries, and Code Explained by Manmohan Pal Video link: https://youtu.be/3D1QUHjCdeY Blog: https://arduinobymanmohan.blogspot.com/p/how-to-add-libraries-and-lcd-to-arduino.html 3) How to Add RTC to Arduino Project | Step-by-Step Tutorial by Manmohan Pal Video link: https://youtu.be/2Bf92OhbAL0 Blog: https://arduinobymanmohan.blogspot.com/p/how-to-add-rtc-to-arduino-project-step.html 4) How to Add Pushbuttons to Your Arduino Projects by Manmohan Pal Video Link: https://youtu.be/gqWiHdVMx_o https://arduinobymanmohan.blogspot.com/p/how-to-add-pushbuttons-to-your-arduino.html 5) Arduino RTC Time Setting with Pushbutton & LCD Display | Debounce Code Tutorial Video Link: https://youtu.be/Xh1mgZ3ySUg https://arduinobymanmohan.blogspot.com/p/set-time-in-rtc-using-push-button.html


No comments:

Post a Comment

Wifi Home automation Diy kit

 Wifi Home automation Diy kit By Manmohan Pal Wifi Home Automation Kit Hi, This is Manmohan Pal, I am glad to present a DIY kit for Home Aut...