Adafruit_SSD1306 and Wire serial communication

Hi -

I'm kind of new to i2c but I have a need to have 2 Arduinos talk to each other so I was thinking of using the i2c. Wired up the master and slave and loaded the example sketches and all works fine.

I then wanted to add a oled screen to the slave for some features. The screen seems to work but the communication between the 2 Arduinos stopped working. I know Adafruit_SSD1306 uses i2c internally so there may be something I need to make this work

MASTER :

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

SLAVE: ( If you uncomment the code that starts up the display the master stops getting data. )

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoBLE.h>
#include <WiFiNINA.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET     -1
#define SCREEN_ADDRESS_RIGHT 0x3D

Adafruit_SSD1306 left_display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(9600);
  delay(2000);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event

//  if (!left_display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS_RIGHT)) {
//   Serial.println(F("SSD1306 allocation failed"));
//    for (;;); // Don't proceed, loop forever
 // }

 // left_display.display();
   
}

void loop() {
  delay(100);
}

void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

..which then became a master?

Please remember to use code tags when posting code

Thanks .. so I take it the way the oled lib works it needs to control the bus as a master .. so there is no way to make this work ..

I have to confess, I've never tried to change the rôle of a slave, but it's unlikely to be an OLED library problem

Some of the MCU's have multiple SPI and I2C buses. An ESP32 has 2 user accessible SPI buses and 2 I2C buses. One I2C bus can be the master that does the thing with the display and the other I2C bus can do the slave thing.

Life is simpler if one Arduino is always Master. And the other is always a Slave.
The Master "speaks" to the Slave whenever the she wants. The Slave does as he is told.
The Master can periodically ask the Slave if the Slave wants to talk to her.

The OLED is always going to be a Slave. The Master is in control of the OLED.

Yes, it is possible for the Slave to "become" a Master in a "Multi-Master" environment. But this is more trouble than it is worth.
Think about the real world. Life is "smoother" if you avoid fights and just do as you are told.

David.

Thanks .. I think I will need to re-think my approach. Perhaps use SPI or another display to get around this issue.

You can communicate via SPI, USART, I2C, ...
It does not really matter which.

But it is quite convenient to do everything on a single I2C bus. With one Arduino as the Master.

Think about driving a car. One person is at the steering wheel but can always ask passengers for where to go.
If you exchange seats while driving on the motorway it is possible but a bit hazardous.

David.

1 Like

Unless of course the car drives itself :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.