Using TwoWire I2C2 with esp32 & two pressure sensors

Hi,

I am using ESP32 , and 2 pressure sensors.
I succeed getting the data from each sensor separately.
As they have the sam I2c address, I am trying to call I2c1 & I2c2.
When I call i2c2 - I get the error message
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
if I delete this line: I2C2.begin(I2C2_SDA2, I2C2_SCL2,100000);
all is working great ( from one sensor)

Thanks:::

here is my code:

#include <Wire.h> // so we can use I2C communication

const int SENSORADDRESS = 0x28; // ELVH-L01D-HRRD-C-N2A4 address from the datasheet

#define I2C1_SDA1 21

#define I2C1_SCL1 22

#define I2C2_SDA2 19

#define I2C2_SCL2 18

#define SENSOR_CONTROL_REG_1 0x28

#define SENSOR_DR_STATUS 0x00 // Address of DataReady status register

#define SENSOR_OUT_P_MSB 0x01 // Starting address of Pressure Data registers

byte I2Cdata[4] = {0,0,0,0}; //buffer for sensor data 32 bits

byte I2Cdata2[4] = {0,0,0,0}; //buffer for sensor data 32 bits

TwoWire I2C1 = TwoWire(0); //I2C1 bus

TwoWire I2C2 = TwoWire(1); //I2C2 bus

void setup(){

Serial.begin(9600);

I2C1.begin(I2C1_SDA1, I2C1_SCL1, 100000); // Start I2C1 on pins 21 and 22

I2C2.begin(I2C2_SDA2, I2C2_SCL2,100000);

I2C_Write1(SENSORADDRESS, 0100000000); // put in start mode

//I2C_Write2(SENSORADDRESS, 0100000000); // put in start mode

I2C_Write1(SENSORADDRESS, 0b00111000); // set oversampling to 128

//I2C_Write2(SENSORADDRESS, 0b00111000); // set oversampling to 128

}

void loop(){

float pressure;

Read_Sensor_Data1();

//Read_Sensor_Data2();

delay(500);

}

// Read the pressure and temperature readings from the sensor

void Read_Sensor_Data1(){

I2C_Write1(SENSOR_CONTROL_REG_1, 0b00111010); //bit 1 is one shot mode

do {

// Serial.println("DO Inside read sensordata.");

I2C1.requestFrom(SENSORADDRESS,1);

} while ((I2C1.read() & 0b00000010) != 0);

//Serial.println("While Inside read sensordata.");

I2C_ReadData1(); //reads registers from the sensor

}

// Read Pressure data (4 bytes)

void I2C_ReadData1(){

byte readUnsuccessful;

//Serial.println("void I2C_ReadData!!!!");

do {

byte I=0;

byte dataStatus = 0;

I2C1.beginTransmission(SENSORADDRESS);

I2C1.write(SENSOR_OUT_P_MSB);

I2C1.endTransmission(false);

// read 4 bytes. 2 for pressure, 2 for temperature.

I2C1.requestFrom(SENSORADDRESS,4);

while(I2C1.available()) {I2Cdata[i++] = I2C1.read();

Serial.print("sensor1 = ");

Serial.println(I2Cdata[1]);

delay(100);

}

//Serial.println(readUnsuccessful);

readUnsuccessful=1;

} while (readUnsuccessful);

//Serial.println("readUnsuccessful"[I]);

}

void I2C_Write1(byte regAddr, byte value){

I2C1.beginTransmission(SENSORADDRESS);

I2C1.write(regAddr);

I2C1.write(value);

I2C1.endTransmission(true);

}

many i2c devices have some pads on the board to select a different address.

post the sensor model

Thanks -
The sensor is - ELVH-L01D-HRRD-C-N2A4
And the address can't be changed

then you can't use more than one sensor without elaborate hardware tricks

The Espressif documentation says ESP32 has two I2C ports. Have you read THIS?

Yes, That is why I thought I can use the I2c even though the sensors has the same address.
But when I use the code - it does not work for me.

Have you run the core dump data through the ESP32 Exception decoder?

But you can buy versions with different I2C Addresses:

I didn't - Thanks - I will try the core dump

Welcome to the forum.

Things are improving all the time. You don't have to use these anymore:

TwoWire I2C1 = TwoWire(0);

They are already in Wire.cpp here.

You can just use 'Wire' and "Wire1'.
If you want, then you can assign the pin numbers:

Wire1.begin( 19, 18);

But you can also use the default pins. I don't know the default pins for Wire1 :woozy_face: so I suggest you assign the pin numbers for Wire1.

This sketch scans both I2C buses:

#include <Wire.h>

void setup() 
{
  Serial.begin(115200);

  Wire.begin();             // default: 21, 22
  Wire1.begin( 19, 18);     // I don't know the default pins !

  Serial.println("---------- Scanning Wire -------------");
  I2C_ScannerWire();

  Serial.println("---------- Scanning Wire1 ------------");
  I2C_ScannerWire1();
}

void loop() { delay(10); }

