RTC on Wire2 in multi-tab sketch


Well-known member
My RTC_master sketch is fairly mature and works for the DS1307 module with inbuilt NVRAM, or the DS3231 module with 32K EEPROM chip.
I use the permamanet memories to record itmes such as datetime when set, battery changed, drift factor (for correcting DS1307)

The sketch works well on Wire (i.e. Wire0) but now I'm trying to Sync to a DS3231 RTC module on Wire2, while keeping the OLED active on Wire0

I am using several I2C devices:
on Wire0 there is an SSD1306 OLED and a MPU9250 (not in use)
on Wire2 there is the DS3231 RTC with an AT24C32 EEPROM, and a BMP-180 pressure sensor (not in use yet).

The I2C_scanner sketch sees them all AOK (see attached)

My sketch is structured using 13 tabs:
Cels.cpp + Cels.h (for Dallas thermometer on DS1307 module)
EEP.cpp + EEP.h (for 32K EEPROM on DS3231)
OTXT.cpp + OTXT.h (for SSD1306 OLED)
RTC.cpp + RTC.h (for DS1307 or DS3231
xMSC.cpp + xMSC.h (menu and miscellaneous functions)
ZZ.cpp + ZZ.h (global defines)

To tell the MCU the RTC+EEPROM are on Wire2 I added
#define Wire Wire2
to EEP.h and RTC.h

When I run the sketch both the OLED and EEPROM are working, but the RTC fails to retrieve the time.
Happy to provide the whole sketch if need be, but here are the routines that initialise the RTC and read time:
void initRTC(){
  Serial << " * Initialise ";
  setSyncProvider(RTC.get);                         // set RTC as master time source
  if (RTC.chipPresent()){
    if      (RTC_type == 1) {Serial << F("DS1307\n"); RTC_report_NVRAM(); } // report key values from NVRAM
    else if (RTC_type == 2) {Serial << F("DS3231\n"); report_PTC_EEPROM();} // report key values from EEPROM
    else                    {Serial << F("(** <undefined> **)\n"); RTC_type = 0x16;} // unknown RTC type = #F
    start_time = now();                             // save start time
  else {Serial << F(" ** RTC FAIL ** STOP (check circuit)\n"); while(true){};} 

void processTime(){
  timeX = now();                                            // get time from RTC
  if (timeStatus() == timeNotSet)    Serial << F("  Status: timeNotSet\n");
  if (timeStatus() == timeNeedsSync) Serial << F("  Status: timeNeedsSync\n");
  if (RTC_type == 1) timeC = correctRTCtimeForDrift(timeX);
  else               timeC = timeX;                         // DS3231 doesn't calculate or use corrected timeC

void reportDateTime(time_t t, boolean text, boolean reportDrift){ // text: weekday and month as text. drift in secs
  // (t >= 1262304000 && t < 2524608000){                     // if in valid years (1/1/2010 to 31/12/2049)
  //if (t >= 1577836800 && t < 2524608000){                     // if in valid years (1/1/2020 to 31/12/2049)
    Serial << " ";
    Serial << ' ' << '[' << t << ']';
    if (reportDrift) Serial << " drift: " << _FLOAT(drift,1) << " secs";
    Serial << '\n';
  //else Serial << " *** time invalid ***\n";

void report_HHMMSS(time_t t){
  printWithLeadingZero(hour  (t)); Serial << ":";
  printWithLeadingZero(minute(t)); Serial << ":";

void report_MMDDYY(time_t t, boolean text){
  if (text) Serial << dayShortStr(weekday(t)) << " ";
  if (text){
    Serial << monthShortStr(month(t));
    byte RTCshortyear = year(t) - 2000;
  else {
    Serial << "/";
    printWithLeadingZero(month(t));  Serial << "/";

As mentioned all this code works if the RTC + EEPROM are on Wire0 but not on Wire2

BTW does the sequence matter when loading the header files
This how mine are included before running setup():
#include "ZZ.h"                       // global #defines
#include "OTXT.h"                     // OLED
#include "RTC.h"                      // DS1307 or DS3231 RTC
#include "EEP.h"                      // EEPROM (on DS3231 module)
#include "Cels.h"                     // Celsius & Dallas
#include "xMSC.h"                     //


  • T3.6 I2C scan - OTXT RTC BMP.JPG
    T3.6 I2C scan - OTXT RTC BMP.JPG
    70.4 KB · Views: 8
Still looking for help with this ... perhaps if I re-word:

To re-use my proven code with lots of "Wire." entries on Wire2 on my T3.6 I am using:
#define Wire Wire2
but only in the RTC and EEPROM tabs, leaving the SSD1306 OLED on Wire0
This is partiallly working as the code detects the AT24C32 EEPROM on Wire2, but is not retrieving the time from the DS3231 RTC on same I2C bus.
(plus the OLED is working and I2C_scanner detects all the active devices on Wire 0 and Wire2)

I've looked into Timelib and DS1307RTC code but I can't figure out why it doesn't pick up on Wire2.
I suspect the allocation to Wire is first relevant in this instruction:
but I'm really not sure.

On the other hand the issue might simply be due to the order in which I #include the various tabs?