ESP32 2 I2C buses

tanguy-e
Posts: 4
Joined: Tue May 16, 2023 11:17 am

ESP32 2 I2C buses

Postby tanguy-e » Tue Feb 27, 2024 1:52 pm

Hello, i'm trying to use 1306 OLED with a I2C bus and a BMP280 sensor with another one :

Code: Select all

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include "SSD1306Wire.h" 


#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
SSD1306Wire display(0x3c, SDA_OLED, SCL_OLED);


Adafruit_BMP280 bmp(&I2Ctwo); // I2C


void setup() {
  Serial.begin(115200);
  I2Ctwo.begin(SDA, SCL);
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(0, 0, "Left aligned (0,10)");
  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  //status = bmp.begin();
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure()/100);
    Serial.println(" hPa");

    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
    Serial.println(" m");

    Serial.println();
    delay(2000);
}
It leads to a kernel panic but i can't understant whta i'm doing wrong.
Any help ?
Thanks

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 2 I2C buses

Postby lbernstone » Wed Feb 28, 2024 6:11 am

You can decode the backtrace
You are defining the TwoWire objects incorrectly. They are class instances, not variables. You would call them like:

Code: Select all

TwoWire Wire(0);
But really, there's no need. Wire and Wire1 are already defined for you. Call Wire.begin(sda, scl) to initialize them in your setup.

tanguy-e
Posts: 4
Joined: Tue May 16, 2023 11:17 am

Re: ESP32 2 I2C buses

Postby tanguy-e » Wed Feb 28, 2024 9:51 am

Thank you. So i modify the beginning of the software :

Code: Select all

SSD1306Wire display(0x3c, SDA_OLED, SCL_OLED);
Adafruit_BMP280 bmp(&Wire1); // I2C

void setup() {
  Serial.begin(115200);
  Wire1.begin(SDA, SCL);
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(0, 0, "Left aligned (0,10)");
I suppose SSD1306Wire display(0x3c, SDA_OLED, SCL_OLED) manage the Wire setup and i just need to manage Wire1 one for BMP280 but this leads to kernel panic too. Maybe i have to pass &Wire to SSD1306Wire and begin it in the setup but i can't see how to do this ...

MicroController
Posts: 1688
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 2 I2C buses

Postby MicroController » Wed Feb 28, 2024 11:49 pm

use 1306 OLED with a I2C bus and a BMP280 sensor with another one
May be worth noting that the two can happily live together on a single I2C bus.

tanguy-e
Posts: 4
Joined: Tue May 16, 2023 11:17 am

Re: ESP32 2 I2C buses

Postby tanguy-e » Thu Feb 29, 2024 10:18 am

Yes it couold be but it's not possible. I use a WiFi LoRa 32(V3) board and the OLED is hard solded and the connexion are not accessible from the outside. So i need to have a bus for the onboard OLED and another one for the external sensor ...

Who is online

Users browsing this forum: Baidu [Spider] and 40 guests