sketch for RTC required

Can someone please recommend a RTC sketch. I have tried many sketch examples and for me none seem to work.

I have both a Uno and a Mega and am trying to install a DS3231 RTC.

Before seeking advice I thought I should start off with a reliable sketch and then after installation come back to the Forum for help.

Basically I need sketch or sketches which will;

  1. Instal the RTC to both Uno and Mega.

  2. Enable me to set the RTC time.

  3. Read back the current time and use that time to manipulate an output pin.

Can you please recommend appropriate sketch.

The library which you probably have now, should have examples.

Have a look at the thread for one of my projects:
https://forum.arduino.cc/index.php?topic=408565.0

Be aware that, when I wrote that thread, that project was a work in progress.
The final post in that thread is code for a chiming clock which uses two buttons, a 16x2 LCD character display, and either a speaker or piezo buzzer.

Be aware that, if you actually build this, you might need a resistor for your speaker or buzzer. This is to limit the amount of current passing through it, because too much current could damage your hardware.

This is what I have always used http://bildr.org/2011/03/ds1307-arduino/

Here's an old sketch that works for me on a Nano / UNO, uncomment lines 18 & 19, set time and date, upload to Arduino, then comment lines 18 & 19 so it does not reset the time and date next time.
I like the DS3232RTC library by Jack Christensen, you can find and install it from the Arduino IDE library manager.

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
   //set the initial time here:
   //DS3231 seconds, minutes, hours, day, date, month, year
   //setDS3231time(0,0,5,1,18,6,18);
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  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)
  Wire.endTransmission();
}

void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
  Serial.println();
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second
}

Thanks all for replies.

To start off I have tried attached Sketch but get unexpected serial monitor output as per attached JPG.

Looking through sketch I can see no obvious reason for strange output.

Any helpful suggestions for a NOOB?

sketch_jun18d.ino (2.95 KB)

Sorry, should have said line 19 only leave line 18 as is, try this, I will set time & date to UTC.

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
   //set the initial time here:
   //DS3231 seconds, minutes, hours, day, date, month, year
   setDS3231time(0,40,6,2,18,6,18); // <<< set time & date here
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  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)
  Wire.endTransmission();
}

void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
  Serial.println();
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second
}

Here's a few lines from serial monitor:

6:42:00 6/18/18 Day of week: Monday

6:42:01 6/18/18 Day of week: Monday

6:42:02 6/18/18 Day of week: Monday

6:42:03 6/18/18 Day of week: Monday

6:42:04 6/18/18 Day of week: Monday

6:42:05 6/18/18 Day of week: Monday

6:42:06 6/18/18 Day of week: Monday

6:42:07 6/18/18 Day of week: Monday

6:42:08 6/18/18 Day of week: Monday

6:42:09 6/18/18 Day of week: Monday

6:42:10 6/18/18 Day of week: Monday

6:42:11 6/18/18 Day of week: Monday

6:42:12 6/18/18 Day of week: Monday

6:42:13 6/18/18 Day of week: Monday

6:42:14 6/18/18 Day of week: Monday

Did as suggested even recopied your second sketch but exactly the same

print out as per sample.

Most sketches I have tried have the same result.

They seem to compile and upload OK but the output from the Serial Monitor is
never what I would expect.

Have tried 2 different Arduino, 2 different RTC and replaced all the jumper leads.

Do you have a battery for the DS3231? The output looks like the clock is not set. Or you could have copied something wrong. Copy the code from outsider's post, and paste it into the IDE window, rather than retype it.

Thank you to Outsider and ChrisTenone it is now working fine.

Really appreciate your help.

Thanks :slight_smile: :slight_smile:

This community shares help and solutions.

If you tell us what the problem was, it might help the next person.