Adafruit_SSD1306: how to tell display not connected

Hello,

My 128x64 display works OK. Now I would like to detect that the display ( or e.g. SDA cable) is disconnected. I thought I would achieve that by checking return bool from ::begin ().
However, the code below returns always 'true' (even when SDA not connected)

#include <Adafruit_GFX.h>      // Include LCD 128x64 Adafruit ssd1306
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306          gDisplay         { 128, 64, & Wire, -1 };
static constexpr uint8_t  SCScreenAddress  { UINT8_C ( 0x3C ) };

bool InitialiseDisplay ()
{
	const bool oko { gDisplay.begin ( SSD1306_SWITCHCAPVCC, SCScreenAddress ) };
	if ( true == oko )
	{
		Serial.println ( "All good." );
	}

	return oko;
}

Thank you, Frank

Lokk at 10.1.21 Status register Read: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf

1 Like

It says this:

But I have no clue how to use that info to detect display not present.

"No status read is provided for serial mode" --> there is no way to read information back from the display in serial mode, as the read status register is the only command that would allow it. And you use serial mode. ergo: out of luck.

1 Like

I have a Lilygo ESP32 T-Beam, that normally has an OLED display connected.

I have a socket on the back of the T-Beam case where I can plug in an I2C backpack 20x4 LCD, which is visible in direct sunlight whereas the OLED is not.

The software automatically detects if the I2C backpack 20x4 LCD, on address 0x3F, is connected and if it is uses that display, here is the code;

  Wire.begin(SDA, SCL);

  Wire.beginTransmission(0x3F);
  int16_t error = Wire.endTransmission();

  if (error == 0)
  {
    Serial.println("LCD found");
    USELCD = true;
    USEOLED = false;
  }
  else
  {
    Serial.println("LCD not found.");
    USELCD = false;
    USEOLED = true;
  }

Do the same for the OLED ?

1 Like

Hm, interesting. Thank you for clarification.

I see there are functions Adafruit_SSD1306::drawPixel () and getPixel (). Maybe this pair could be used to see if pixel is set or not ? Or is the display frame buffer in Adruino MCU memory ?

Yes ! I was just reading about i2c and that slave sends ACK so that HW signal could be used.
Thank you for the code, will try that.

1 Like

Having the begin() as the initializer of a local const might cause problems, depending on when the compiler actually calls the function.

Interesting point. My understanding is 'it is called when variable is allocated on the stack and initialized'. Here, before if () is called.

But then what about this. I think okoCC is allocated on the stack before if() is executed as well. And therefore initialised before if () ? No idea. Must be defined somewhere in C++ standard what sequencing is used here.

bool InitialiseDisplay ()
{
	bool oko { gDisplay.begin ( SSD1306_SWITCHCAPVCC, SCScreenAddress ) };
	if ( true == oko )
	{
		Serial.println ( "All good." );
        oko = false;
	}
    const bool okoCC { oko }; // is there a guarantee okoCC to be always 'false' ?
	return oko;
}

Here is some discussion

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.