L48 Real Time Clock
L48 Real Time Clock
It is true that the Arduino has a build-in sense for time. It knows how much time has passed
since it started executing a program. You can use the millis() function to get a number that
represents this time in milliseconds. A similar function is micros(), and it does the same
thing as millis() except that it reports the elapsed time in microseconds instead of
milliseconds. There are also the time-related functions delay() and delaymicroseconds()
which add a delay in your program, the fist in milliseconds and the second in
microseconds.
Still, your sketch cannot ask the Arduino for the time. You could use an Internet time server
and poll it occasionally for the time, but this method requires Internet connectivity, a
dependency that maybe an overkill for some projects.
A real-time clock break out board is a PCB that contains a time-keeping integrated circuit.
It’s like the watch you wear on your wrist. You glance at it to get the time. The Arduino will
be able to ask the real-time clock for the time too.
A simple application for a real-time clock is for time-stamping log entries recorded on an
SD card. We will look at this problem in this lecture. You can also consider applications
where certain events must be scheduled, like turning an illuminated sign on and off, or
taking sensor readings at predetermined times.
1
Peter Dalmaris Lecture 47 Arduino Step by Step
2
Peter Dalmaris Lecture 47 Arduino Step by Step
Download it, copy the library folder to the Arduino libraries folder, and restart the IDE. Then,
load the sketch from File —> RTClib —> ds1307.
#include <Wire.h>
Import the Wire library so that we can use the IC2 bus.
#include "RTClib.h"
Import the RTClib library so that we can use the clock.
RTC_DS1307 rtc; Declare an RTC_DS103 object, name it “rtc”.
void setup () {
Serial.begin(9600);
Initialise the IC2 bus.
Wire.begin();
Initialise the real time clock device.
rtc.begin();
if (! rtc.isrunning()) {
This will get the time from your
Serial.println("RTC is NOT running!");
computer during compilation
rtc.adjust(DateTime(2014,01,16,14,45,00));
and automatically set the time.
// following line sets the RTC to the date & time this sketch was compiled
//RTC.adjust(DateTime(__DATE__, __TIME__));
}
This will adjust the time with the
} help of the C-language DateTime Check to determine is the device is on. Since the
object initialiser. device is powered by a battery when the Arduino is
off, it will continue to keep track of time and be in a
void loop () {
“running” state. As long as a time has been set and
DateTime now = rtc.now();
the battery can power it, the “rtc.isrunning()”
Serial.print(now.year(), DEC);
function will return true. The only time you will
Serial.print('/');
probably see a “false” returned is when you run this
sketch for the very first time.
Serial.print(now.month(), DEC);
Serial.print('/');
Get the time now, and store it in the “now” variable,
Serial.print(now.day(), DEC);
which is of time DateTime.
Serial.print(' ');
Print time attributes to the Serial port.
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Get the number of seconds elapsed since 1/1/1970,
Serial.print("s = ");
when Unix time begins.
Serial.print(now.unixtime() / 86400L);
… and convert that to days, since 1 day
Serial.println("d");
contains 86400 seconds.
3
Peter Dalmaris Lecture 47 Arduino Step by Step
Once the clock has been set, it will keep the time independently of the Arduino,
compensating for leap years and the like until its onboard battery fails. All you have to do in
order to get a reading of the time is to call the now() function, which returns a DateTime
object.
!
Results of time/date calculations
4
Peter Dalmaris Lecture 47 Arduino Step by Step
In the next page, I provide the merged sketch, and comments to the interesting segments.
5
Peter Dalmaris Lecture 47 Arduino Step by Step
RTC_DS1307 rtc;
Import the RTClib library so that we can use the
const int chipSelect = 10; clock breakout library.
void loop(){
DateTime now = rtc.now();
Get the sensor readings.
String dataString = "";
for (int analogPin = 0; analogPin < 2; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 1) {
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
print_time(now, dataFile);
Get a handle to the datalog file.
dataFile.println(dataString);
Call the print_time function which will
dataFile.close();
produce the timestamp. Pass a reference
Serial.println(dataString);
to the now object that contains the
}
current time, and the datafile object
which contains a handle to the data file.
else {
Serial.println("error opening datalog.txt”)
}
Print the sensor readings to the data file.
delay(3000);
}
6
Peter Dalmaris Lecture 47 Arduino Step by Step
There is nothing new in this sketch, just a recombination of known elements. You now have
a way to create a data-logger, which can work in remote locations, on a battery which can
be charged by a small solar panel.
An exercise
With the right hardware, keeping time is easy. Try out this exercise:
!
Create an LCD clock. Use a real time clock like the one in this lecture, combine it with an LCD
screen, and display the date and time on it.
!
For the adventurous, here’s another exercise: Extend the LCD clock so that it has these features:
!
1. A toggle switch
3. A buzzer
4. Only keeps track of time (don’t worry about the date for now)
!
The toggle switch will set the mode of the clock to Time Set or Alarm set. When the toggle switch is
on, then turning the potentiometer will change the time. When the switch is off, then turning the
potentiometer will change the alarm time.
!
Assuming the clock keeps time in 24 hour format, make your gadget so that when the alarm time is
reached, a sound will be generated by the buzzer for 10 seconds.