MPU6050 angle only between 0-90 i want only Z-AXIS should change

#include<Wire.h>
 #include <LiquidCrystal_I2C.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
 LiquidCrystal_I2C lcd(0x27, 16, 2);
int minVal=265;
int maxVal=402;
 
double x;
double y;
double z;
 
void setup(){
Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
 lcd.init();
  lcd.backlight();
  lcd.setContrast(500);

}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ, minVal, maxVal, 0, 90);
 
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
 
Serial.print("AngleX= ");
Serial.println(x);
 
Serial.print("AngleY= ");
Serial.println(y);
 
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
lcd.clear(); // Clear the LCD display
  lcd.setCursor(0, 0); // Set the cursor to the first row
  lcd.print("AngleZ = ");
  lcd.print(z);
delay(400);
}


i want angle only between 0 - 90 along z axis , the liquid crystal is showing only 90 not changing when mpu6050 sensor moves

this video shows that how the angle z should change as expected

please need help

Video

Thankyou very much

If you read AcZ above maxVal, then this statement will map it above 90:

then this will constrain it to 90:

Where did you get this math?

This statement takes an acceleration measurement, and maps it into an arbitrary range.

int zAng = map(AcZ, minVal, maxVal, 0, 90);

Since it is not a measurement of an angle, why is the result called "zAng", and what is the reason for choosing the range 0 to 90?

1 Like

It looks like bad math from the internet. What is zAng? Is it supposed to be Yaw or bearing?

You can't calculate Yaw or Bearing from the accelerations, you would need gyroscope or magnetometer.

The math for roll and pitch is basic trigonometry.

Oh, you've changed the code.

I've seen this code before. It makes no sense, and I don't know what the results of the calculations mean. Do you?

If you want to measure pitch and roll angles, this web page explains how. How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot

1 Like

I moved your topic to an appropriate forum category @awais110565.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Without any of the map()s or contstrain()s, this line in the new code:

...would give you a 0-360°--direction of tilt. Constraining that measurement of a direction of tilt to a single 90° quadrant seems nonsensical. For example, constraining the direction of measured tilt to the 90° between rightwards & forward means that tipping backwards gives a result of straight rightwards tilt???

The above gives you nothing useful.

the link move like this in 3d space , so how can i find angle by mpu6050

Use the code in the following tutorial, and delete all copies of the complete nonsense you have posted so far:

https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing

so basically what is happening here that there is tilt and roll in the link?

If you want to measure tilt and roll, use the code in the link.

To be completely clear, these are mathematically correct expressions for the only two angles you can measure with the MPU-6050, where x,y,z are the three RAW acceleration values.

void RP_calculate(){
  double x_Buff = float(x);
  double y_Buff = float(y);
  double z_Buff = float(z);
  roll = atan2(y_Buff , z_Buff) * 57.3;
  pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
}
1 Like

Please reread reply #13, as many times as necessary.

Hint: you can try it out, and see if the angles actually make sense!

What values of roll and pitch appear in the serial monitor?

nm

You can't just make stuff up, like this line:

  mpu.getAcceleration(x, y, z);

Try this:

// minimal MPU-6050 tilt and roll (sjr). Works with MPU-9250 too.
// works perfectly 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 (worked 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);
}

So your z-axis and y-axis are horizontal, while your +y-axis is vertical.

What is the way that link moves in 3d space? It looks like theta pivots around z, Does the link also pivot around y? Is the MPU6050 mounted on the link? Is the pivot parallel to the z-axis and the MPU6050's Y-axis along the link? Like:

image

Then accZ' will always be near zero (because Z' is horizontal), and when theta = 0° accY'=0 because Y' is horizontal and accX = -1g because X' points downwards. And theta = 90° when accY=1g because the Y'-axis points up, and accX'=0 because the X' axis is horizontal, To get atan2(y',x') to give the proper angle, you have to be very careful with the axes, the signs and direction of gravity. If you mount it in a non-standard way, the standard formulas aren't going to work

@awais110565 has received a temporary account suspension for vandalizing this topic.

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