ESP8266 & Real Time Clock

ESP8266 & Real Time Clock

I bought a DS3231 which is a real time clock (RTC) with a cell battery on it. It works through the I2C interface so it should not be too difficult to set up.

It turns out there already is a library for it for Arduino and that same library works on the ESP. Even the code is the same.

To hook up the D3231 RTC you need four wires, 3.3V, GND, SDA and SCL. 3.3V (marked as VCC on the board) and GND are easy. Standard I2C ports are 4 for SDA and 5 for SCL. On the ESP this will be D2 for SDA and D1 for SCL.

Setting up the library is straight forward. Download it here and extract it to your Arduino library directory …\Arduino\libraries or something alone those lines. You have to rename the directory called RTCLib-Master to RTCLib and restart Arduino IDE (make sure to close all instances).

Next load the ds3231 example that you can find in …\Arduino\libraries\RTCLib\examples\ds3231. Upload it to your ESP and watch the serial monitor. You will see something like:

2016/9/21 (Wednesday) 17:26:31
 since midnight 1/1/1970 = 1474478791s = 17065d
 now + 7d + 30s: 2016/9/29 5:56:37

If you look at the code you can see it’s rather straight forward. You include Wire.h, the I2C library and RTCLib.h for the RTC library. Initiate the instance RTC_DS3231 rtc and you are set. There is some code in the setup to give the RTC time to boot up. The most important call is rtc.now() which returns the current date time in a DateTime type variable. Also note that the function TimeSpan takes four arguments, days, hours, minutes and seconds. The comment in the code suggests that TimeSpan(7,12,30,6) calculates the time ahead of 7 days and 30 seconds. This should be 7 days, 12 hours, 30 minutes and 6 seconds.

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include 
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);

  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}
Advertisements

2 thoughts on “ESP8266 & Real Time Clock

  1. how to define other pins for I2C for ESP8266. I have ESP-07 and I need 2 for SDA and 14 for SCL

    Thank you ,faithfully yours
    Al But

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s