DS1302 problem [SOLVED]

I have a project, part of which uses a SD card, an HC-05 Bluetooth board and an old DS1302 real time clock. I was using a Nano, but ran short of SRAM, so I upped the game to a Nano Every. After loading the board (via the Board Manager), I tried to compile the sketch which was working on the Nano. I had a object, invoked as DS1302 RTC(kCePin, kIoPin, kSclkPin); . This would not compile for the Every, yielding some odd error about expecting a ) before a * . I had a flash of inspiration, and changed RTC to RTC_ (R T C underscore), and it compiled without objection, uploaded to the Every and ran as expected, logging time-stamped data to the SD card (with echo to Serial and via Bluetooth). I now have lots more SRAM to play with!

RTC is already defined for the Nano Every, because the atmega4809 has an internal RTC, although it is not highly accurate because the timing is based off the processor clock and not a crystal controlled oscillator (the UNO WiFi Rev 2 board that used the atmega4809 does have the 32.768KHz crystal to provide an accurate RTC).

1 Like

I figured it was something like that. It’s a pity the compiler didn’t have a more understandable error message - something like “RTC is already defined” or “multiple definition of RTC”, which would have led me to a solution much more quickly.

I’ve always been curious why folks use an RTC module. You’ve got a crystal controlled Arduino Nano, UNO, whatever and a HARDWARE timer that spits out the values of the number of milliseconds since power ON. If you’re just interested in the time of day, why do you need an RTC? OK, there is a constant (if your microcontroller is in a relatively stable temperature area) drift in your 16mHz crystal but you can compensate for that. That’s what I do and it works great. I just measured that drift after a few days and I adjust the clock every midnight to catch up or slow down my clock. I was losing 89.65 seconds a day. I can show you my code if you really want but it’s really a simple concept (to me anyway).

If you’re building a datalogging (or any other) system which is designed to be installed and run stand-alone for months or years, and to be resilient through power-outages, temperature variations, random resets (due to external factors - lightning strikes? cosmic rays? some idiot pulled the wrong plug? whatever …) an RTC is really the only way to go.
If it’s a continuously monitored system and it doesn’t really matter if it loses a minute/hour/day/whatever, then your method is great.

I don’t need the RTC. My 2 sensors are standalone and run on batteries. They sleep a lot and depend only on the watchdog timer to make their measurements every 65 and 100 minutes and then transmit them to a third unit. The 3rd unit is connected to the web and Thingspeak does all the datalogging. Unit 3 also displays the last measurements and the time of day. The clock drifts less than a second a day with the automatic midnite correction algorithm but yeah, I have to reset it if we have a power shortage. Consequence, I have to find the time of day on another clock.

No, usually the Uno has a ceramic resonator.

(Also, sp. “16MHz”)

1 Like

“I don’t need the RTC.”
Maybe you don’t, but I do.

I always thought it was a crystal on my Nano. Anyway it’s pretty damn accurate with its midnite correction! So is the m for mega capitalized in your part of the world in honor of Mr. Hertz?

No, the M for mega is capitalised globally to distinguish it from m for milli.

And yes, Hertz is capitalised, just like Newton, Joule, Ampere, Volt(a) etc.

Actually, no, not if you want to comply with the SI standard notation. The symbols are sometimes upper case (‘V’ for volts) and sometimes lower case (‘m’ for metres). The symbol for hertz (cycles per second) is ‘Hz’.

When written as words, they should always be lower case: hertz, meter, volt, ampere, kilogram, newton, farad, etc. The only time you would ever capitalise them is when referring to the person: Newton, Volta, etc.

This is a good guide:


…but if you prefer you can read it straight off the SI website:


So, read my above note: it is capitalised in every part of the world. :grinning: The symbol ‘mHz’ means millihertz, or thousands of a hertz.

Finally, you can see the full list of SI prefixes (milli, micro, nano, kilo, mega, etc) here:


Yes I did know about the convention that when the unit name is spelled out, it is not capitalised, I omitted to mention that the capitalisation refers to the initialism.

And that seconds and metres are not capitalised.

Indeed, and only when those units are derived from a person’s name, hence ‘V’ after Volta, but ‘m’ for metres, as it was not a person’s name.

Do you think that’s a resonator or a crystal?

I can’t see your JPEG, but know the official Uno is fitted with a resonator (at least that’s what the spec on the product page says)

Two microcontrollers on the Uno. The Atmega328P has a 16MHz resonator but the ATmega16U2 has a 16MHz crystal!

People use RTC clocks because arduinos' millis() will break after 51 days (because of unsigned long overflow).

Basically millis() reaches the maximum possible number for an unsigned long after 51 days of continuous running, which means that millis will break.

You could reset the arduino using the reset pin, but that would give you all sorts of desyncing problems. People use an RTC because it isn't prone to this problem (time is broken down into it's individual components), and you can get an absolute time (rather than something that is relative to when the arduino was turned on).

People use RTCs because they're battery-backed, and so maintain the time if power is removed, and because they're usually equipped with a crystal, rather than a resonator.

[Mod edit - strong language removed]

Original poster edit: what strong language?
Original poster edit edit: don't worry, the "strong" language is quoted on the next post

{Facepalm} You're new here, aren't you?

First off, would you mind taking the tone down?
trying to bite someone's tongue off for having an opinion helps no one.

People use RTCs because they're battery-backed, and so maintain the time if power is removed, and because they're usually equipped with a crystal, rather than a resonator.

I agree. This does not mean, however, that this is the only use for them. I said what I said in my previous post because that is what I was using it for, and have seen other people use it for in the past.
I have an automatic indoor watering system for my pot plant that is only supposed to turn on at a specific time at a specific date. I could use millis() and use arduinos' resonator, and then do some math with __DATE__ and __TIME__ to get epoch, but that would go out of sync when the arduino turns off/on.
Instead I could use an RTC, setting the time once and not having to worry about relative time or overflow.

(I'd love to hear your explanation of how millis will break, nearly a day and a half after rollover)

millis() returns an unsigned long. After 51 days of the arduino continually being on, the unsigned long will wrap around from its' maximum value back to zero. This can be undesirable, which is why i used the word "break".