I'm using Bluerobotics Bar30 pressure sensor (I2C) and MPU9250 IMU sensor (also I2C) with Arduino UNO.
The following code is working perfectly:
#include "MPU9250.h"
MPU9250 mpu9250;
#include <Wire.h>
#include "MS5837.h"
MS5837 sensor;
void setup() {
Serial.begin(9600);
Wire.begin();
delay(2000);
if (!mpu9250.setup(0x68)) { // change to your own address
while (1) {
Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
delay(5000);
}
}
while (!sensor.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);
}
sensor.setModel(MS5837::MS5837_30BA);
sensor.setFluidDensity(997);
}
void loop() {
if (mpu9250.update()) {
static uint32_t prev_ms = millis();
if (millis() > prev_ms + 25) {
print_roll_pitch_yaw();
prev_ms = millis();
}
}
sensor.read();
Serial.print(sensor.pressure());
Serial.println(" mbar");
}
void print_roll_pitch_yaw() {
Serial.print("Yaw, Pitch, Roll: ");
Serial.print(mpu9250.getYaw(), 2);
Serial.print(", ");
Serial.print(mpu9250.getPitch(), 2);
Serial.print(", ");
Serial.println(mpu9250.getRoll(), 2);
}
However, the following code isn't working. I'm getting the pressure values from the Bar30 sensor, but mpu9250 values aren't coming on the serial monitor, it's all zeros.
#include "MPU9250.h"
#include <Wire.h> //
#include "MS5837.h" //
MS5837 sensor; //
MPU9250 mpu;
void setup() {
digitalWrite(13, LOW);
Serial.begin(115200);
Wire.begin();
if (!mpu.setup(0x68)) { // change to your own address
while (1) {
Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
delay(5000);
}
}
while (!sensor.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);
}
sensor.setModel(MS5837::MS5837_30BA);
sensor.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater)
}
void loop() {
if (mpu.update()) {
static uint32_t prev_ms = millis();
if (millis() > prev_ms + 25) {
print_roll_pitch_yaw();
prev_ms = millis();
}
}
}
void print_roll_pitch_yaw() {
Serial.print(mpu.getYaw(), 2);
Serial.print(", ");
Serial.print(mpu.getPitch(), 2);
Serial.print(", ");
Serial.print(mpu.getRoll(), 2);
Serial.print(", ");
sensor.read();
Serial.print(sensor.pressure());
Serial.print(", ");
Serial.print(sensor.temperature());
Serial.print(", ");
Serial.print(sensor.depth());
Serial.print(", ");
Serial.println(sensor.altitude());
delay(100);
}
Can't understand why. The only difference is that this part:
sensor.read(); Serial.print(sensor.pressure()); Serial.println(" mbar");
is in the print_roll_pitch_yaw function in the code that's working, while in the second code which isn't working, this part of the code is in the loop function.