Cannot combine I2C with the Serial port

Does someone understand why the below code does not work. Basically it's simple I set up an I2C link to display a message on a 64*128 OLED screen and with python I send H and L bytes to blink the connected LED on pin 2 via serial communication on COM6. When I have commented the write_init();, with //, and send H and L via python the LED blinks. When however I uncomment the write_init(); and send the H and L data via python again the LED does not blink anymore ?? Somehow it looks that using the I2C disables the serial communication. I use an Arduino UNO, How can I resolve this ?

/*
Assembly Programmer

created 2021
by Rolf Dubbeld
*/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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)
// The pins for I2C are defined by the Wire-library.
// On an arduino Nano: A4(SDA), A5(SCL)
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL),
// On an Teensy 4.0: 18(SDA), 19(SCL),
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int ledPin = 2; // the pin that the LED is attached to
char incomingByte;

void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{ Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}

write_init();

digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
}

void loop()
{ // see if there's incoming serial data:
if (Serial.available() > 0)
{ // read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') { digitalWrite(ledPin, HIGH); }
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {digitalWrite(ledPin, LOW); }
}
}

void write_init()
{ display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0);
display.println("ASSEMBLY RCVR");
display.display(); delay(1000);
}

I'm thinking about a hardware problem and the I2C bus.

Is something displayed on the OLED ?
Can you add more messages ?
For example

Serial.println( "before write_init");
write_init();
Serial.println( "after write_init");

Could you edit your post and make the sketch look like a sketch, with three backslash-single-quotes ?

```
Your sketch
```

What do you get if you run the i2cScanner sketch? Does it show you device on the i2c bus?

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