Can i use an MPU-6050 upside-down?

I'm trying to use my MPU-6050 upside-down since it's easier to place on the circuit. However it returns weird values: when it is horizontal it returns 0 degrees as it should be but when i try to rotate it i get weird angles.

When it is placed head up it works perfectly, but isn't there a way to use it upside down?

Here's the code i use to get the angles and filter them:

  //Acceleration Angles (0 = X, 1 = Y);
  Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;
  Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;

  //Gyroscope Anlges
  Gyro_angle[0] = Gyr_rawX/131.0; 
  Gyro_angle[1] = Gyr_rawY/131.0;

  //Resulting Angles
  Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];

The acceleration and gyro values are measured relative to the (X,Y,Z) coordinate axes defined by the chip itself. See the device data sheet for the details.

Your program needs a transformation that relates those chip axes to whatever real world (X, Y, Z) axes you are using, and that depends on how the IMU module is mounted.

Many people use a general transformation matrix.

So for example in this case the Z axe is upside down and I should transform it with that axe inverted?

Sorry If I didn't understand correctly but I don't know much about transformation matrix

You must use atan2, not atan, for angle calculations.

Oh i see! But in this case what should i use as second parameter of the function? Atan2 looks like it needs 2 parameters.

So for example in this case the Z axe is upside down and I should transform it with that axe inverted?

You can't invert the Z axis without changing the direction of X or Y as well.

The sensor axes are fixed with respect to each other, and you have to know which direction all of them are pointed to work out the transformation.

You need trigonometry and linear algebra to do this. If you are interested in learning about these topics, google "coordinate system transformations". One place to start is here.

DragoSupremo:
Oh i see! But in this case what should i use as second parameter of the function? Atan2 looks like it needs 2 parameters.

Yes, that's why you need it, you are performing a catesian to polar coordinate transform and you need to
know the signs of both axes to get the angle right for all 360 degrees.

So for instance on the xy-plane you'd call atan2 (y, x) to get the mathematical angle.

Review coordinate transforms:

polar coords (r, theta). x = r cos(theta), y = r sin(theta), thus tan(theta) = y/x.

But note that tan(theta) repeats every 180 degrees, not every 360, so it loses vital information.

cartesian coords (x, y). r = sqrt(x^2+y^2), theta = atan2(y, x)

Note that atan2 sees the signs of both x and y and can reconstruct the correct angle always,
and doesn't blow up if x = 0, which atan(y/x) would.