Arduino, clocks and EEPROM

Arduino, clocks and EEPROM

Hi there! It’s been a while since my last post.

Today I’m going to be writing about Arduino. In case you don’t know what Arduino is:

Arduino is a microcontroller used for sensing and controlling things in your environment. Its easiness of use and the fact that it’s open-hardware has made it very popular in the last years.

Right now I’m working on a couple of projects involving Arduino. You’ll know more about them in next posts.

For one of these projects I needed to have a clock in order to store when certain events happened. Luckily Arduino has a library for this purpose:

The Time Library is useful when you need to know the exact time without using an external hardware. Using it is very easy and you can set the time simply by running this line of code:

setTime(12, 0, 0, 24, 6, 2014); //Sets the time to 24/06/2014 12:00:00 HH24

Don’t forget to include the library:

#include <Time.h>  

Be careful that once your Arduino stop receiving energy the clock will reset. You can whether accept this or find an alternative. One possibility is for example to store the time regularly or add buttons to set the time manually, just like any alarm-clock.

If you decide to store it, you can use the Arduino EEPROM for this purpose. Be aware that this memory apart from not being infinite (it varies from 0.512 to 4kb), it has a lifetime, so you should write on it only when necessary. According to this post in the Arduino forum the lifetime is about 100.000 write cycle/ cell. So if you decide to store the time every minute, that would be 1440 writes per day (60*24). That way, and considering you don’t use the same cells for any other purpose, your EEPROM will only last 69.4 days (100000/1440), which doesn’t sound a really good idea.

Anyway, if you decide to store the time at least to make the clock to start on a certain date and not on 01/01/001, you can use the following code:

#include <EEPROM.h>


void saveCurrentTime() {
  EEPROM_writeInt(1, year()); //Writes the year in the position 1 of the EEPROM 
  EEPROM_writeInt(5, month()); //Writes the month in the position 5 of the EEPROM 
  EEPROM_writeInt(7, day()); //Writes the day in the position 7 of the EEPROM 
  EEPROM_writeInt(9, hour()); //Writes the hour in the position 9 of the EEPROM 
  EEPROM_writeInt(11, minute()); //Writes the minutes in the position 11 of the EEPROM 
  EEPROM_writeInt(13, second()); //Writes the seconds in the position 13 of the EEPROM 

void EEPROM_writeInt(int ee, int value)
    byte* p = (byte*)(void*)&value;
    for (int i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);

Then, to load the stored time:

void loadCurrentTime() {  

int EEPROM_readInt(int ee, int size)
    int value = 0.0;
    byte* p = (byte*)(void*)&value;
    for (int i = 0; i < size; i++)
        *p++ =;
    return value;

That’s it . If you’ve found a better way to do this, please leave me a comment!

Hope to see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *