Arduino Clock
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
The Beginning
- Gather all your materials
- Connect your resistors to your breadboard underneath the pushbuttons supplying them with negative power
- Connect 5k positive wire connection to pushbuttons, giving them a negative connection
- 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
- Connect the RS (register select) pin on the LCD to pin 12 on the Arduino
- Connect the RW (Read/Write) pin to the ground as it sets the LCD screen to write mode
- Connect the EN (enable) pin to pin 11
Connect the Data Pins
These pins are used for sending data from the Arduino to the LCD screen for displaying different characters or commands.
- Connect the D4 pin on the LCD to pin five on the Arduino
- Connect the D5 pin on the LCD to pin four on the Arduino
- Connect the D6 pin on the LCD to pin three on the Arduino
- Connect the D7 pin on the LCD to pin two on the Arduino
Double-check All Connections
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;
}
}
}