Display.begin fails when Bluetooth added

Hi guys,

First post here so I apologise for any formatting problems, I've read the "READ FIRST" posts so hopefully it's okay.

I'm having an issue with my current Arduino project. I'm using an ATMEGA328p board and I have an 0.96" OLED and BMP180 connected using I2C to pins A4 and A5. I have code that takes the temperature read by the sensor and displays it on the OLED.

#include <SFE_BMP180.h>
#include <Wire.h> //needed for I2C
#include <Adafruit_GFX.h> //needed for OLED screen
#include <Adafruit_SSD1306.h> //needed for OLED screen
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

SFE_BMP180 pressure;

#define ALTITUDE 71 

void setup()
{


  
 Serial.begin(9600);
 if (pressure.begin())
  Serial.println("BMP180 init success");
 else{
  Serial.println("BMP180 init fail\n\n");
  while(1);
 }

  


 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
 {
 Serial.println("failed to access OLED...");
 for(;;);
 }

 delay(500);
 display.clearDisplay(); //clear display buffer
 display.setTextSize(1); //set text size
 display.setTextColor(WHITE); //set colour of font
 display.setCursor(0, 10); //set cursor 0 pixels from left 10 pixels down from top
 // Display static text
 display.println("Hello, EENG3110!"); //print text to display
 display.display(); //this needs to be called to actually update the OLED
}
void loop()
{
 char status;
 double T,P,p0,a;

 status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      Serial.print("Temperature: ");
      Serial.print(T,2);
      Serial.println(" °C");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.println("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,50);
  display.println(T, 2);
  display.setCursor(70,50);
  display.println("C");
  display.setTextSize(1);
  display.setCursor(63,48);
  display.println("o");
  display.display();

  delay(100);
  
}

This works as expected. I added an HC06 Bluetooth module and tested it independently using some default code I found online. It worked as expected also.

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(5, 4); // RX | TX
 
void setup() 
{
    Serial.begin(9600);
    Serial.println("Arduino with HC-06 is ready");
 
    // HC-06 default baud rate is 9600
    BTSerial.begin(9600);  
    Serial.println("BTserial started at 9600");
}
 
void loop()
{
 
  // Keep reading from HC-06 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());
 
  // Keep reading from Arduino Serial Monitor and send to HC-06
  if (Serial.available())
  BTSerial.write(Serial.read());
}

However, when I combined the two, the display.begin fails and gives me the "failed to access OLED..." error as programmed. I have tried both with the Bluetooth code in separate function and with it in the void loop. I've tried so many troubleshooting steps, and even tried replacing the SoftwareSerial library with NeoSWSerial but to no avail. I'm at my wits end so if any of you have any idea what the problem could be, I'd be very grateful. The code I currently have is as follows (I know it's not super clean but it's just temporary whilst I get everything working at once)

#include <Wire.h> //needed for I2C
#include <NeoSWSerial.h>
#include <SFE_BMP180.h>
#include <Adafruit_GFX.h> //needed for OLED screen
#include <Adafruit_SSD1306.h> //needed for OLED screen
#include <Adafruit_BusIO_Register.h> // 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
NeoSWSerial BTSerial(5, 4); // RX | TX
int Temp = 0;

SFE_BMP180 tempsensor;

#define ALTITUDE 71

void setup(){
 BTSerial.begin(9600);
 Serial.begin(9600);
 
 if (tempsensor.begin())
  Serial.println("BMP180 init success");
 else{
  Serial.println("BMP180 init fail\n\n");
  while(1);
 }

  

 //display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
 {
  Serial.println("failed to access OLED...");
  for(;;);
 }

 delay(500);
 display.clearDisplay(); //clear display buffer
 display.setTextSize(1); //set text size
 display.setTextColor(WHITE); //set colour of font
 display.setCursor(0, 10); //set cursor 0 pixels from left 10 pixels down from top
 // Display static text
 display.println("Hello, EENG3110!"); //print text to display
 display.display(); //this needs to be called to actually update the OLED
 
}


void loop()
{
  tempsense();
  bluetooth();  
  
}


void tempsense(){
   char status;
 double T;

 status = tempsensor.startTemperature();
  if (status != 0)
  {
    delay(status);

    status = tempsensor.getTemperature(T);
    if (status != 0)
    {
      Serial.print("Temperature: ");
      Serial.print(T,2);
      Serial.println(" °C");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.println("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,50);
  display.println(T);
  display.setCursor(70,50);
  display.println("C");
  display.setTextSize(1);
  display.setCursor(63,48);
  display.println("o");
  display.display();

  delay(100);
}


void bluetooth(){
  // Keep reading from HC-06 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());
 
  // Keep reading from Arduino Serial Monitor and send to HC-06
  if (Serial.available())
  BTSerial.write(Serial.read());
  
}

Thanks in advance!

The Adafruit_SSD1306 library tries to allocate a 1K screen buffer at runtime when begin() is called. If there isn't 1K of RAM free, you'll get a 0 back from the begin call.

You might investigate a text only SSD1306 library which doesn't use a 1K buffer, like the one below.

if you wish to use Bluetooth with SSD1306 and BMP180 consider moving to a ESP32 which has onboard WiFi, Bluetooth Classic and BLE with plenty of IO facilities, Flash, SRAM etc
works with Arduino IDE and has extensive libraries supporting Bluetooth etc

You should ignore the above. There is nothing wrong with the hardware you have and rushing out to buy an ESP32 will just move the problem without solving it, and add its own complications to boot.

I think your Bluetooth subroutine is junk , waiting for an input that will never come.

You have already proven you know what you are doing with wiring HC-06, so you might try

void bluetooth(){
  BTSerial.println(T);
}

While it might not do any harm, Unos can't do doubles, use a float.

I think v.d. Decken above is noteworthy. The only time I have been able to get SSD1306 to do something actually useful was with text libraries from Adafruit

SSD1306Ascii.h
SSD1306AsciiAvrI2c.h

as you can see here. I don't know if the first is the same library as that above.

I've been using this text library for years. No bluetooth but a number of sensors. Never had an issue. Try the "ZevvPeep8x16.h" font, I've found it the most readable.