Go Down

Topic: How to make the Arduino and RTC work with the Daylight saving time? (Read 1 time) previous topic - next topic

AudreyM


Hi,

I am using Arduino to control light conditions for a project. The precision of time is very critical and I use the RTC in the code.  The clock did not switch to daylight saving time this weekend, but the computer clock did. I re-started the computer just in case but it didn't fix the issue. And I do not understand why. Shouldn't it synchronize with the computer? I read different posts about time adjustements but I don't know why the pc and arduino are not synchronized. And I don't have time to make some tests after code modifications as I need it to work correctly for the project. I am in the Pacific Time zone and here is the code I use

#include <Wire.h>
#include <Chronodot.h>           
#include <Time.h>                 
#include <TimeAlarms.h>           

Chronodot RTC;

time_t syncProvider()   
{
  return RTC.now().unixtime();
}

void setup()
{
  Serial.begin(9600);     
  Serial.println("Initializing Chronodot.");
  Wire.begin();
  RTC.begin();
  setSyncProvider(syncProvider);     
 

  if (! RTC.isrunning()) {
  Serial.println("RTC is NOT running!");      // following line sets the RTC to the date & time this sketch was compiled
 
  RTC.adjust(DateTime(2014,3,10,5,59,0));
 
  }
 
   if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");
 
 
   
  Alarm.alarmRepeat(6,0,0, MorningAlarm);     
  Alarm.alarmRepeat(18,0,0, EveningAlarm);   
}


void  loop(){ 
  digitalClockDisplay();
  Alarm.delay(1000);
  int irLed = 9;     
  int redLed = 10;
  analogWrite(irLed, 100);   
  analogWrite(redLed, 8);
 
}


void MorningAlarm(){       
 
  int whiteLed = 11;         
  analogWrite(whiteLed, 150);   
     
  delay(1000);                 

   Serial.println("LIGHT ON");                           
}


void EveningAlarm(){       
 
  int whiteLed = 11; 
  analogWrite(whiteLed, 0);   
       
  delay(1000);

  Serial.println("LIGHT OFF");         
}




void digitalClockDisplay()         
{
  DateTime now = RTC.now();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    if(now.month() < 10) Serial.print("0");
    Serial.print(now.month(), DEC);
    Serial.print('/');
    if(now.day() < 10) Serial.print("0");
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    if(now.hour() < 10) Serial.print("0");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    if(now.minute() < 10) Serial.print("0");
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    if(now.second() < 10) Serial.print("0");
    Serial.print(now.second(), DEC);
    Serial.println();
   
    delay(5000);
   
}                            // END

I am not very familiar with Arduino yet, I just started to use it a few weeks ago. Any explaination/suggestion? Thanks for your help,

Audrey

mirith

Don't forget Code Tags.

What RTC are you using?  Its possible it doesn't account for daylight savings time.  Or, since the US decided to be annoying about it, it accounts for it next weekend.

AudreyM

Hi Mirith,

Sorry for the code tags.
I use the ChronoDot V2.1 High Precision RTC.
Sorry for what might be a stupid question, but I am new to Arduino: as this clock could be used anywhere on earth and therefore needs to be set in the adequate time zone, shouldn't it just synchronize with the pc? Or is it working on another principle? Anyway in the worst case, I can still just take that into account myself when I set the time for the alarms, but I would like to understand how it works.
Is there a way to know if it will account for the daylight saving time next weekend? Which would be the worst option, as "uncontrolled"..

Thanks,

Audrey

PaulRB

Hi Audrey,

Potentially you could write some code in your sketch to change to DST & back again at the appropriate dates & times. Use a byte of EEPROM to store the current DST status so it doesn't get done more than once.

Also, here is a suggestion for simplifying your sketch a little:

Code: [Select]
void digitalClockDisplay()         
{
  DateTime now = RTC.now();
  char buffer[20];
 
  sprintf(buffer, "%02d/%02d/%02d %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
  Serial.println(buffer);
   
  delay(5000);
   
}



Paul

Duckie58

Hi Audrey,
Check out this library by my friend Jack.

https://github.com/JChristensen/Timezone

It has all the functions you are looking for.  Jack also has libraries for several of the RTC chips used with the arduino on GIT.
Duckie

AudreyM

Hi Paul and Duckie,

Thanks for the suggestions, I will try with that.

Audrey

liudr

OP,

I am not sure where you live. Most the world does NOT use "day-light-saving" time. If you live in the USA, not all states use it either. Tell me that a small micro-controller such as Arduino is able to determine where it is located and what local rules is and adjusts to this rule. Having a USB cable to a computer is no help. I have a bunch of flash drives. They don't update time when DST goes into effect.

The most effective way is just to program it into arduino code to do the adjustment, and hope you are not running timed events based on RTC tick. They will be screwed, say if you run something every 2 hours. You have to account for that too.

All my projects in deployment are using the UTC. They upload data to servers, which determines the local time from the UTC time stamp and the time zone parameter each unit has.

Paul__B


All my projects in deployment are using the UTC. They upload data to servers, which determines the local time from the UTC time stamp and the time zone parameter each unit has.


Kinda like Linux, eh?

danb35


shouldn't it just synchronize with the pc? Or is it working on another principle?

Your RTC will automatically synchronize with the PC only if you've coded both the Arduino and your PC to do so.  There's nothing in your code that does that.  It tests to see if the RTC is running, and if not, sets it to a specified date and time.  You could code your project to query an attached computer for the time, but you'd also have to have some software on the computer to recognize and respond to that query (IIRC, one of the RTC examples has sample code for Processing to do that).  But there's no automatic updating going on.

liudr



All my projects in deployment are using the UTC. They upload data to servers, which determines the local time from the UTC time stamp and the time zone parameter each unit has.


Kinda like Linux, eh?



We wouldnt even have this problem if we dont have to use dst, some useless concept anyway. Have you guys used it once or twice in the past?

LarryD

Makes a difference up north during the winter months.
Kids catching the school bus appreciate it.
I don't though.
The way you have it in your schematic isn't the same as how you have it wired up!

liudr


Makes a difference up north during the winter months.
Kids catching the school bus appreciate it.
I don't though.


How north are you? Further north than I am? With DST winter days kind end around 4pm here. It's dark at 4pm. It's also dark at 8am. The days are just shorter. There is no way to adjust that away.

LarryD

Canada
Rather than 8:57 sun rises at 7:57 kids take advantage of the morning light not the evening.
The way you have it in your schematic isn't the same as how you have it wired up!

liudr

That's a bit further north. DST helps a bit with the morning.

Jack Christensen

#14
Mar 12, 2014, 01:10 am Last Edit: Mar 12, 2014, 01:13 am by Jack Christensen Reason: 1
Audrey,

If there is an RTC out there that comprehends and adjusts for DST, I'm not aware of it.

RTCs are usually meant to be capable of standalone operation and do not in themselves have the ability to sync with a PC or anything else. That would be a job for firmware (i.e. your code) in the microcontroller (Arduino).

This library as mentioned above is one way to do it. It assumes the RTC is set to UTC and then adjusts to the proper local time. It's been around now through several DST changes so is working well AFAIK.

HTH ... jc

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up