Problem initialising I2C OLED display with MCP2515 CAN transceiver

Having a bit of a problem with Adafruit SSD1306 library not being able to initialise I2C OLED display when I include while (CAN_OK != CAN.begin(CAN_500KBPS)) line for initialising MCP2515 CAN transceiver in the setup() function.

#include <stdio.h> // for function sprintf
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "mcp_can.h"

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

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

long unsigned int rxId;
unsigned long rcvTime;
const int SPI_CS_PIN = 10;

MCP_CAN CAN(SPI_CS_PIN);

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

	if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
		Serial.println(F("SSD1306 allocation failed"));
		for (;;); // on error loop forever
	}

	// show splash screen initialised by Adafruit library
	display.display();
	delay(2000);

	ClearDisplayBufferAndInitText();

	while (CAN_OK != CAN.begin(CAN_500KBPS))              // init can bus : baudrate = 500k
	{
		ClearDisplayBufferAndInitText();
		display.println("CAN BUS module failed to initialized");
		display.println("retrying....");
		display.display();
		delay(200);
	}

	ClearDisplayBufferAndInitText();
	display.println("CAN BUS module initialized!");
	display.display();
	delay(2000);
} // end of setup()


void loop() {
	ClearDisplayBufferAndInitText();

	display.println("read & display CAN bus massages");
	delay(2000);
} // end of loop()

void ClearDisplayBufferAndInitText() {
	display.clearDisplay(); // clear display buffer

	// init text properties
	display.setTextSize(1);      // normal 1:1 pixel scale
	display.setTextColor(WHITE); // draw white text
	display.setCursor(0, 0);     // start at top-left corner
	display.cp437(true);         // use full 256 char 'Code Page 437' font
}

I have confirmed that the OLED display can initialise and display stuff.

I have confirmed that the CAN transceiver can initialise and print stuff to serial monitor.

But when I try to put them together I get the error that OLED failed to initialise and the code goes into an infinite loop.

Can someone throw their eye over the code and let me know if some obvious mistake that I fail to see.

Just to add to the confusion I am using Arduino Nano connecting modules as follows:

OLED pin Nano pin
GND GND
VDD 3.3V
SCK A5
SDA A4
MCP2515 pin Nano pin
VCC 5V
GND GND
CS D10
CS D11
SO D12
SI D13
INT NC

The Adafruit library for the SSD1306 uses much memory (RAM). You can get it to work with ATmega328p based boards (as the Nano is) but only if you use almost no other code, especially not code that uses much RAM. So your combination is probably too much for the small processor. Change to a Mega2560 or something similar with has more RAM.

pylon:
your combination is probably too much for the small processor

I've been reading up on posts here regarding I2C and SPI but found no conclusive evidence that the two can't be used together. In fact, one post specifically dealt with I2C and SPI working together.

I've experienced a similar problem when I tried to use the MPU6050 Gyro+Accelerometer module to output some values on the screen. But I have seen clips on YouTube of people having Nano + I2C LCD display working with the MPU6050. You could be on to something there.

I will check to see if I am running into memory shortage issue and revert back as soon as I have results.

Thanks for the tip.

I've been reading up on posts here regarding I2C and SPI but found no conclusive evidence that the two can't be used together. In fact, one post specifically dealt with I2C and SPI working together.

Using I2C and SPI in the same sketch is absolutely no problem.

I've experienced a similar problem when I tried to use the MPU6050 Gyro+Accelerometer module to output some values on the screen. But I have seen clips on YouTube of people having Nano + I2C LCD display working with the MPU6050. You could be on to something there.

The same type of display? It really depends on the hardware and the used libraries.

@ iiminov: Did you ever find a solution for this? I have the same problem.

@ iiminov: Did you ever find a solution for this? I have the same problem.

Did you read my answer #1? Do you still think this hardware configuration is the right choice (I'm assuming you're using the exact same hardware as you're stating to have the same problem without describing differences)?

I have the same problem.
I have confirmed that the OLED display can initialise and display stuff.

I have confirmed that the CAN transceiver can initialise and print stuff to serial monitor.

But when I try to put them together, the oled cannot running display and MCP2515 canbus no received the message

the code is below

#include <splash.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "mcp_can.h"

Adafruit_SSD1306 display(-1);

/SAMD core/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif

const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);

int x = 0;
int a01 = 0;
int a02 = 0;
int a03 = 0;
int b01 = 0;
int c01 = 0;
int c02 = 0;
int d01 = 0;
int d02 = 0;
int e01 = 0;
int f01 = 0;
int g01 = 0;
int g02 = 0;

const byte interruptPin = 0;
unsigned char flagRecv = 0;
char str[20];
unsigned long id = 0;

void setup()
{
Serial.begin(250000);
/*
while (CAN_OK != CAN.begin(CAN_500KBPS))
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
SERIAL.println("CAN BUS Shield init ok!");

attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt

CAN.init_Mask(0, 1, 0x10f120e0);
CAN.init_Mask(1, 1, 0x10f120e0);

CAN.init_Filt(0, 1, 0x10f120ef);
SERIAL.println("FILT OK");

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("ST6ON56iii TECH Co.");
display.setCursor(0, 20);
display.println("PD65L");
display.drawRect(28, 18, 95, 10, WHITE);
display.display();
display.setCursor(0, 36);
display.println("DjTQ");
display.drawRect(28, 36, 95, 10, WHITE);
display.display();
display.setCursor(0, 52);
display.println("SPhE");
display.drawRect(28, 52, 95, 10, WHITE);
display.display();

}

void MCP2515_ISR()
{
flagRecv = 1;
}

void loop() {
unsigned char len = 0;
unsigned char buf[9];

if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);

a01 = buf[0];
b01 = buf[1];
c01 = buf[2];
d01 = buf[3];
e01 = buf[4];
f01 = buf[5];

a02 = a01 - 128; //offset -128

d02 = map(d01, 0, 255, 0, 100);

g01 = (f01 * 256) + e01;
g02 = g01 * 0.01;

SERIAL.print(g02, HEX);
SERIAL.print(a02, HEX);
SERIAL.print(d02, HEX);

display.fillRect(28, 18, g02, 10, WHITE);
display.fillRect(28, 36, a02, 10, WHITE);
display.fillRect(28, 52, d02, 10, WHITE);
display.display();
}
delay(20);
// put your main code here, to run repeatedly:
}

I have the same problem.

And you didn't read the thread as the poster above you did.

You failed to set code tags, so please edit your post and insert them to make the code readable. Don't expect us to have a look at it without code tags.