Go Down

Topic: Problem initialising I2C OLED display with MCP2515 CAN transceiver (Read 95 times) previous topic - next topic

iiminov

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.

Code: [Select]

#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 pinNano pin
GNDGND
VDD3.3V
SCKA5
SDAA4

MCP2515 pinNano pin
VCC5V
GNDGND
CSD10
CSD11
SOD12
SID13
INTNC

pylon

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.

iiminov

Quote from: 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.

pylon

Quote
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.

Quote
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.

Go Up