MPU6050 acceleration values changing on rotation about axis

I’m working on an automatic brake light sensor that will activate and LED when a certain level of deceleration is experienced. I’m reading data in from the MPU6050, but whenever I rotate it the acceleration values change along with the gyroscope values. I’m new to Arduino and I can’t figure out why this is happening. I’m using an Arduino UNO. My code is down below, any help would be much appreciated.

For example, when lying flat on the table the acceleration values are constant. But when I rotate the accelerometer 90 degrees, the values for the acceleration will change dramatically, even though there should be no linear acceleration. Also, the gyroscope values change when linear acceleration is experienced.

brakeSensor2.0.ino (5.16 KB)

This short example sketch is the minimal code to get the values: Arduino Playground - MPU-6050.
I suggest to use that code and move the sensor in all directions to learn the behavior of the sensor.

The acceleration values are very sensitive for short vibrations. When you keep it in your hand and rotate it, it might not change that much. Without offset compensation, it will measure the earth gravity of 1G.

The gyro does not measure the rotation as one would expect, it measures the change of rotation.

With a filter that combines accelerator, gyro and magnetometer the disadvantages of each sensor is eliminated by the others. It is often called "sensor fusion". Filters are for example: Kalman filter and AHRS filter.