ESP32 with GY-91 Sensor

Hello everyone !

This is my first time using Arduino IDE with an ESP32.

I'd like to read the values from both the MPU-9250 and the BMP280. Using two librairies: MPU9250_asukiaaa and Adafruit_BMP280, i managed to get values from accelerometer and gyro but I always get 0 from magnetometer and "nan" from temperature or pressure. I assume this is not a wiring issue since it works for accel and gyro.

I'm using an ESP32 NodeMCU Dev Kit C from AzDelivery with the IDE Board type: DOIT ESP32 DEVKIT V1
My wiring is:
3v3 - 3v3
Ground - Ground
PinG32 - SDA
PinG25 - SCL

I modified a script from Mohammad Damirchi:

Nota Bene: I've identified the I2C address just in case:

  • 0x68 for MPU9250
  • 0x76 for BMP280

Thanks for your future help.

#include <MPU9250_asukiaaa.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp;
MPU9250_asukiaaa mpu;
float aX, aY, aZ, aSqrt, gX, gY, gZ, mDirection;
uint16_t mX, mY, mZ;

#define SDA_PIN 32
#define SCL_PIN 25

void setup() {
  
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN);
  mpu.setWire(&Wire);

  bmp.begin();
  mpu.beginAccel();
  mpu.beginGyro();
  mpu.beginMag();

}

void loop() {

  if (mpu.accelUpdate() == 0) {
    aX = mpu.accelX();
    aY = mpu.accelY();
    aZ = mpu.accelZ();
    aSqrt = mpu.accelSqrt();
    Serial.print("accelX: " + String(aX));
    Serial.print("\taccelY: " + String(aY));
    Serial.print("\taccelZ: " + String(aZ));
    Serial.print("\taccelSqrt: " + String(aSqrt));
  }

  if (mpu.gyroUpdate() == 0) {
    gX = mpu.gyroX();
    gY = mpu.gyroY();
    gZ = mpu.gyroZ();
    Serial.print("\tgyroX: " + String(gX));
    Serial.print("\tgyroY: " + String(gY));
    Serial.print("\tgyroZ: " + String(gZ));
  }

  if (mpu.magUpdate() == 0) {
    mX = mpu.magX();
    mY = mpu.magY();
    mZ = mpu.magZ();
    mDirection = mpu.magHorizDirection();
    Serial.print("\tmagX: " + String(mX));
    Serial.print("\tmaxY: " + String(mY));
    Serial.print("\tmagZ: " + String(mZ));
    Serial.print("\thorizontalDirection: " + String(mDirection));
  }

  Serial.print("\tTemperature(*C): ");
  Serial.print(bmp.readTemperature());

  Serial.print("\tPressure(Pa): ");
  Serial.print(bmp.readPressure());

  Serial.println("");
  delay(3000);
  }

Welcome to the forum.
Per guidelines, could you please post you code in code tags. Click the </> button at the top of the message window.

Having said that, I think the problem may be the declaration of the mag variables.

Go to the github source of the library, which is nicely linked by the Electropeak website, and check out the ReadMe file. Look at the magnetometer section:

float mDirection;
uint16_t mX, mY, mZ;

Even though you are following a published example, it always pays to go back to the source.
Hope this helps. If so, please let us know.

Hi, thank you for your advice.

I changed my answer containing the code, adding code tags before and after.

Indeed, I declared with the wrong type of variable, I've updated my code (as seen in the previous answer) but I get the same result: no values for magnetometer and ofc still "nan" for the temperature and pressure.

The sensor and the BME use different libraries and probably different addresses. I have not looked carefully into the BME library, but could there be a clash of address or registers between the two?

The github readme page has examples of reading accel, gyro and mag.
I would suggest going really simple and test each bit of code individually, and then the code for just the BME by itself.
If you find that you get gyro OK, but adding mag causes trouble, then there is a clash in the sensor library.

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