How to process raw data from mpu6050 accelerometer

Hii
i am working with mpu6050 and stucked with getting a proper angle value from accelerometer.
(I am using full scale range of accelerometer). for now, just consider one angle (angle X) , so when i am reading the raw data from the accelerometer it is giving me the range of value from 0 - 16384 when i turn MPU6050 from 0 to 90 degree.
and when rotating 90 to 180 degree, value chnages from 16384 - 0
image
image

and same is going in while rotating 180 to 270, value is changing from 0 to -16384 and again for 270 - 360 degree it goes all the way -16384 to 0.

I actually want to map this value in the range of 0 - 360 degree,
how it will be done, any idea would be appreciated

here is the program i wrote, just fro reading accelerometer x value.

#include<Wire.h>

const int MPU_addr = 0x68; // MPU6050 I2C address
byte error;
int16_t temp;
int16_t accel_x;

void initialise_gyro()
{
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B); // power management 1
  Wire.write(0x00);
  Wire.endTransmission(true);

  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6c); // standby mode of particular hardware
  Wire.write(0x00);
  Wire.endTransmission(true);

  Wire.beginTransmission(MPU_addr);
  Wire.write(0x1C); // accelerometer configuration
  Wire.write(0b00000000);
  Wire.endTransmission(true);
  delay(100);
}

int16_t read_accel_x()
{ // for reading accel_x
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  error = Wire.endTransmission(false);
  if (error != 0)
  {
    Serial.println("ERROR DURING READING");
  }
  Wire.requestFrom(MPU_addr, 2, true);
  if (Wire.available() == 2)
  {
    temp = Wire.read() << 8;
    temp |= Wire.read();
  }
  else
  {
    Serial.println("data not equal to two byte");
  }
  return temp;
}

void setup() {
  Serial.begin(115200);
  initialise_gyro();
  delay(200);
}


void loop() {
  accel_x = read_accel_x();
  Serial.println(accel_x);
  //Serial.println(accel_x/180); //for the range of angle 0-180 , 0-16384
  delay(100);
}

1 Like

Map

1 Like

with map function we can map the value 0-90 degree for the read val 0-16384, but what for the second side, how will we know whether the value is in from 0-90 degree or in 90-180 degree??

1 Like

If the value is the same then you can’t tell unless you have another input to help differentiate

Mathematically correct code for tilt angle measurements, using the MPU-6050 follows.
See tutorial at How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot

// minimal MPU-6050 tilt and roll (sjr). Works with MPU-9250 too.
// tested with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
// Tested with 3.3V eBay Pro Mini with no external pullups on I2C bus (works with internal pullups)
// Add 4.7K pullup resistors to 3.3V if required. A4 = SDA, A5 = SCL

#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {

  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.write(0);
  Wire.endTransmission(true);                        //end the transmission
  Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(MPU_addr1);
  Wire.write(0x3B);  //send starting register address, accelerometer high byte
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(MPU_addr1, 6); //get six bytes accelerometer data
  int t = Wire.read();
  xa = (t << 8) | Wire.read();
  t = Wire.read();
  ya = (t << 8) | Wire.read();
  t = Wire.read();
  za = (t << 8) | Wire.read();
// formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied

  Serial.print("roll = ");
  Serial.print(roll,1);
  Serial.print(", pitch = ");
  Serial.println(pitch,1);
  delay(400);
}

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