SH1106 I2C + RTC1307 + Serial doesn't work together

I just created a simple clock with 2 128x64 oled display (sh1106) + an RTC clock, using:
lib_deps = olikraus/U8g2
paulstoffregen/Time @ ^1.6
adafruit/RTClib @ 1.13.0

Individually, everything works “fine”.
u8g2 + Serial => ok (easy)
2 x u8g2 + Serial => ok (a bit more complex, i2cAddresses need to be left shift or multiply per 2)
RTClib + Serial => ok (easy)
u8g2 + RTClib + Serial => ko

When you add some “logs” (ex: to scan the i2C bus et find the addresses) using Serial.print then it’s a nightmare. Displays are stuck, display things from the other one, rtc clock is not running, … then you add more logs and it’s a dead end.

How I figured out this ? I commented most of the code leaving the display of static text, then I uncommented lines after lines to find the guilty one(s): Serial.print !

I looked at @olikraus code (thanks for the lib) and others looking at TwoWire, Serial, and other Print usages.
RTCLib is the easiest (small). Looks correct, but I don’t like the usage of Wire and all its static things, making easy for libs to interfere…
u8g2 is really rich and requires really more time. So far I just understand the concept but can’t follow and keep all the code in memory.

I haven’t yet found why it collapses only when they are all 3 together (hope my unit test are correct).

I also tried other display and RTC libs but I was limited to one screen or other issues…

Conclusion:

  • Unit tests, Optimistic coding, trust in your code and no Serial.print !
  • Maybe I don’t have a proper usage of Serial, Wire … but I don’t know yet

Go on. You have made no effort to post the constructors. Or attach any code whatsoever,

You have three Slave I2C devices. All should work fine on the same hardware TWI bus.

If you attempt to use software I2C on shared pins there will be tears before bedtime.

Life is much simpler if you post links to hardware (and unusual libraries). And attach / paste relevant code.

David.

You’re right @david_prentice, I should add detailed description of the hardware. But I can’t see why, how hardware could be involved…

Some code (working)

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2a(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2b(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
RTC_DS1307                         rtc;

void setup(void) {
	Serial.begin(57600);
	rtc.begin();
	u8g2a.setI2CAddress(0x3c << 1);
	u8g2b.setI2CAddress(0x3d << 1);
}

void loop(void) {
	delay(1000);    
	rtc.begin();
	DateTime val = rtc.now();
	u8g2a.clearBuffer();
	u8g2a.setFont(u8g2_font_ncenB08_tr);  
	u8g2a.setCursor(20, 10);
	u8g2a.print("screen A");
	u8g2a.sendBuffer();

// where it used to fail.		Serial.print(val.hour(), DEC); 

	u8g2b.clearBuffer();
	u8g2b.setFont(u8g2_font_ncenB08_tr);  
	u8g2b.setCursor(20, 10);
	u8g2b.print("screen B");
	u8g2b.sendBuffer();
}
`

I would expect the compile to fail.

You have two 128x64_xxx_F_ full buffer constructors.
i.e. 1024 bytes twice. When the Nano only has 2048 bytes of SRAM.

You can use small buffers e.g. xxx_1_ constructors. And use the firstPage, nextPage construction.

David.

1 Like

This topic was automatically closed after 120 days. New replies are no longer allowed.