Skip to content Skip to sidebar Skip to footer

ESP8266 NodeMCU RTC DS3231 Code

ESP8266 NodeMCU RTC

ESP8266 NodeMCU RTC - In this article I will share a guide to get RTC data using NodeMCU. The ESP8266 NodeMCU V3 currently using doesn't have RTC Built In. This is different from STM32 which has RTC Built In, read the tutorial here. Therefore, we must use an IC or RTC Module, such as DS3231, DS1307, or others.

RinkyDink's DS3231 library is a good one. However, there is one problem when I use it for advanced, like the day of the week cannot be synchronized. So I found a better library made by Adafruit which is RTClib. 

This RTClib library supports the DS3231, DS1307 and PCF8523 RTC chips. For boards, this RTClib library supports:

  • ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V,  Metro 328,  Metro Mini
  • ATmega328 @ 12MHz : Adafruit Pro Trinket 3V
  • ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0
  • ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro
  • ESP8266 : Adafruit Huzzah
  • ATmega2560 @ 16MHz : Arduino Mega
  • ATSAM3X8E : Arduino Due
  • ATSAM21D : Arduino Zero, M0 Pro
  • ATtiny85 @ 16MHz : Adafruit Trinket 5V
  • ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V


Based on the datasheet, this RTC DS3231 made by MAXIM INTEGRATED has the following features: 
  • Real time to count seconds, minutes, hours, date, month and year valid up to 2100.
  • Accuracy ±2ppm from 0°C to +40°C
  • Accuracy ±3.5ppm from -40°C to +85°C
  • Digital temperature sensor output accuracy ±3°C
  • I2C Communication (400kHz)
  • Battery backup voltage 3.3V

Download Library DS3231

RTC module I am using In this article is DS3231, which uses Adafruit's library. To operate this RTC esp8266 arduino ide, please download the RTClib library at the following link:

Include RTClib Library that you have downloaded to the Arduino IDE:

  • Open Arduino IDE
  • Click Sketch > Include Library > Add .ZIP Library
  • Look for the RTClib-master.zip Library that you have downloaded
  • Click OK

DS3231 NodeMCU Wiring

NodeMCU to properly retrieve RTC data, must connect them properly. The communication used is I2C. You have to connect SCL to SCL and SCK to SCK. You can see the wiring like this:

ESP8266 NodeMCU RTC DS3231 Code

Upload RTC Basic Program Code

Now, we will try to read data from RTC. The data include day, date, month, year, hour, minute and second. Below is the basic program code that I have provided, based on the example.file of the Adafruit RTC. Please copy the following program and paste it into Arduino IDE.

#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("RTC Not Found");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC is losing power, let's set the time!");
  }

  // the following lines set the RTC to the date & time of the computer when this sketch was compiled
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  // If you want to set the RTC time manually, use the line rtc.adjust
  // Example January 21, 2014 at 03.00:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

void loop () {
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); // Show the Day
  Serial.print(", ");
  Serial.print(now.day(), DEC);     // Show the Date
  Serial.print(' ');
  Serial.print(Month[now.month()]); // Show the Month
  Serial.print(' ');
  Serial.print(now.year(), DEC);    // Show the Year
  Serial.print(", ");


  Serial.print(now.hour(), DEC);    // Show the Hour
  Serial.print(':');
  Serial.print(now.minute(), DEC);  // Show the Minute
  Serial.print(':');
  Serial.print(now.second(), DEC);  // Show the Second
  Serial.println('\n');

  delay(1000);
}

Please select the ESP12E-1 board and port on the board. If you don't have a setup board or haven't installed NodeMCU on Arduino you can read the article here.

If you have never uploaded a program to NodeMCU, you can read an article on how to upload a Blink program for beginners.

After the program is uploaded, open the Arduino IDE Serial Monitor and you will see the data that looks like this:

ESP8266 NodeMCU RTC DS3231 Code


Set Time Using Serial

Now we will create a program with which we can set the RTC manually. Pay attention to the command rtc.adjust(DateTime(year, month, date, hour, minute, second));, this will make the program will set the time at the same time.

For example, if we only want to change the date or time, how do we do that? Here I include the complete program. This program will display RTC data on Serial Monitor

To change the data, send the following characters to the serial monitor:

  • TG to change Date
  • BL to change the Moon
  • TH for changing Year
  • JM to change Hours
  • MT to change Minutes
  • DT to change Seconds


/*
   www.chippiko.com

   This program will display RTC data on Serial Monitor

   To change the data, send the following characters to the serial monitor:
    - TG to change Date
    - BL to change the Moon
    - TH for changing Year
    - JM to change Hours
    - MT to change Minutes
    - DT to change Seconds
*/

#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String read_string = " ";
int read_int = 0;

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("RTC Not Found");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC is losing power, let's set the time!");
  }
}

void loop () {
  readRTC();
  setRTC();
  delay(1000);
}

void readRTC()
{
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); 
  Serial.print(", ");
  Serial.print(now.day(), DEC);    
  Serial.print(' ');
  Serial.print(Month[now.month()]);
  Serial.print(' ');
  Serial.print(now.year(), DEC);  
  Serial.print(", ");
  Serial.print(now.hour(), DEC); 
  Serial.print(':');
  Serial.print(now.minute(), DEC);  
  Serial.print(':');
  Serial.print(now.second(), DEC);  
  Serial.println('\n');
}
void setRTC() {
  DateTime now = rtc.now();

  while (Serial.available() > 0) {

    read_string = Serial.readStringUntil('\n');
    read_string.trim();
    delay(100);

    // The day cannot be changed because it will change itself according to the date
    
    if (read_string == "TG") {
      Serial.print("Insert the date: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), read_int, now.hour(), now.minute(), now.second()));
    }

   
    if (read_string == "BL") {
      Serial.print("Insert month: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), read_int, now.day(), now.hour(), now.minute(), now.second()));
    }

    
    if (read_string == "TH") {
      Serial.print("Insert Year: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(read_int, now.month(), now.day(), now.hour(), now.minute(), now.second()));
    }

    
    if (read_string == "JM") {
      Serial.print("Insert hour: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), read_int, now.minute(), now.second()));
    }

    //Jika menit
    if (read_string == "MT") {
      Serial.print("Insert minute: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), read_int, now.second()));
    }

    
    if (read_string == "DT") {
      Serial.print("Insert second: ");

      //Wait a moment for new data to be sent
      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), read_int));
    }
  }
}

Thank you for visiting Chip Piko's website. Hope it is useful.

Post a Comment for "ESP8266 NodeMCU RTC DS3231 Code"