Go Down

Topic: how to store RTC time in an arduino uno board, which library should i use...? (Read 4298 times) previous topic - next topic

bperrybap

What's happening is known as an XY problem. Literally in this case!
:)

But he did give a hint in post #10 that this involved elapsed time, hence my comments about favoring epoch based time values as using those make it trivial to calculate time durations.
Wheras using hours/mins for that can get messy depending on the duration and the time when the events occur.

Heck you could even use millis() if all you need is a semi accurate time duration.
The Time and TimeLibraries can work without an RTC as well....  ;)

-- bill

pranav2016

my project is having three functions
1st function.... turn on the light automatically at 6 pm and turn off at 6 am daily.

2nd function.... turn on the the light when button is pressed and off automatically at 12 pm. and once button is pressed we should not have to press it again....light should be on at the same time when button is pressed earlier and off at 12 automatically.

3rd function...same function as 2nd but it is for making the light off;;;;
   
   i am using pushbutton to switch between these functions

ChrisTenone

Hi Bill,

If our OP were making a general purpose timer, I would agree that using 'epoch time' would be simpler due to having all the same units, say seconds or milliseconds, and thus all calculations would be continuous. From the last post however, it looks like he's building a programmable on/off switch. If this was my project, I would probably use clock time though. When you push the button, set a variable with the hms. I'd compare that variable to the clock time once a second and set the light switch accordingly.

----

Hi Pranav,

Now it makes sense to me. I outlined my idea above. The example I posted earlier is how I interact with a modern RTC chip, such as the DS3231, or the older DS1307 using I2C, without a Time library. You can also count millis (or seconds) without a library. The function millis() is made for this - it's the milliseconds since you last started your Arduino. Theoretically* there are 1000 milliseconds in a second, 3,600,000 in an hour, and 86,400,000 per day.

I said theoretical, because the milliseconds that are counted by an Arduino are not exactly a millisecond long. They use a resonator that is within 5% of the real time, but it changes with temperature, and age.  This is the big advantage of an RTC chip. It is accurate to much less that a percent. And it provides both a precise one second pulse (useful for accurate epoch timing), as well as stream the clock time over I2C.

In addition to your Arduino circuit (with or without an RTC), with interface components like the button, a mode switch, and perhaps a time and status display, you will also need the power switching circuitry. Probably using relays.

Start by drawing your circuit in block-schematic form. This is so you (and others) can tell exactly what is hooked to what. And write the first iteration of your sketch. Come back, post your schematic and code (use code tags!!), and folks will jump right into it.
What, I need to say something else too?

bperrybap

This project sounds like it could be even solved with some of the X10 modules and its companion s/w.

For any time based s/w I still prefer to use unix epoch based time. With something like the Time library, you get the flexibility of using either form of time and can convert back and forth if/when necessary.
This allow tracking time efficiently as an epoch which also allows easy delta calculations and then convert to and/or use human time only when necessary - which might be in the user interface used to display and configure things.

The Time and TimeAlarms library seem like a good fit for this project based on the descriptions i've seen so far.
TimeAlarms can be used to create the daily periodic events/alarms and you can create/destroy timers as necessary to handle the time based events needed after button presses.

Seems pretty simple if Time & TimeAlarms is used.
Just set up a sync provider to allow the Time library to  periodically read the time from the RTC to keep the time accurate.
The main code doesn't have to do any polling or comparison of the time as
the TimeAlarms library will call the designated functions for any/all configured alarms or timers at the appropriate time.



If it were me, I'd use a DS3231 and combine these libraries:
- Time
- TimeAlarms
-  DS3232RTC https://github.com/JChristensen/DS3232RTC
- Button https://github.com/JChristensen/Button

This should provide all the needed time and button reading capabilities.
I've used these libraries on several clock projects and it allows for a very quick development cycle as all the "hard" stuff is already done and debugged.

Why create all this (or some subset of it) from scratch when you could use existing libraries and get on with the project?

If existing libraries are not used, code to do all the time comparisons and track the back ground events for the button pushes has to be written.
While it isn't that difficult, if things change or more functionality is added, it can start to get messy depending on the initial architecture used.
i.e. you can get away with lots of stuff when there is only a single event, single time, single button etc, but if things change or expand it can get messy to update the s/w, depending on how it was written.



I recommend using a DS3231 over the older DS1307 for several reasons
- they are now the same price (less than $1USD shipped to your door with a battery)
- DS32321 is more accurate
- DS1307 only runs a 100kHz so you can't speed the bus up to 400Khz
(pretty much all current I2C devices support 400Khz - faster can be important in some environments as it frees up the processor to do other things)


