Setting RTC DS1307

Hi all, I am trying to use the example of a real time clock in Arduino. This uses the DS1307 RTC. My problem is I can't find anywhere how to set the date and time of the DS1307 once I have it. Would the instructions come with the DS1307?
The programming needed to use the RTC is given in the example. However ,unless the date and time have already been set on the RTC there will be no input to the Arduino.
Does anyone know where I can find instructions for setting the RTC? I have looked on the internet and can find nothing.

As not everybody knows which example you're talking about, please post the example sketch here and post a link the the library that you're using or that you're going to use.

You set the time using the Arduino; most examples that I know show how to set the data/time.

1 Like

Did you look here?

You can try this sketch: (You need to install RTClib.h Library which is attached with this post.)
RTClib-master.zip (54.2 KB)

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
DateTime myDT;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
byte lastReadSecond = 0;

void setup ()
{
  Serial.begin(9600);
  Wire.begin();

  Wire.beginTransmission(0x68);
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0)
  {
    Serial.println("RTC is not present.");
    while (true);
  }
  Serial.println("RTC is present.");

  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//taking current time from PC
  // rtc.adjust(DateTime(2021, 12, 31, 01, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}

void loop ()
{
  myDT = rtc.now();
  if (lastReadSecond != myDT.second())//wait 1-sec as long as they are equal = 1s has not gones
  {
    showTimeDate();//1-sec has e;apsed; show time and date
  }
}

void showTimeDate()
{
  //update to the new second
  myDT = rtc.now();
  lastReadSecond = myDT.second();
  //------------------------------
  Serial.print(myDT.year(), DEC);
  Serial.print('/');
  //-----------------------------
  Serial.print(myDT.month(), DEC);
  Serial.print('/');
  Serial.print(myDT.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[myDT.dayOfTheWeek()]);
  Serial.print(") ");
  //-----------------------------
  byte myHour = myDT.hour();
  myHour = myHour - 12;
  if (myHour < 10)
  {
    Serial.print('0');  //show leading zero
  }
  Serial.print(myHour, DEC);
  Serial.print(':');
  //-----------------------------
  byte myMin = myDT.minute();
  if (myMin < 10)
  {
    Serial.print('0');  //show leading zero
  }
  Serial.print(myMin, DEC);
  Serial.print(':');
  //--------------------------
  byte mySec = myDT.second();
  if (mySec < 10)
  {
    Serial.print('0');  //show leading zero
  }
  Serial.print(mySec, DEC);
  //----------------------------
  Serial.println();
}

Thanks for that Golam. I have now found a very simple example sketch in Arduino. I had to install a library. I am waiting for the DS1307 now . Here is the code:

#include <time.h>
#include <DS1307 RTC.h>
#include <Wire.h>
set time(hr,min,sec,day,month,yr);
RTC. set(now());

I guess then that the 4th and 5th line would be included in 'set up' . The other 3 lines before set up. The connection would be to the RTC not the nano etc. Not sure of the connections from the laptop to the RTC as there looks like no usb connection to the RTC. Then I suppose you just press compile. A lot of doubt here on the connections. You may be able to help? Then you can use the other example sketch in arduino which inputs the data from the RTC.

DS1307 is an I2C compatiable device; so, it must be connected with a host which has I2C bus, and it is Arduino NANO. The NANO will communicate with the PC to show the Time of the Day on the OutputBox of Serial Monitor of Adduino IDE.

Here is the connection among DS1307, NANO, and PC (Fig-1).


Figure-1:

  • In your future projects, suggest you use the DS3231 RTC.
    This is a far superior RTC over the DS1307.

Hi Golam, I have the nano and laptop of course. Is the blue thing the host you refer to. Are their matching pins on the DS1307? You only show 4 in the diagram. Any idea where I can get this blue thing which could read Tiny 12C? To get this straight then I need 4 things.
Laptop, nano, DS1307, Tiny 12C?

I have found it now.

probably is superior but there is no example of DS3231 in the Arduino sketches.

  • When you use the Library Manager to install the DS3231 library, under examples you will find 4 or 5 sample sketches.
    The latest version for this library is 1.1.2

  • For your DS1307 watch this.
  • Disable the on board battery charging circuit when using a non rechargeable battery.

Was there a DS1307 example in a fresh install? I doubt that very much.

Fine!

1. You have Laptop -- ok.

2. You have Arduino NANO Board - ok.

3. Do you have the bare DS1307 IC (Fig-1) or DS1307 Breakout Board (marked as Tinc I2C/I2C modules in Fig-1 of post #6)?

image
Figure-1:

image
Figure-2:

4. I would suggest that you collect item of Fig-2 of Step-3 and make connection as per Fig-1of post #6 and ten upload the sketch of post #4. You need to install the RTClib.h Library (attaced with this post). the host in Fig-1 of post #6 is the Arduino NANO Board.

5. If yo want to use item of Fig-1 of Step-3, then you hve to build the clock circuit on breadboard as per Fig-3 and then connect it with Arduinno NANO as per Fig-1 of post #6. For learning purpose, you can omit connecting the Battery (real time of the day will not be saved when power is cut).

image
Figure-3:

RTclib.h
RTClib-master.zip (54.2 KB)

Hi Golam, your coding is very complicated and I am getting pretty confused with your instructions. The example coding I found for setting the date and time is very simple. I would like to work with that. I need to find simpler instructions. Someone recommended the DS3231. I have the library for that now and it looks worse than the DS1307.
I thank you for trying your best to explain this to me but I am starting to think a RTC may not be the way to go.

  • No, it’s very easy.

  • You will find DS1307 will need constant manual updating of the time.
    The DS3231 is Β± a few seconds per month.

  • The youtube video offered above should give you all the information necessary.

the video is for ds1307 Larry, not ds3231

that video has nothing to do with a RTC. it is all about a temperature reading

  • Correct, as per your 1st post request.

  • You will find there is almost zero difference between the DS3231 and the DS1307.

  • The first half of the youtube video is about the DS1307.

rtc.adjust(DateTime(2021, 12, 31, 01, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec