Compass Belt

Hello Everyone!

This is my first post and I am trying to get this gyroscope sensor to read more accurately.

I am using an MPU-9250 sensor (3-axis Gyroscope) connected to an Arduino Uno ATmega328 which has 8 vibration motors attached to it (down a belt).

An example of the problem is; when I turn the gyroscope sensor halfway, the degree readings do not change that much. Roughly around a degree range of 20 difference (see image). I need help in trying to figure out how to get the readings to change from 0-360 degrees when I rotate the gyroscope sensor all the way around.

#include "MPU9250.h"
#include <Wire.h>

/* MPU9250 Connection
VDD---------3.3V
GND---------GND
SDA---------A4
SCL---------A5
*/

// MPU9250 sensor on I2C bus 0 address 0x68
MPU9250 IMU(Wire,0x68);
int status;

// pin connections to motors 2-9 
const int NPin = 2;
const int NEPin = 3;
const int EPin = 4;
const int SEPin = 5;
const int SPin = 6;
const int SWPin = 7;
const int WPin = 8;
const int NWPin = 9;

void setup()
{
  // serial display data
  Serial.begin(115200);
  // connection to I2C bus
  Wire.begin();
  pinMode(NPin, OUTPUT);
  pinMode(NEPin, OUTPUT);
  pinMode(EPin, OUTPUT);
  pinMode(SEPin, OUTPUT);
  pinMode(SPin, OUTPUT);
  pinMode(SWPin, OUTPUT);
  pinMode(WPin, OUTPUT);
  pinMode(NWPin, OUTPUT);
}

void loop()
{
  Wire.beginTransmission(0x68);
  Wire.write("A");
  delay(100);
  Wire.requestFrom(0x68, 2);
  byte MSB = Wire.read();
  byte LSB = Wire.read();
  Wire.endTransmission();
  float degs = ((MSB << 8) + LSB) / 10;
  Serial.print(degs);
  Serial.println(" degrees.");
  // degrees to indicate Northern location
  if ((degs >= 337.5 && degs <= 360) || (degs >= 0.0 && degs < 22.5))
  {
    digitalWrite(NPin, HIGH);
  }
  else
  {
    digitalWrite(NPin, LOW);
  }
  // Check all other directions using a 45-degree range.
  if (degs >= 22.5 && degs < 67.5)
  {
    digitalWrite(NEPin, HIGH);
  }
  else
  {
    digitalWrite(NEPin, LOW);
  }
  if (degs >= 67.5 && degs < 112.5)
  {
    digitalWrite(EPin, HIGH);
  }
  else
  {
    digitalWrite(EPin, LOW);
  }
  if (degs >= 112.5 && degs < 157.5)
  {
    digitalWrite(SEPin, HIGH);
  }
  else
  {
    digitalWrite(SEPin, LOW);
  }
  if (degs >= 157.5 && degs < 202.5)
  {
    digitalWrite(SPin, HIGH);
  }
  else
  {
    digitalWrite(SPin, LOW);
  }
  if (degs >= 202.5 && degs < 247.5)
  {
    digitalWrite(SWPin, HIGH);
  }
  else
  {
    digitalWrite(SWPin, LOW);
  }
  if (degs >= 247.5 && degs < 292.5)
  {
    digitalWrite(WPin, HIGH);
  }
  else
  {
    digitalWrite(WPin, LOW);
  }
  if (degs >= 292.5 && degs < 337.5)
  {
    digitalWrite(NWPin, HIGH);
  }
  else
  {
    digitalWrite(NWPin, LOW);
  }
  delay(100);
}

The gyroscope in the MPU-9250 IMU is a rate gyro, which means it reports the rate of rotation.

To get total rotation, you have to integrate the rate with respect to time, after calibrating the gyro to remove the offset.

However, the MPU-9250 is best used as an AHRS (Attitude and Heading Reference System), which reports absolute 3D orientation. You need AHRS software and to calibrate the magnetometer before that will be useful.

  Wire.beginTransmission(0x68);
  Wire.write("A");
  delay(100);
  Wire.requestFrom(0x68, 2);
  byte MSB = Wire.read();
  byte LSB = Wire.read();
  Wire.endTransmission();
  float degs = ((MSB << 8) + LSB) / 10;
  Serial.print(degs);
  Serial.println(" degrees.");

This code seems to be reading registers 65 and 66 (‘A’ = 65 = 0x41) which contain the TEMPERATURE and have nothing to do with the gyroscope or compass.

  // degrees to indicate Northern location
  if ((degs >= 337.5 && degs <= 360) || (degs >= 0.0 && degs < 22.5))

What is this?

jremington:

  // degrees to indicate Northern location

if ((degs >= 337.5 && degs <= 360) || (degs >= 0.0 && degs < 22.5))



What is this?

Looks like “North, plus or minus 22.5 degrees” to me.

That would be a northern direction, not location.

Hi,
Is there a reason you include the MPU-2950 library, but don't appear to use it?

Have you looked at the example code that come with that library?
It will simplify reading the appropriate registers.

Tom... :slight_smile: