Arduino Clock with 16×2 LCD and Rotary Encoder without RTC

Here a simple arduino code for clock function without using RTC. If you only want to display the time and not the date, you do not need an RTC. The built-in crystal oscillator of the arduino is accurate enough for time displaying function.

Schematic Diagram

This is simple to build.

Code

[code lang=”c”]
// 1602 clock by tataylino.com
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int x = 0;
int minute = 1;
int hour = 12;
int sec = 0;
int am = 0; //1 = morning
bool up = false;
bool down = false;
bool display_update = true;
bool amtopm = false;

int backled = 9;
int button = 13;
int val = 0;
int aState;
int aLastState;

void setup() {
pinMode(button, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
// pinMode(13, OUTPUT);
pinMode(backled, OUTPUT);
analogWrite(backled, 255);
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(“Orasan”);

//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) – 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
}
void display() {
if(hour >= 10) {
lcd.setCursor(0, 2);
lcd.print(hour);
} else {
lcd.setCursor(0, 2);
lcd.print(“0”);
lcd.setCursor(1, 2);
lcd.print(hour);
}

lcd.setCursor(2, 2);
lcd.print(“:”);

if(minute >= 10) {
lcd.setCursor(3, 2);
lcd.print(minute);
} else {
lcd.setCursor(3, 2);
lcd.print(“0”);
lcd.setCursor(4, 2);
lcd.print(minute);
}

lcd.setCursor(5, 2);
lcd.print(“:”);
if(sec >= 10){
lcd.setCursor(6, 2);
lcd.print(sec);
} else {
lcd.setCursor(6, 2);
lcd.print(“0”);
lcd.setCursor(7, 2);
lcd.print(sec);
}
if(am == 1){
lcd.setCursor(8, 2);
lcd.print(“am”);
} else {
lcd.setCursor(8, 2);
lcd.print(“pm”);
}
}

void counting_time(){
if(minute < 0 ) {
minute = 59;
hour–;
}
if(hour < 1){ hour = 12; } if (sec >= 60) {
sec = 0;
minute++;
}
if (minute >= 60) {
hour++;
minute = 0;
}
if (hour >=13) {
hour = 1;
}
if(hour == 12 && minute == 0) { // to make sure only 1 am to pm transition
if(amtopm == true) {
amtopm = false;
if(am == 1){
am = 0;
} else {
am = 1;
}
}
}else{
amtopm = true;
}
}

ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz
sec++;
counting_time();
if(display_update) {
display();
}
}

void loop() {
val = digitalRead(button); // read button
if (val == HIGH) {
//lcd.setCursor(0, 0);
//lcd.print(“o”);
//minute++;
//delay(500);
}
aState = digitalRead(A3);
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(A2) != aState) {
display_update = false;
minute ++;
counting_time();
if(minute >= 10) {
lcd.setCursor(3, 2);
lcd.print(minute);
} else {
lcd.setCursor(3, 2);
lcd.print(“0”);
lcd.setCursor(4, 2);
lcd.print(minute);
}
display_update = true;
} else {
display_update = false;
minute –;
counting_time();
if(minute >= 10) {
lcd.setCursor(3, 2);
lcd.print(minute);
} else {
lcd.setCursor(3, 2);
lcd.print(“0”);
lcd.setCursor(4, 2);
lcd.print(minute);
}
display_update = true;
}
}
aLastState = aState; //
}

[/code]

 

 

 

One thought on “Arduino Clock with 16×2 LCD and Rotary Encoder without RTC”

Comments are closed.