Finding an object is either moving or at rest using MPU6050 sensor

I am a beginner and new at using the MPU6050 accelerometer. I want to place this MPU6050 accelerometer sensor on an object and going to find out whether the object is either moving or at rest. Here, I don't care about the orientation of the object. Orientation of the object can be at any posture. But the object should be at rest. I used the MPU6050 library
(GitHub - jarzebski/Arduino-MPU6050: MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library). But example codes provided with it are not satisfying and also I don't know the threshold value of acceleration readings to keep the object at rest. Anyone here, kindly assist me by providing Arduino codes for this. Pls... and excuse me for directly asking the codes. I am at the beginner stage.

"At rest" and uniform motion in a straight line are indistinguishable to an accelerometer, as there is no acceleration. But the latter is hard to achieve in real life, so some level of acceleration due to vibration or bouncing is expected for a moving object even if the velocity is approximately constant.

Accelerometers are noisy, so you would need to set some detection threshold.

Basic code for printing raw data from the MPU-6050 can be found in countless tutorials on the web, but this one works:

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;

void setup() {
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)

void loop() {
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.requestFrom(MPU_addr, 14); // request a total of 14 registers
  int t =;
  AcX = (t << 8) |; // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  t =;
  AcY = (t << 8) |; // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  t =;
  AcZ = (t << 8) |; // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  t =;
  Tmp = (t << 8) |; // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  t =;
  GyX = (t << 8) |; // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  t =;
  GyY = (t << 8) |; // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  t =;
  GyZ = (t << 8) |; // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  t =;
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp / 340.00 + 36.53); //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
1 Like

It is very useful to run and some tutorials for yourself, isn´t it?

1 Like

Thank you Jremington. That threshold is the issue. Could you please tell me the threshold values to keep a device at rest approximately.

Thank you Paul. I am looking forward to receiving the threshold. If you could, kindly provide me the approximate values of the sensor , to keep it at rest

That threshold is for you to decide. If you chose a value that is too low your sketch will tell you the device is moving when it is not. If the value it too high the device can slowly move away without you noticing.

There are many sources of noise that can affect your system and the values you get e.g., supply voltage, vibration of your object, ... You need to set up the system and do some experiments. Then you need to compromise. That is the value of an engineer, to select the right compromise for an application. In your case the threshold.

Also remember there is a big rock under your device called earth creating an acceleration of ~1g.

until now you have just talked about

keeping an object at rest implies some kind of action to make the object stay right at that place where it is.

As long as you keep your real project a secret it will be hard to give any further advice. As mentioned above there are a lot of individual factors that influence what the right threshold-value will be. Imagine your "object" is placed in a car that is driving down a bumby road. The threshold-value will be completely different from your "object" is laying on a table.
To go even further: detecting the object is at rest or moving is not a self-purpose. I guess the main goal is not switching on a LED when the object is moving. Your main goal is something else.
It is quite often that as soon as the final purpose is described in detail another and different solution can be found for this particular purpose.

And there are these words from you "keeping an object at rest". Is this meant as something - whatever it is is taking some action to counteract on the objects movements?

So all in all you should tell what the whole poject is. If you do not you will have a hard way of learning it all yourself. If this is some kind of school-project I would even consider to choose a completely different application using an arduino.

This kind of project does not work like "Alexa order the same pizza as last week"

Even Alexa will ask back
Alexa: "same pizza as on monday, tuesday or saturday last week?"

You: "saturday"

Alexa: saturday 12 am or saturday 20 pm?"

You: "20 pm"

Alexa: "do you have real money? The service you ordered from last saturday 20 pm does not accept credit-cards"

You are working on an informatic-project. And what is needed most on an informatic project is information.

best regards Stefan

What does this mean?


What is causing the device to move?
Is it tilting?
Is there some sort of sideways force?
What are you going to use to correct the unwanted movement?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thank you all for your responses. I am new to this forum.That's why, i don't know the details required to be provided to get a help.
I want to make device like smart watch. This device will be placed at the wrist of a man. I want to detect whether movement is there or not. It can be the hand movement or the movement of person(walking, running etc) .

For Eg: one person has worn this device at his wrist. Now, he is moving.. (Or he is waving his hand). At this point, my device should detect that, there is a movement. No need to care about the direction. Only requirement is checking, whether there movement is a movement or not. If no movement is detected, then it will be considered as resting point. That's what all i need.

This means you have to do experiments with a testperson that does typical things:

  • walking
  • sitting down
  • standing up
  • scratch your body
    all multiple times and always store the measured data to do an analysis of the data after that.

with this research you will find more or less typical values of what kind of movement causes what values.

And based on these values you can write a program that tries to determine movement or no movement.

This seems quite a lot of work. But you will learn a lot from it. Even if the project should not be successful you will have gained a lot of experience.

"no movement = resting point" I guess this is still not the final purpose.
If you want to determine if a person is standing / sitting still or moving I guess using a PC with a videocamera and openCV will result in much quicker success.
Though I don't know if this is what you want.

So it seems your first steps will be to learn how to describe your project in a precise way that good suggestions can be made.
best regards Stefan

So you are looking at a basic pedometer device.


arduino pedometer mpu6050

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

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