GY-91 Sensor; Arduino

From wikiluntti
Revision as of 20:24, 26 November 2022 by Mol (talk | contribs) (→‎Example code)

Introduction

Uisng GY-91 sensor with Asukiaa's MPU9250 library. Install the library using Arduino IDE Manage Libraries. More information about the library is at https://github.com/asukiaaa/MPU9250_asukiaaa

GY-91 includes MPU-9250 circuit which has gyro, accelerometer and compass.

Theory

Example code

GY-91's MPU9250 accelerometer data.

The following code reads and prints the acceleration to each three directions, but using the following ideas, you can use gyroscope and magnetometer data also.

The output is sent to Serial line, and if the USB cable is plugged, you can read the data using Serial monitor, or more preferably using Serial plotter. Make sure that the bps is correct.

#include <MPU9250_asukiaaa.h>
MPU9250_asukiaaa mySensor;
float aX, aY, aZ, aSqrt;

void setup() {
  Wire.begin();
  mySensor.setWire(&Wire);
  mySensor.beginAccel();

  Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop() {
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();
  aSqrt = mySensor.accelSqrt();
  // Do what you want

  Serial.print( aX );
  Serial.print( "\t" );
  Serial.print( aY );
  Serial.print( "\t" );
  Serial.print( aZ );
  Serial.print( "\n" );
  
}

Accelerometer

  mySensor.beginAccel();
  accelX();

Gyrometer

  mySensor.beginGyro();
  gyroX();

Magnetometer

  mySensor.beginMag();
  magX();

Exercises

See Also & References

https://www.cod3v.info/index.php?title=Arduino_OKY3259-1_GY-BMP280