unix timestamp

hey guys,

I am using da3231sn RTC in my project.I want to set the system time,also want to set an alarm.When alarm will generate i want unix timestamp(total number of second in one number ) and want to store it in another variable.That will helpful for my further procedure in project.

So can anybody suggest me the lib. or procedure or anything to achieve my goal.

i have already try DS1307RTC.h library with Time.h library to set the system time in DS3231SN.I am success.Then i try to set an alarm in DS3231SN using Wire.h library but the alarm is not generating.

i have put alarm for one minute.

I am serching for some function which accept the RTC time(hour,min,sec,date,month,year) and return me one number which is having total num of second same as we see in unix timeticks.

The Time library returns the Unix time with the now() method.

I use this library: https://github.com/JChristensen/DS3232RTC

It uses time_t values and works with the Time library.

I've used on several projects with DS3231 chips.

For alarms you can use the TimeAlarms library addon to Time or use the alarm functions in DS3232RTC

Jack has a timezone library as well. https://github.com/JChristensen/Timezone While it handles things a bit backwards in terms of requiring the user program to deal with handling of the timezone, it works very well and is easy to configure.

--- bill

+1. I highly recommend those two libraries. I use them exclusively, in conjunction with the Time library.

I didn't see anything about Time Zones in your post, but if you handle them, it will take care of daylight savings time automatically. That in itself makes it worthwhile. If you do that, it's best to set your RTC to UTC time.

It makes your clock essentially, "set and forget".

thank u all.

one more confusion,why can’t i use wire.h lib. as well to set the alarm in RTC DS3231SN.that would be the easiest way i think.

i want alarm for every after one hour on all days.(i mean it doesn’t matter what day is there simple…)

i have been go through the alarm function in ds3232rtc library file.but it seems like we need to give day also.that is not suitable for my project.so can i do something like disable or avoid totally ?

Avoid. It's much easier to do it in software.

bperrybap: I use this library: https://github.com/JChristensen/DS3232RTC

It uses time_t values and works with the Time library.

I've used on several projects with DS3231 chips.

For alarms you can use the TimeAlarms library addon to Time or use the alarm functions in DS3232RTC

Jack has a timezone library as well. https://github.com/JChristensen/Timezone While it handles things a bit backwards in terms of requiring the user program to deal with handling of the timezone, it works very well and is easy to configure.

--- bill

The alarm can be used to wake a sleeping AVR?

If you want an alarm evey hour:

RTC.setAlarm( ALM1_MATCH_MINUTES, 0, 0, 0, 0 ); // that will set the alarm flag when hour changed

Then in loop():

if ( RTC.alarm( ALARM_1 ) ) // this function return true if the alarm flag was set by ds3231, and clears it for the next alarm
{
  doSomethingEveryHour();
}

If you want to use an interrupt (for example if you put your arduino in sleep mode and want to wake it once per hour), then you can do something like this:

pinMode( rtcAlarmPin, INPUT_PULLUP ); // rtcAlarmPin is the pin that is connected to the INT/SQW output of the DS3231
attachInterrupt( digitalPinToInterrupt( rtcAlarmPin ), NULL, FALLING );

RTC.alarmInterrupt( ALARM_1, true );
RTC.setAlarm( ALM1_MATCH_MINUTES, 0, 0, 0, 0 );

GoForSmoke: The alarm can be used to wake a sleeping AVR?

Yes, but it requires an extra wire from the RTC to the AVR to drive an AVR interrupt pin. However, more than likely if you are wanting to put the AVR asleep you probably want it for low power reasons. And to get low power with an RTC module will require h/w mods to the module as well.

--- bill

RTC alone uses less power than RTC plus AVR but I was thinking of an RTC with its own battery.

GoForSmoke: RTC alone uses less power than RTC plus AVR but I was thinking of an RTC with its own battery.

The ds32xx parts can be told to drive the clock out/interrupt signals even when running on battery backup. But trying to run the entire system on batteries with low power can get tricky. Even something as simple as using a voltage regulator or a voltage booster will kill your battery life.

If using a battery powered AVR, then the AVR always has VCC, so usually you would typically connect the battery AVR VCC to the RTC module. And if you have the battery AVR vcc connected to the RTC then the RTC will run off that power source rather than its own backup battery. Many RTC modules have LEDs, and other devices like memory chips that consume extra power when using VCC vs their battery backup. So an unmodified RTC module will consume much more power than an AVR that is asleep.

All these can be solved with some h/w mods and some additional s/w to ensure that everything is put to sleep. --- bill

@bperrybap

Just control VCC of the RTC module with an output of the arduino :)

If using ZS-042 module (cheapest DS3231 module on ebay), the only modification needed is to remove a resistor as described on this topic.

Karma to you two, I have a much better picture of options now. Will try for external power.