Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Adafruit_SSD1306 - Problems to allocate 2nd display
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit_SSD1306 - Problems to allocate 2nd display

by msantos_eca on Tue Aug 04, 2020 2:37 pm

Hello,

I am trying to develop a app with 2 Oled SSD1306 7 pins.
However, I had had problem to allocate the second display when run the setup fucntion.
Here is the code
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>                     // requried to run I2C SSD1306
#include <SPI.h>                      // requried to run I2C SH1106
#include <Adafruit_GFX.h>             // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_SSD1306.h>         // https://github.com/adafruit/Adafruit_SSD1306


#define OLED1_SCK     2
#define OLED1_SDA     3
#define OLED1_RESET   4                  // Both Displays
#define OLED1_DC      5
#define OLED1_CS      6

// OLED - Display 02
#define OLED2_SCK     7
#define OLED2_SDA     8
#define OLED2_RESET   9                  // Both Displays
#define OLED2_DC      10
#define OLED2_CS      11


Adafruit_SSD1306 display1(128, 64, OLED1_SDA, OLED1_SCK, OLED1_DC, OLED1_RESET, OLED1_CS);
Adafruit_SSD1306 display2(128, 64, OLED2_SDA, OLED2_SCK, OLED2_DC, OLED2_RESET, OLED2_CS);


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

  bool flag = true;
  flag = display1.begin(SSD1306_SWITCHCAPVCC, 0x3C);                // needed for SH1106 display1
  if(flag == false){
    Serial.println("[1] fails ");
  }
  flag = display2.begin(SSD1306_SWITCHCAPVCC, 0x3D);
  if(flag == false){
    Serial.println("[2] fails ");
  }
  display1.clearDisplay();                                   
  display2.clearDisplay();                                 
}



The variable "flag = display2.begin(SSD1306_SWITCHCAPVCC, 0x3D);" returns "false" everytime.

Please, could you help me?

Regards,

Marcelo.

msantos_eca
 
Posts: 10
Joined: Tue Aug 04, 2020 1:26 pm

Re: Adafruit_SSD1306 - Problems to allocate 2nd display

by dastels on Tue Aug 04, 2020 4:06 pm

I'm not clear why you're using two SPI buses. Typically you'd use one set of SCLK, MISO, and MOSI (as required) and separate CS (and bust input in this case) for each device.

Regarding the error, it looks like you're mixing SPI and I2C usage of the library.

Have a look at the tutorail, specifically the C++ usage page: https://learn.adafruit.com/monochrome-oled-breakouts/arduino-library-and-examples

Dave

dastels
 
Posts: 3518
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit_SSD1306 - Problems to allocate 2nd display

by msantos_eca on Tue Aug 04, 2020 5:07 pm

Hi Dave,

Thanks for your response.

I'am trying to build a stero VU meter. In the display "1" I'd like to show an information and another in display "2".

However, it fails to allocate display 2.

I Used your tip...
Here is the mininum code:
Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

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

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   2
#define OLED_CLK    3
#define OLED_DC     4
#define OLED_CS     5
#define OLED_RESET  6
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define OLED1_CS    10
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED1_CS);



void setup() {
  Serial.begin(9600);
  if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
    Serial.println(F("SSD1306 allocation failed [2]"));
    for(;;); // Don't proceed, loop forever
 }
 
  display1.clearDisplay();
  for(int16_t i=0; i<display1.height()/2-2; i+=2) {
    display1.drawRoundRect(i, i, display1.width()-2*i, display1.height()-2*i,
      display1.height()/4, SSD1306_WHITE);
    display1.display();
    delay(1);
  }

  display2.clearDisplay();
  for(int16_t i=0; i<max(display2.width(),display2.height())/2; i+=2) {
    display2.drawCircle(display2.width()/2, display2.height()/2, i, SSD1306_WHITE);
    display2.display();
    delay(1);
  }

}

void loop() {
}


But it still fails to initialize display "2".

Do you have some idea?
Regards,

Marcelo

msantos_eca
 
Posts: 10
Joined: Tue Aug 04, 2020 1:26 pm

Re: Adafruit_SSD1306 - Problems to allocate 2nd display

by dastels on Tue Aug 04, 2020 5:57 pm

For SPI, the calls to begin just need the first argument... not the address. It's ignored so you can just let it default to 0. It will make your code clearer.

What controller are you using?

Can you past the result of compiling (specifically the memory amounts)?

Dave

dastels
 
Posts: 3518
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit_SSD1306 - Problems to allocate 2nd display

by msantos_eca on Tue Aug 04, 2020 6:06 pm

I am using Arduino NANO ATMega328p.

Here is the memory information:
Code: Select all | TOGGLE FULL SIZE
Sketch uses 15098 bytes (49%) of program storage space. Maximum is 30720 bytes.
Global variables use 557 bytes (27%) of dynamic memory, leaving 1491 bytes for local variables. Maximum is 2048 bytes.


I changed for only "begin" and the result is the same.

msantos_eca
 
Posts: 10
Joined: Tue Aug 04, 2020 1:26 pm

Re: Adafruit_SSD1306 - Problems to allocate 2nd display

by dastels on Tue Aug 04, 2020 8:19 pm

OK, that explains it.

Each display object allocates 1K of "dynamic" memory. So the first one grabs its 1024 bytes, leaving 467 bytes... so the second one can't alloc the 1024 bytes it wants. You need a controller with more RAM (aka dynamic memory) in order to use 2 displays.

Dave

dastels
 
Posts: 3518
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit_SSD1306 - Problems to allocate 2nd display

by msantos_eca on Tue Aug 04, 2020 8:27 pm

Thanks, Dave.

I changed with U8GLIB_SH1106_128X64 and it has worked.

Regards.

msantos_eca
 
Posts: 10
Joined: Tue Aug 04, 2020 1:26 pm

Please be positive and constructive with your questions and comments.