Arduino Real Time Clock!
I recently got an Arduino starter kit and have been trying out all kinds of projects. I thought about building a clock and here it is!
Supplies
- Uno R3
- 16 pin LCD
- Large breadboard
- Buzzer (doesn't really matter what kind as long as it buzzes)
- DS1307 module (the time keeper)
- Potentiometer
- Jumper wires
- 2 220Ω resistors
Wiring
LCD wiring is in pics 2 and 3
The Potentiometer wiring, middle pin- VO on LCD, Left pin- 5V, Right pin- GND
Buzzer wiring, + (through 220Ω resistor) D6 (on Uno), - GND
DS1307 wiring, VCC- 5V, GND- GND, SDA- A4 (on Uno), SCL- A5 (on Uno)
Note: The small breadboard in the pic is supposed to be the DS1307
Install Libraries
I used the Arduino IDE to upload my code. You will have to install the RTClib by Adafruit for this to work, When your IDE is open you should see on the top left of your screen some words. They should say (in this order) File, Edit, Sketch, Tools, Help. Click on "Sketch", then go down to "Include libraries", you should see more things pop up. Click on "Manage libraries", that should bring you to a search bar in the IDE. Type, RTClib by Adafruit and tap "install". Now you know how to install libraries!
Upload Code
Here is the code,
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
RTC_DS1307 rtc;
// LCD pins: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int buzzerPin = 6;
int lastChimedHour = -1;
void setup() {
Wire.begin();
rtc.begin();
lcd.begin(16, 2);
pinMode(buzzerPin, OUTPUT);
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.print("Clock Starting");
delay(2000);
lcd.clear();
}
void loop() {
DateTime now = rtc.now();
// ---- DISPLAY TIME ----
lcd.setCursor(0, 0);
if (now.hour() < 10) lcd.print('0');
lcd.print(now.hour());
lcd.print(':');
if (now.minute() < 10) lcd.print('0');
lcd.print(now.minute());
lcd.print(':');
if (now.second() < 10) lcd.print('0');
lcd.print(now.second());
// ---- DISPLAY DATE ----
lcd.setCursor(0, 1);
if (now.month() < 10) lcd.print('0');
lcd.print(now.month());
lcd.print('/');
if (now.day() < 10) lcd.print('0');
lcd.print(now.day());
lcd.print('/');
lcd.print(now.year());
// ---- HOURLY CHIME ----
if (now.minute() == 0 && now.second() == 0 && now.hour() != lastChimedHour) {
lastChimedHour = now.hour();
int hour12 = now.hour() % 12;
if (hour12 == 0) hour12 = 12;
chime(hour12);
}
delay(200);
}
// Function to beep N times
void chime(int count) {
for (int i = 0; i < count; i++) {
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(700);
}
}
Have fun! you can add an alarm, buttons to adjust the alarm, ect