Real Time Clock DS1307
Real Time Clock DS1307
In this sketch, we will make a digital clock to display the real time(DS1307) and
temperature(LM35) in Serial monitor.
Hardware Required
Arduino
LM35
DS1307
Jump wire
Breadboard
Theory
DS1307 RTC Module
The DS1307 can keep track of seconds, minutes, hours, days, dates, months, and
years. It can work in either a 12-hour or 24-hour format and has an AM/PM indicator.
For months with fewer than 31 days, it automatically adjusts the date at the end of the
month, including leap year corrections (valid up to 2100).
LM35
The LM35 outputs the voltage linearly proportional to the Centigrade temperature.
The output scale factor of the LM35 is 10 mV/°C. It means that the temperature is
calculated by dividing the voltage (mV) in output pin by 10.
Arduino DS1307
Arduino LM35
5V VCC
5V VCC
GND GND
A0 OUT
A4 SDA
GND GND
A5 SCL
Code
// Date and time functions using a DS1307 RTC connected via I2C and
Wire lib
#include <RTClib.h>
// UNCHANGABLE PARAMATERS
#define SUNDAY 0
#define MONDAY 1
#define TUESDAY 2
#define WEDNESDAY 3
#define THURSDAY 4
#define FRIDAY 5
#define SATURDAY 6
// event on Monday, from 13:50 to 14:10
uint8_t WEEKLY_EVENT_DAY = MONDAY;
uint8_t WEEKLY_EVENT_START_HH = 13; // event start time: hour
uint8_t WEEKLY_EVENT_START_MM = 50; // event start time: minute
uint8_t WEEKLY_EVENT_END_HH = 14; // event end time: hour
uint8_t WEEKLY_EVENT_END_MM = 10; // event end time: minute
const int sensorPin = A0;
float sensorValue;
float voltageOut;
float temperatureC;
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void setup () {
Serial.begin(9600);
// sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// sets the RTC with an explicit date & time, for example to set
// January 21, 2021 at 3am you would call:
// rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
void loop () {
sensorValue = analogRead(sensorPin);
voltageOut = (sensorValue * 5000) / 1024;
printTime(now);
}
void printTime(DateTime time) {
Serial.print("TIME: ");
Serial.print(time.year(), DEC);
Serial.print('/');
Serial.print(time.month(), DEC);
Serial.print('/');
Serial.print(time.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[time.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(time.hour(), DEC);
Serial.print(':');
Serial.print(time.minute(), DEC);
Serial.print(':');
Serial.println(time.second(), DEC);
Serial.print("temperature: ");
Serial.println(temperatureC);
delay(5000);
}
Demonstrations
After upload the Arduino, open the serial monitor to see the result.
If the serial monitor print “Couldn’t find RTC”, check if the SDL pin and SCL pin
is connected to right position or not?