Interference on I2C bus

Hello erveryone,

I have a problem with my project. Im using a Lilygo TTGO Lora32 V1.6.1 board with intergrated OLED and LoRa chip. i've added an BME280 sensor to the project.

The problem that im running into is that when i add the BME280 sensor on the I2C bus that de OLED screen only shows static. I can't reset it. Only when is remove the power from the board.

I will add the code below. Note that some functions are commented out. This are future functions.

/*********
Lilygo TTGO LoRa32 V1.6.1 With BME280.

Future:
- Ultrasonic Sensor
- MCP23017 I2C IO Expander
*********/

//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>

//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Libraries for BME280
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23
#define DIO0 26

//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 433E6

//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22 
#define OLED_RST 13
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

//BME280 definition
#define SDA 21
#define SCL 22

TwoWire I2Cone = TwoWire(1);
Adafruit_BME280 bme;

//packet counter
int readingID = 0;

int counter = 0;
String LoRaMessage = "";

float temperature = 0;
float humidity = 0;
float pressure = 0;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);


//int ultraSonicTrig = 4;
//int ultraSonicEcho = 36;
//long duration, cm;

//Initialize OLED display
void startOLED(){
  //reset OLED display via software
  pinMode(OLED_RST, OUTPUT);
  digitalWrite(OLED_RST, LOW);
  delay(20);
  digitalWrite(OLED_RST, HIGH);

  //initialize OLED
  Wire.begin(OLED_SDA, OLED_SCL);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  //
  
  
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("LORA SENDER");
}

//Initialize LoRa module
void startLoRA(){
  //SPI LoRa pins
  SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  LoRa.setPins(SS, RST, DIO0);

  while (!LoRa.begin(BAND) && counter < 10) {
    Serial.print(".");
    counter++;
    delay(500);
  }
  if (counter == 10) {
    // Increment readingID on every new reading
    readingID++;
    Serial.println("Starting LoRa failed!"); 
  }

    // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xFF);

  Serial.println("LoRa Initialization OK!");
  display.setCursor(0,10);
  display.clearDisplay();
  display.print("LoRa Initializing OK!");
  display.display();
  delay(2000);
}

void startBME(){
  I2Cone.begin(SDA, SCL, 100000); 
  bool status1 = bme.begin(0x76, &I2Cone);  
  if (!status1) {
    Serial.println("Could not find a valid BME280_1 sensor, check wiring!");
    while (1);
  }
}

void getReadings(){
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;

  /**
  // The sensor is triggered by a HIGH pulse of 20 or more microseconds.
      // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(ultraSonicTrig, LOW);
  delayMicroseconds(5);
  digitalWrite(ultraSonicTrig, HIGH);
  delayMicroseconds(20);
  digitalWrite(ultraSonicTrig, LOW);
    
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(ultraSonicEcho, INPUT);
  duration = pulseIn(ultraSonicEcho, HIGH);
    
  // Convert the time into a distance
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343

  **/
}

void sendReadings() {
  LoRaMessage = String(readingID) + "/" + String(temperature) + "&" + String(humidity) + "#" + String(pressure);
  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print(LoRaMessage);
  LoRa.endPacket();
  
  display.clearDisplay();
  display.setCursor(0,0);
  display.setTextSize(1);
  display.print("LoRa packet sent!");
  display.setCursor(0,20);
  display.print("Temperature:");
  display.setCursor(72,20);
  display.print(temperature);
  display.setCursor(0,30);
  display.print("Humidity:");
  display.setCursor(54,30);
  display.print(humidity);
  display.setCursor(0,40);
  display.print("Pressure:");
  display.setCursor(54,40);
  display.print(pressure);
  display.setCursor(0,50);
  display.print("Reading ID:");
  display.setCursor(66,50);
  display.print(readingID);
  display.display();
  Serial.print("Sending packet: ");
  Serial.println(readingID);
  readingID++;
}

void printReadings(){

  Serial.print("T: ");
  Serial.print(temperature);
  Serial.print(", H: ");
  Serial.print(humidity);
  Serial.print(", P: ");
  Serial.print(pressure);
  //Serial.print(", D: ");
  //Serial.print(cm);
  //Serial.print("cm");
  Serial.println();
}


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);;
  startOLED();
  startBME();
  startLoRA();

  //pinMode(ultraSonicTrig, OUTPUT);
  //pinMode(ultraSonicEcho, INPUT);
  
}
void loop() {
  getReadings();
  sendReadings();
  printReadings();
  delay(10000);
}

Note that the I2C pins are defined twice. This is because the 2 libraries both call them different.

Thankyou for your help!

Rick

I suspect that is your problem. You might need a board with two I2C bus's

1 Like

It is important to use the most simple and most straightforward code that everyone else uses. Can you use the I2C bus in the most common way ?

Please remove this:

TwoWire I2Cone = TwoWire(1);

That was maybe needed for a short time many years ago.
Just use Wire and Wire1.


Use the default for Wire.begin(), don't use the parameters for the pin numbers:

Wire.begin();

Call that in setup().


Remove this:

//BME280 definition
#define SDA 21
#define SCL 22

They are already defined.

Remove this as well:

#define OLED_SDA 21
#define OLED_SCL 22
2 Likes

wow this is it. thanks for your help!

i2c requires one pull-up resistor oneach signal. do you have them?

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