4 Replies Latest reply on May 22, 2014 10:49 AM by jhmurry_Intel

    How to access the real time clock from Arduino

    jhmurry_Intel


      From Arduino, how can I access the real time clock? I would like to be able to retrieve the current time and set the current time.

        • 1. Re: How to access the real time clock from Arduino
          flummer

          Hi,

           

          What about using system("...") calls as ado explains in this post with the date command?

           

          /Thomas

          1 of 1 people found this helpful
          • 2. Re: How to access the real time clock from Arduino
            flummer

            Hi again,

             

            I just played a bit with this tonight and using only the system("...") command won't work, as you won't be able to get the output back into the "Arduino" code, but using popen(...), you get a file pointer that you can read from, so the below code sets the time in the setup section and reads it in the loop:

             

            void setup()
            {
              // put your setup code here, to run once:
              Serial.begin(9600);
            
              char *cmd = "/bin/date 1021222513"; // Oct. 21, 2013, 22:25
              char buf[64];
              FILE *ptr;
            
              if ((ptr = popen(cmd, "r")) != NULL)
              {
                while (fgets(buf, 64, ptr) != NULL)
                {
                  Serial.print(buf);
                }
              }
              (void) pclose(ptr);
            }
            
            void loop()
            {
              // put your main code here, to run repeatedly:
              char *cmd = "/bin/date";
              char buf[64];
              FILE *ptr;
            
              if ((ptr = popen(cmd, "r")) != NULL)
              {
                while (fgets(buf, 64, ptr) != NULL)
                {
                  Serial.print(buf);
                }
              }
              (void) pclose(ptr);
              delay(1000);
            }
            

             

            This is a general example on calling a command on the linux side with both passing arguments and getting data back, but for setting and getting time, I have found that there are also library functions like gettimeofday(...) and settimeofday(...) that can be called directly without starting a new process.

            • 3. Re: How to access the real time clock from Arduino
              jhmurry_Intel

              flummer -- Excellent response. Thank you!! Now I can use some of the format options with date to parse the "buf" variable. This also shows a great design pattern as you mentioned  for Linux side passing arguments and getting data back.

               

              Two replacement options for your command that I found useful are below.

                char *cmd = "/bin/date +%F%t%T"; // outputs as 2013-10-21     22:25:00

                OR

                char *cmd = "/bin/date +%s"; // outputs as 1382394306  // %s = seconds since `00:00:00 1970-01-01 UTC'

              1 of 1 people found this helpful
              • 4. Re: How to access the real time clock from Arduino
                jhmurry_Intel

                Something else worth noting that I wasn't aware of and that many not familiar with Linux may not be aware of....

                 

                There are two clocks: System clock and hwclock (RTC). The hwclock keeps time after power down IF you have the coin battery attached. Some useful Linux commands in dealing with those is:

                • To show hardware clock time: # hwclock
                • To show system clock time: # date
                • To set system clock using hwclock: # hwclock --hctosys
                • To set hwclock using system clock: # hwclock --systohc
                • To set system clock to May 22 2014 at 9:32am ( mmddhhmmyy ): # date 0522093214