Using time in arduino

I have 2 questions:

  1. Why to use this method DS1307RTC Library, For Accessing Real Time Clock (RTC) Chips when I can use the time library of the arduino without external components?

  2. Why should someone use arduino MEGA 2560, when the DUE is more powerful(32 bit) at the same cost ?

As you can imagine I am very new to arduino.

:slight_smile:

  1. The TimeLibrary is also not part of the Arduino just like DS1307RTC Library. But yes, TimeLibrary can be used without RTC (Real Time Clock, of which the DS1307 is an example) but then it relies on the timing of the Arduino which is not very accurate. You will lose or gain a couple of second (might even be minutes) a day on your time. Not a problem if you're interested in timing 2 minutes into the future but not so great for a clock...

a) It's more in line with a Uno, it's also a 8-bit AVR
b) It was there before the Due
c) You can get cheap clones in Chine for under $5,-

Thanks, are there any other ways of connecting the DS1307RTC to the arduino except serial and SPI?

No, the DS1307RTC is a I2C device (like all RTC I know actually) so it needs to connect to I2C.

In arduino mega for example, can i use SPI for a component and I2C for DS1302RTC ate the same time?

Can you have a charger in one socket and a lamp in the other?

Aka, yes you can.

Not only that, you can have a dozens of thing on I2C and dozens of things on SPI at the same time.

arduino_gladiator:
I have 2 questions:

  1. Why to use this method DS1307RTC Library, For Accessing Real Time Clock (RTC) Chips when I can use the time library of the arduino without external components?

A real time clock is more accurate at keeping time than the CPU.
So combining a RTC with the Time library is better as the Time library can be periodically synchronized with something that tracks time more accurate.
If you don't do this your time will drift possibly several minutes a day.
Not to mention using a battery backed RTC means the time can be set automatically at power up.
And at this point in time I'd use a DS3231 as it is much more accurate than a DS1307 and you can get them for under $1 USD shipped to your door.

  1. Why should someone use arduino MEGA 2560, when the DUE is more powerful(32 bit) at the same cost ?

And you can get ESP8266 modules that are even more powerful with more resources like 4MB of flash and built in WiFi for even less like around $3-5 USD. As low as a couple of dollars if you just want the module without the other support chips.

The AVRs are nice if you need to drive lots of pins like lots of LEDs. You do some pretty amazing things with nothing but pins using charlie plexing LEDs that you could not do on the other processors.
But these days I would just use WS8212 LEDs and you can drive LOTs of them including color and brightness using only a single pin. They are much easier to wire up.
For lots of applications it is hard to beat the value of the ESP modules particularly if you want to connect it to a network.

You could get away without an RTC if you were using a ESP8266 module that uses NTP to synchronize the time.

--- bill

My only complaint about the ESP8266 modules is that I don't know a ton about them and I can't find any datasheet to explain them in detail like I can get for the 328 or the 2560.

Delta_G:
My only complaint about the ESP8266 modules is that I don't know a ton about them and I can't find any datasheet to explain them in detail like I can get for the 328 or the 2560.

Adafruit's is $10 and if you want, you use the Arduino IDE...

versus $25 for an UNO...

hell... buy a Particle Photon and get FREE cloud API!!! and all the DataSheets you want for $20!

The Uno is a gateway drug...

If you have an external RTC (such as DS1307 or DS3231) hooked up properly, you don't even need the Time library. That's kind of what the RTC is for.

Think of it this way: you could keep track of time by chanting: "One potato, two potato, three potato,..." That's not very accurate. It is much better if you use a wristwatch instead. The RTC is essentially a "wristwatch" for your Arduino.

Here is a sketch I wrote, which you might find useful. What the sketch does is, it attempts to read the time from the RTC (DS3231 will work, and I think a DS1307 would also work), and it will tell you the result of the attempt: either failure or success; and if success, the time and date.

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
 
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}
 
void loop()
{
  // ask RTC for the time
  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) { 
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC"); 
  }
  delay(500);
}

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}

void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    case 1: Serial.print("Mon"); break;
    case 2: Serial.print("Tue"); break; 
    case 3: Serial.print("Wed"); break; 
    case 4: Serial.print("Thu"); break; 
    case 5: Serial.print("Fri"); break; 
    case 6: Serial.print("Sat"); break; 
    case 7: Serial.print("Sun"); break;
    default: Serial.print("Bad");  
  }
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}

odometer:
If you have an external RTC (such as DS1307 or DS3231) hooked up properly, you don't even need the Time library. That's kind of what the RTC is for.

If you know every in and out of the chip you don't need any library. But it makes it damn easy and quick to use...

Aka, the time library works WITH the RTC and gives you all sorts of nice feature (one being to run without RTC) for easy access.

And as I can see the Time library is spit up into the newer Time library, TimeAlarm library and the DS1307RTC library.

BulldogLowell:
Adafruit's is $10 and if you want, you use the Arduino IDE...

That's not fair, then you need to include the serial cable as well which already brings you to $19,-

The ESP8266 is a cool and useful chip but it's kind of power hungry...

Not only that, you can have a dozens of thing on I2C and dozens of things on SPI at the same time.

You can have dozens of things on the SPI bus at the same time, as long as you have dozens of pins to use as chip select pins to select each one individually.

You can have dozens of things on the I2C bus at the same time, as long as each has a unique address.

Just to make things clear... 8)

septillion:
No, the DS1307RTC is a I2C device (like all RTC I know actually)

1307's predecessor the 1302 wasn't

I cannot find arduino due in board options in arduino IDE! What do I do here?

Everything starts by reading the starter guide :wink:

Thanks!

I get this error in compiling my code for MEGA 2560.

error: size of array 'day_measurement' is too large

If I use DUE which has more memory, will it accept the array?

Or it all has to do with the compiler, so any board I use will not accept the array?

Due has more memory indeed. But without showing the code I have no idea if it will even fit there. And what on earth do you try to put into memory?

DS3234 is an SPI RTC, someone mentioned only knowing about RTC with I2C.

Depending on what your IO needs are, Atmega1284P has 16K SRAM if you need more dynamic memory than the 8K in the Mega2560.

Thanks a lot for the answers I look all the info you provided me!