Arduino Clock

by 784465 in Circuits > Arduino

39 Views, 0 Favorites, 0 Comments

Arduino Clock

Clock.jpeg

This tutorial will walk you through the steps to build an Arduino Clock. This project requires basic programming skills and knowledge of hardware components. We will use an LCD screen to display the time and two buttons to change the hour and minute.


This project is an easy way to develop your programming and building skills with more complicated materials. By following this guide, you can make an Arduino clock customized to fit your current time.

Supplies

879-1__51603.jpg
sfe-mini-push-button-switch-2_1_1024x.webp.jpeg
Unknown.jpeg
Jumper.jpeg

The Beginning

  1. Gather all your materials
  2. Connect your resistors to your breadboard underneath the pushbuttons supplying them with negative power
  3. Connect 5k positive wire connection to pushbuttons, giving them a negative connection
  4. Grab the LCD screen, connect two jumper cables to the VSS pin on the display to the ground, and place the VDD pin to the positive

Connecting RS, RW, and EN Pins

Screenshot 2025-01-22 at 9.04.01 PM.png
  1. Connect the RS (register select) pin on the LCD to pin 12 on the Arduino
  2. Connect the RW (Read/Write) pin to the ground as it sets the LCD screen to write mode
  3. Connect the EN (enable) pin to pin 11

Connect the Data Pins

Screenshot 2025-01-22 at 9.00.16 PM.png

These pins are used for sending data from the Arduino to the LCD screen for displaying different characters or commands.

  1. Connect the D4 pin on the LCD to pin five on the Arduino
  2. Connect the D5 pin on the LCD to pin four on the Arduino
  3. Connect the D6 pin on the LCD to pin three on the Arduino
  4. Connect the D7 pin on the LCD to pin two on the Arduino

Double-check All Connections

Screenshot 2025-01-22 at 11.45.41 PM.png

Make sure to check if any components are lost or out of place, as it will not work if a wire is out of place.

Code

#include "LiquidCrystal.h"


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


int h = 12;

int m = 59;

int s = 45;

int flag = 1;


const int hourButton = 8;

const int minuteButton = 9;


void setup() {

Serial.begin(9600);

lcd.begin(16, 2); // Initialize the LCD (no backlight control needed)

pinMode(hourButton, INPUT_PULLUP);

pinMode(minuteButton, INPUT_PULLUP);

}


void loop() {

lcd.setCursor(0, 0);

lcd.print("Time ");

printTime();


lcd.setCursor(0, 1);

lcd.print("Have a nice Day");


delay(987);

s++;


manageOverflow();


if (digitalRead(hourButton) == LOW) {

adjustHour();

}

if (digitalRead(minuteButton) == LOW) {

adjustMinute();

}


Serial.print("Time: ");

printTimeSerial();

Serial.println(flag == 0 ? " AM" : " PM");

}


void printTime() {

if (h < 10) lcd.print("0");

lcd.print(h);

lcd.print(":");

if (m < 10) lcd.print("0");

lcd.print(m);

lcd.print(":");

if (s < 10) lcd.print("0");

lcd.print(s);

lcd.print(flag == 0 ? " AM" : " PM");

}


void printTimeSerial() {

if (h < 10) Serial.print("0");

Serial.print(h);

Serial.print(":");

if (m < 10) Serial.print("0");

Serial.print(m);

Serial.print(":");

if (s < 10) Serial.print("0");

Serial.print(s);

}


void manageOverflow() {

if (s == 60) {

s = 0;

m++;

}

if (m == 60) {

m = 0;

h++;

}

if (h == 13) {

h = 1;

flag = 1 - flag;

}

}


void adjustHour() {

h++;

if (h == 13) {

h = 1;

flag = 1 - flag;

}

}


void adjustMinute() {

s = 0;

m++;

if (m == 60) {

m = 0;

h++;

if (h == 13) {

h = 1;

flag = 1 - flag;

}

}

}