Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Real-Time Clock with I2C LCD Display and Adjustable Time Buttons

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Real-Time Clock with I2C LCD Display and Adjustable Time Buttons

Circuit Documentation

Summary

This circuit is designed to interface an Arduino UNO with a DS1307 Real-Time Clock (RTC) module and a 16x2 I2C LCD display. It also includes three pushbuttons for adjusting the time (hours, minutes, and seconds). The circuit allows the user to view the current time on the LCD and adjust it using the pushbuttons.

Component List

Resistor

DS1307 RTC (Wokwi Compatible)

16x2 I2C LCD

Pushbutton

Arduino UNO

Wiring Details

Resistor

DS1307 RTC (Wokwi Compatible)

16x2 I2C LCD

Pushbutton

Arduino UNO

Code Documentation

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

// Initialize the RTC and LCD
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if necessary

const int buttonHourPin = 2; // Pin where the hour button is connected
const int buttonMinutePin = 3; // Pin where the minute button is connected
const int buttonSecondPin = 4; // Pin where the second button is connected

unsigned long previousMillis = 0; // Stores the last time the display was updated
const long interval = 1000; // Interval at which to update the display (1 second)

void setup() {
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  pinMode(11, OUTPUT);
  digitalWrite(11, HIGH);

  // Initialize serial communication for debugging
  Serial.begin(9600);

  // Initialize the RTC
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // Uncomment the following line to set the RTC to the current date & time
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  // Initialize the LCD
  lcd.init();
  lcd.backlight();
  lcd.clear();

  // Initialize the button pins as input with internal pull-up resistor
  pinMode(buttonHourPin, INPUT_PULLUP);
  pinMode(buttonMinutePin, INPUT_PULLUP);
  pinMode(buttonSecondPin, INPUT_PULLUP);
}

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

  // Check if the hour button was pressed
  if (digitalRead(buttonHourPin) == LOW) {
    // Get the current time from the RTC
    DateTime now = rtc.now();

    // Increment the hours
    DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour() + 1, now.minute(), now.second());
    rtc.adjust(newTime);

    newTime = rtc.now();

    // Update the display immediately
    updateDisplay(newTime);

    // Debounce delay
    delay(200);
  }

  // Check if the minute button was pressed
  if (digitalRead(buttonMinutePin) == LOW) {
    // Get the current time from the RTC
    DateTime now = rtc.now();

    // Increment the minutes
    DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute() + 1, now.second());
    rtc.adjust(newTime);

    newTime = rtc.now();

    // Update the display immediately
    updateDisplay(newTime);

    // Debounce delay
    delay(200);
  }

  // Check if the second button was pressed
  if (digitalRead(buttonSecondPin) == LOW) {
    // Get the current time from the RTC
    DateTime now = rtc.now();

    // Increment the seconds
    DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second() + 1);
    rtc.adjust(newTime);

    newTime = rtc.now();

    // Update the display immediately
    updateDisplay(newTime);

    // Debounce delay
    delay(200);
  }

  // Check if it's time to update the display
  if (currentMillis - previousMillis >= interval) {
    previous