Problem with Dual 128 x 65 OLED displays via Adafruit SSD1306, I2C and NANO

I am trying to run 2no. 128 x 64 OLED displays from a NANO using I2C

I have a generic NANO clone and the usual cheap white OLEDS purchased from Amazon.

I’ve managed to do everything I wanted with one display.

When I added a second the second display would not display (I have moved the resistor on one and have confirmed the I2C addresses). If I comment out one display the other works correctly but when both are supposed to be active only the first one (whichever order I put them in) displays.

I’ve spent many hours browsing the internet and YouTube and I didn’t seem to be doing anything wrong.

In the end I found a piece of test code that someone else had written and tried that - it worked but I could not see why it was different. However, the code had set screen height to 32 but when I set it to 64 I was back to the same problem!

Code attached:

#include <Wire.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // Default OLED address, usually
  display2.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Second OLED address, via onboard jumper
  


display.clearDisplay();
display.display();

display2.clearDisplay();
display2.display();

  delay(1000);
  
  display.clearDisplay();
  display.setTextSize(1); 
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Display A");
  display.display();

  delay(1000);
  
  
  display2.clearDisplay();
  display2.setTextSize(1); 
  display2.setTextColor(WHITE);
  display2.setCursor(0,0);
  display2.print("Display B");
  display2.display();
}

void loop() {
  }

I’ve also tried several boards and get the same result.

Is there a way around this?

Thanks in advance.

Each Adafruit_SSD1306 object allocates 1024 bytes for a buffer at runtime. Your Nano has only got 2048 bytes.

If you want two SSD1306 with a Nano / Uno, use U8g2lib library with 1 constructors and page loop.

David.

Thanks

I suspected it might be something like that.

I'm presuming the 1024 allocation is only for the 128 x 64 display then, as it works with the 128 x 32?

I could dig an UNO out but from memory, the UNO has the same dynamic memory?

I guess I'll have to look at the U8g2lib library.

Oops. 128x32 is 512 bytes. So two 128x32 should be fine with Adafruit_SSD1306.

You do not seem to have many wasteful anonymous strings in SRAM. Nor any big arrays.
I would expect your sketch to work.

David.

Two 128 X 32 are fine.

It’s only when I define as 64 that it falls over.

Strangely though, when I comment out one of the sets of print statements it prints the other but I’ve still declared both displays so wouldn’t it have allocated the memory?

Then again, I thought both displays would use the same memory allocation for the buffer as you have to use clear display() between each display write so it doesn’t write the same thing to the second display?

Excuse the potentially daft questions - I’m new to this.

Only you know what you want to do.

You could use a single Adafruit_SSD1306 object but select the relevant

    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // display #1
    display.clearDisplay();
    ...
    display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // display #2
    display.clearDisplay();
    ...

the begin() method() will take a significant time. It would be better to buy a BluePill or Nano Every for more SRAM.

Or to use U8g2lib with paged constructors.

U8g2lib is excellent for Font support. But you can use the U8g2lib_for_Adafruit_GFX class to get the benefits of GFX e.g. with ARM or ESP.

David.

Thanks again.

It's all new to me so I need to think about the options.

Creating and changing a single object being slow isn't a problem, I'm only selecting menu options and switching pumps.

I'll have a look at U8g2lib as well.