Getting data from both 30Bar and MPU6050 on Arduino?

Hi everyone,

I recently purchased a 30Bar pressure sensor and am trying to get it to work in combination with a DHT11 sensor and an MPU6050 on an Arduino Uno. I’m using an I2C multiplexer, specifically a PCA9548A for serial communication. The SDA and SCL for both the 30Bar pressure sensor and the MPU6050 are connected to the multiplexer which is connected to A4 and A5 on my arduino.

I want to run these three sensors at once and send the data to processing IDE. I wrote an arduino sketch for each to collect basic data. All the sensors work fine independently, I tried merging them and my 30Bar is reading really crazy values. After some trial and error I think it has something to do with the MPU6050. If I run a sketch for the MPU6050 alone through the multiplexer it works fine, same with the 30Bar, when connecting through the multiplexer alone it works fine.

If I merge the DHT11 sketch with the 30Bar sketch, both work fine, when I add the MPU6050 sketch to that I get crazy numbers, just for the pressure sensor. I made sure to set the model as a MS5837_30BA, I don’t get any errors when I run the sketch, which is below btw. Thanks!

#include <Wire.h>
#include <MS5837.h>
#include <MPU6050_light.h>
#include <DHT11.h>

MPU6050 mpu(Wire);
MS5837 ms5837;
unsigned long timer = 0;
DHT11 dht11(3);

void PCA9548A(uint8_t bus) {
  Wire.beginTransmission(0x70);
  Wire.write(1<<bus);
  Wire.endTransmission();
  Serial.print(bus);
}
void setup() {
  
  Serial.begin(9600);
  Serial.println("Starting");
  Wire.begin();
  
  ms5837.setModel(MS5837::MS5837_30BA);
  ms5837.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater)

  PCA9548A(2);
  
  while (!ms5837.init()) {
    Serial.println("Init failed!");
    Serial.println("Are SDA/SCL connected correctly?");
    Serial.println("Blue Robotics Bar30: White=SDA, Green=SCL");
    Serial.println("\n\n\n");
    delay(5000);
  }  
  
  PCA9548A(3);
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } 
  
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
  mpu.calcOffsets();
  Serial.println("Done!\n");

}

void loop() {
  ms5837.read();

  Serial.print("Pressure: "); 
  Serial.print(ms5837.pressure()); 
  Serial.println(" mbar");
  
  Serial.print("Temperature: "); 
  Serial.print(ms5837.temperature()); 
  Serial.println(" deg C");
  
  Serial.print("Depth: "); 
  Serial.print(ms5837.depth()); 
  Serial.println(" m");
  
  Serial.print("Altitude: "); 
  Serial.print(ms5837.altitude()); 
  Serial.println(" m above mean sea level");
  delay(1000);

  mpu.update();
  
  if((millis()-timer)>10){ // print data every 10ms
	Serial.print("X : ");
	Serial.print(mpu.getAngleX());
	Serial.print("\tY : ");
	Serial.print(mpu.getAngleY());
	Serial.print("\tZ : ");
	Serial.println(mpu.getAngleZ());
	timer = millis();  
  }

  int humidity = dht11.readHumidity();

  if (humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT) {
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
  }
}

Hi @bkeith, welcome to the forum :slight_smile:

You may have resolved this already, but in case you haven’t your posted code seems to only make use of the multiplexer (via your PCA9548A function) in the setup function, and not in the loop, in which case the Bar30 likely isn’t receiving communication after it is first set up, and the MS5837 library is accidentally talking to the MPU6050 and effectively just receiving gibberish in return.

Hi Eliot,

Thanks for the response! I did actually figure this out not long after I made this post and you guessed it, I wasn’t calling the PCA9548A in the void loop. I’m printing 6 values to the serial port and I’m using that data in Processing IDE. Below is my code incase anyone else ever has issues with a similar configuration!

#include "Wire.h"
#include "MS5837.h"
#include <MPU6050_light.h>
#include <DHT11.h>

MS5837 ms5837;
MPU6050 mpu(Wire);
unsigned long timer = 0;
DHT11 dht11(2);

void PCA9548A(uint8_t bus) {
  Wire.beginTransmission(0x70);
  Wire.write(1<<bus);
  Wire.endTransmission();
}
void setup() {
  
  Serial.begin(57600);
  Wire.begin();
  
  PCA9548A(1);

  ms5837.setModel(MS5837::MS5837_30BA);
  ms5837.setFluidDensity(1029); // kg/m^3 (997 is freshwater, 1029 for seawater) 

  while (!ms5837.init()) {
    Serial.println("Init failed!");
    Serial.println("Are SDA/SCL connected correctly?");
    Serial.println("Blue Robotics Bar30: White=SDA, Green=SCL");
    Serial.println("\n\n\n");
    delay(5000);
  }

  PCA9548A(2);
  byte status = mpu.begin();
  
  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
  mpu.calcOffsets();
}

void loop() {

  if((millis()-timer)>2000){ // print data every 2 sec (fastest timing for DHT11)
 
 int humidity = dht11.readHumidity();
 
 Serial.print(humidity);
  Serial.print("\t");

  PCA9548A(1);
  ms5837.read();

  Serial.print(ms5837.pressure());
  Serial.print("\t");

  Serial.print(ms5837.temperature());
  Serial.print("\t");

  Serial.print(ms5837.depth());
  Serial.print("\t");
  
  PCA9548A (2);
  mpu.update();

  float X = mpu.getAngleX();
  float Y = mpu.getAngleY();

  Serial.print(X);
  Serial.print("\t");

  Serial.println(Y);
  timer = millis();
  }
}
1 Like