Be prepared to do some slight h/w mods to most of the RTC boards you buy on Ebay and/or directly from other suppliers as most of these modules were designed for a re-chargable battery and the charge circuit is total CRAP. It should be removed. It is typically one trace cut to to it. Also, many of the modules come with a non chargeable battery, which means that after some period of time, the  battery will leak and fail unless the charge circuit is disabled.
I'd recommend buying a DS3231 that comes with a non chargeable battery, then modding the module to disable the charge circuit. (I've done this on all the RTC modules I've used)



But this is just my views.....
Others will likely have alternative ways of handling this.

--- bill


ChrisTenone

Another advantage the 3231` has over the 1307 is the crystal is inside the chip, so there is one less (delicate) component to worry about.
What, I need to say something else too?

pranav2016

THEN WHAT WILL BE THE FUNCTION TO STORE THE rtc TIME THEN......

ChrisTenone

What, I need to say something else too?

bperrybap

THEN WHAT WILL BE THE FUNCTION TO STORE THE rtc TIME THEN......
Huh? I can't make any sense of this.

Gabriel_swe

THEN WHAT WILL BE THE FUNCTION TO STORE THE rtc TIME THEN......
A cheap RTC module keep track of time. Do you want to store on / off switches?

pranav2016


UKHeliBob

Do you need the actual time or do you need to know how long (hours, minutes, seconds) a switch was on or off ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

bperrybap

Your description in post #16 is really vague.

It mentions 3 functions:

1) Turn on light at 6pm and off at 6am

2) turn on light when button 1 pressed, and turn light off at noon (12 pm)
Then this:
Quote
and once button is pressed we should not have to press it again....light should be on at the same time when button is pressed earlier and off at 12 automatically.
What does that mean?

3) No idea what this means as it doesn't really say:
Quote
3rd function...same function as 2nd but it is for making the light off;;;;
And this last bit:

Quote
i am using pushbutton to switch between these functions
What does that mean?

The description of what you said isn't clear enough for people to understand what you want.

--- bill

odometer

For any time based s/w I still prefer to use unix epoch based time. With something like the Time library, you get the flexibility of using either form of time and can convert back and forth if/when necessary.
You are trying to kill an ant with a howitzer.
And the TimeAlarms libraries are almost certainly more trouble than just writing the thing from scratch.

And I am sure that this is all Greek to the OP.


Why even use an Arduino? Why not just use one of these?
http://www.homedepot.com/p/Intermatic-15-Amp-Plug-In-Lamp-and-Appliance-Timer-White-TN111K/205478759

bperrybap

You are trying to kill an ant with a howitzer.
And the TimeAlarms libraries are almost certainly more trouble than just writing the thing from scratch.
I would disagree.
I like to get things done and actually finish a project to be able to put it to bed without having to monkey around with it. As soon as you do anything "real" that will involve daylight savings time or timezones things will start to get messy.
TimeAlarms is quick and easy to use and supports several different types of events/timers/alarms.
Yeah, I've written stuff like this from scratch before, but having done that, and having used these types of epoch based libraries MANY times over the decades and having the flash code space available in the AVR to be able to use more generalized libraries, I'd still recommend using Time & TimeAlarms libraries as it can provide a much quicker and easier path to project completion.
Using these types of libraries allows putting a clean abstraction layer between the main code and the h/w which keeps the main line code clean and allows it to focus on the overall task vs having to sprinkle h/w specific code all over the place which makes any potential maintenance more difficult.
I'm a big believer in layered s/w design.
I've used the libraries I've mentioned earlier on several personal clock projects.
But then I've been doing embedded s/w development and *nix development for 35+ years...

Quote
And I am sure that this is all Greek to the OP.
This I believe. And I'm guessing that this is the #1 reason that most people will shy away from using epoch based time tracking.
They are not grasping the concept and its usefulness.

Quote
Why even use an Arduino? Why not just use one of these?
http://www.homedepot.com/p/Intermatic-15-Amp-Plug-In-Lamp-and-Appliance-Timer-White-TN111K/205478759
I was thinking more along the lines of a X10 computer interface.
It is programmable and the built intelligence, can calculated sunrise/set for any georgraphic location, and supports macros for doing more than simple independent on/off things.

--- bill

odometer

Quote from: odometer
And I am sure that this is all Greek to the OP.
This I believe. And I'm guessing that this is the #1 reason that most people will shy away from using epoch based time tracking.
They are not grasping the concept and its usefulness.
Chances are, the OP's project doesn't care what day it is, nor how many days have passed: it only cares about the time of day. And for that, anything epoch-based is overkill.

And as for "daylight savings time and time zones": does either the Time or the TimeAlarms library handle those? If so, that's news to me.

If nothing else, Daylight Saving Time could be implemented by a simple "spring forward, fall back" toggle switch. Otherwise, woe to the OP if his (her?) country changes the Daylight Saving rules.


Is the OP even aware that simple, easy-to-set electromechanical lighting timers exist? Electronic versions of the same also exist, but might be a bit more difficult to set.

Go Up