Programming RTC

Hello. I am programming my DS1307 with my Arduino. I have used the following source code:

// Written by John Boxall from

#include “Wire.h”
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
return( (val/1016) + (val%10) );
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
return( (val/16
10) + (val%16) );
void setup(){
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year){
// sets time and date data to DS3231
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year){
Wire.write(0); // set DS3231 register pointer to 00h
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec( & 0x7f);
*minute = bcdToDec(;
*hour = bcdToDec( & 0x3f);
*dayOfWeek = bcdToDec(;
*dayOfMonth = bcdToDec(;
*month = bcdToDec(;
*year = bcdToDec(;
void displayTime(){
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
if (minute<10){
Serial.print(minute, DEC);
if (second<10){
Serial.print(second, DEC);
Serial.print(" “);
Serial.print(dayOfMonth, DEC);
Serial.print(month, DEC);
Serial.print(year, DEC);
Serial.print(" Day of week: ");
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
void loop(){
displayTime(); // display the real-time clock data on the Serial Monitor,
delay(1000); // every second

It works perfectly fine with my DS1307.

Now, I would like to make a trigger-point with an if-statement. I want to (as an example) make an LED turn on everytime the second-pointer is at 20. Everytime the second-counter reaches 20, i would like to make the LED turn on.

I tried implementing it by writing:

int redPin = 2;

void setupLight(){

pinMode(redPin, HIGH)


So now I have the pin-number for the LED and made it high everytime the function will be called. But I’m having trouble making it work. I was thinking about writing something like this:

void loop(){

if (*second == 20){



Is this possible? It doesn’t work when I try to verify the code in the Arduino IDE.

Everytime the second-counter reaches 20, i would like to make the LED turn on.

This is an incomplete requirement, in that it doesn't define when the LED should turn off. Incomplete requirements are a bitch to write code for.

I was thinking about writing something like this

Don't. Think, instead, first about WHERE to add code.

Start with the working code. Understand what it does. loop() calls displayTime() which does a LOT more than its name implies. displayTime() gets the time and it displays the time.

Break that into two functions, with meaningful names. getTime() and displayTime() are good, but loop() needs to then call them both AND the variables that getTime() populates need to be accessible to displayTime().

Then, you can see that you can use those same variables to determine if it is time to turn the LED on.

if (*second == 20){I can't remember seeing that syntax before. What do you expect that line of code to do ?

if (*second == 20){I can't remember seeing that syntax before. What do you expect that line of code to do ?

The function to read the time was (foolishly) defined to take pointers, not references. So, the way to say to store the data where the pointer points, instead of changing where the pointer points, is *pointer = newValue;.

Had the function be properly declared, using references, the usual variable = newValue; syntax would have applied.

Checks to see if the byte pointed to by pointer variable ‘second’ is equal to 20?

Checks to see if the byte pointed to by pointer variable 'second' is equal to 20?

Nasty !

Nasty !

Not really, if you're still a pre C++ programmer :wink:

As an old-time C programmer (learned in college, mid 80s), I’m still a fan of pointers. References were added to C++ to solve specific issues that pointers couldn’t handle -- operator overloading, method chaining, etc. But, when I can get the job done with a good old pointer, I’ll choose that over a reference.