I2C and UART don't work at the same time

I have a vesc motor controller that I am trying to talk to through UART on the RX and TX pins and an OLED 128x64 display I am trying to talk to through I2C. When I try a script only talking to the screen, it works fine. If I run a script only talking to the vesc, it runs fine. But running the following code results in the screen not being about to be initialized.

#include <VescUart.h>
//Screen
#include <Wire.h>
#include <Adafruit_SSD1306.h>

VescUart UART;
Adafruit_SSD1306 display(128, 64);

void setup() {
  Serial.begin(115200);
  while (!Serial) {;}
  UART.setSerialPort(&Serial);
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 allocation failed");     //code get stuck here when run
    for(;;);
  }
  
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("Loading...");
  delay(1000);
  
}

void loop() {
  if ( UART.getVescValues() )  {
    Serial.println(UART.data.rpm);
    Serial.println(UART.data.inpVoltage);
    Serial.println(UART.data.ampHours);
    Serial.println(UART.data.tachometerAbs);
    
  } else {
    Serial.println("Failed to get data");
  }
  display.display();
  delay(50);
  display.clearDisplay();
}

Please post a link to this, and a wiring diagram, with pins, connections and parts clearly labeled. Hand drawn is fine.

Please state which Arduino you are using.

Since the documentation is locked unless you have an account, you are kind of on your own. Look at your documentation/program code listing and see if they turn interrupts of and on.

1 Like

This is the motor controller.

This is a symptom of running out of dynamic memory. The screen takes half of it on a Classic Nano. The text-only SSD1306 library will solve that problem (no graphics).