void I2C_ScannerWire()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
}

void I2C_ScannerWire1()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    Wire1.beginTransmission(address);
    error = Wire1.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
}

You can try the sketch in Wokwi simulation:

ran @Koepel scanner from post #10 on an ESP32 with a LMS9DS1 9-axis iNEMO inertial module on Wire1 and a BMP280 pressure sensor on Wire results

14:44:44.491 -> ---------- Scanning Wire -------------
14:44:44.491 -> Scanning...
14:44:44.525 -> I2C device found at address 0x76  !
14:44:44.525 -> done
14:44:44.525 -> 
14:44:44.525 -> ---------- Scanning Wire1 ------------
14:44:44.525 -> Scanning...
14:44:44.525 -> I2C device found at address 0x1C  !
14:44:44.525 -> I2C device found at address 0x6A  !
14:44:44.525 -> done
14:44:44.525 -> 

running of the devices

14:51:04.717 -> LSM9DS1 data read demo
14:51:04.717 -> initI2C
14:51:04.717 -> Found LSM9DS1 9DOF
14:51:04.854 -> 
14:51:04.854 -> Sensor type: BMP280
14:51:04.854 -> 
14:51:04.854 -> Temperature: 25.12 C
14:51:04.854 -> Pressure:    1018.90 hPa
14:51:04.854 -> Altitude:    60.62 m
14:51:04.854 -> 
14:51:05.837 -> 0x08 0x00 0x0e 0x01 0x1a 0x3f 0x61 0xfd 0xb9 0x05 0x5e 0xff 0xe7 0xff 0xa6 0x00 0xf9 0xfe 
14:51:05.871 -> Accel 0x0008 0x010e 0x3f1a  Mag   0xfd61 0x05b9 0xff5e  Gyro  0xffe7 0x00a6 0xfef9
14:51:05.871 -> Acceleration X:  0.00 m/s^2 Y:   0.16 m/s^2 Z:  9.66 m/s^2 
14:51:05.871 -> magnetic X: -0.09 m/s^2 Y:   0.21 m/s^2 Z: -0.02 m/s^2 
14:51:05.871 -> Gyroscope X: -0.22 m/s^2 Y:   1.45 m/s^2 Z: -2.30 m/s^2 
14:51:05.871 -> 
14:51:05.871 -> Temperature: 25.07 C
14:51:05.871 -> Pressure:    1018.71 hPa
14:51:05.906 -> Altitude:    62.16 m
14:51:05.906 -> 
14:51:06.888 -> 0xde 0xff 0x0a 0x01 0x60 0x3f 0x2b 0xfd 0xd1 0x05 0xa4 0xff 0xf6 0xff 0xcc 0x00 0xee 0xfe 
14:51:06.888 -> Accel 0xffde 0x010a 0x3f60  Mag   0xfd2b 0x05d1 0xffa4  Gyro  0xfff6 0x00cc 0xfeee
14:51:06.922 -> Acceleration X: -0.02 m/s^2 Y:   0.16 m/s^2 Z:  9.71 m/s^2 
14:51:06.922 -> magnetic X: -0.10 m/s^2 Y:   0.21 m/s^2 Z: -0.01 m/s^2 
14:51:06.922 -> Gyroscope X: -0.09 m/s^2 Y:   1.78 m/s^2 Z: -2.40 m/s^2 
14:51:06.922 -> 
14:51:06.922 -> Temperature: 25.07 C
14:51:06.922 -> Pressure:    1018.73 hPa
14:51:06.922 -> Altitude:    61.96 m

Thanks!!!!
Now it is working!
What a great forum

How did you calculate the pressure? from the data received ?

yes - I used libraries ErriezBMX280, Adafruit_BMP280 and Adafruit_BME280_Library
some samples results using ErriezBMX280

18:41:47.630 -> Erriez BMP280/BMX280 example
18:41:47.731 -> 
18:41:47.731 -> Sensor type: BMP280
18:41:47.731 -> 
18:41:47.731 -> Temperature: 22.31 C
18:41:47.731 -> Pressure:    1021.17 hPa
18:41:47.764 -> Altitude:    41.81 m
18:41:47.764 -> 
18:41:48.739 -> Temperature: 22.30 C
18:41:48.739 -> Pressure:    1021.07 hPa
18:41:48.739 -> Altitude:    42.68 m

also works for a BME280

18:47:06.421 -> Erriez BMP280/BMX280 example
18:47:06.559 -> 
18:47:06.559 -> Sensor type: BME280
18:47:06.559 -> 
18:47:06.559 -> Temperature: 22.18 C
18:47:06.559 -> Humidity:    46.69 %
18:47:06.559 -> Pressure:    1021.06 hPa
18:47:06.559 -> Altitude:    42.76 m
18:47:06.559 -> 
18:47:07.550 -> Temperature: 22.23 C
18:47:07.550 -> Humidity:    46.65 %
18:47:07.550 -> Pressure:    1021.05 hPa
18:47:07.550 -> Altitude:    42.84 m

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