MPU6050 how to convert acceleration to angle

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
octavius_8
New user
Posts: 4
Joined: 04 Mar 2020, 08:13

MPU6050 how to convert acceleration to angle

#1 Post by octavius_8 » 04 Mar 2020, 11:32

Hello espeasy community :D
maybe someone can help me with my problem. We are currently working on a school project and use espeasy to measure temperature, humidity and orientation (angle XYZ).
Unfortunately, the data from Gyro MPU6050 shows only acceleration values. Is there a way to convert the information into angles. For example with a formula or using rules.

Our configuration:
Espeasy release
  • ESPEasy_mega-20191208-17-PR_2820 (ESP_Easy_mega-20191208-17-PR_2820_test_beta_ESP8266_4M1M.bin)
The Hardware
  • Wemos D1 mini
  • DS18B20 (x4)
  • BME280
  • MPU6050
Thank you in advance for your ideas

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 how to convert acceleration to angle

#2 Post by TD-er » 04 Mar 2020, 12:51

Not supported in rules, but I guess we could make its output selectable to generate a roll/pitch value.

See https://wiki.dfrobot.com/How_to_Use_a_T ... lt_Sensing

Determine the roll and pitch like that will result in quite unstable readings when one of the sensor's axis is close to being vertical.
So I guess it also needs some filtering
And in most use cases, those are just the orientations used the most. (measuring if something is level or not)

octavius_8
New user
Posts: 4
Joined: 04 Mar 2020, 08:13

Re: MPU6050 how to convert acceleration to angle

#3 Post by octavius_8 » 04 Mar 2020, 14:09

Thanks for the quick response, that means I have to use the formula field (UI Espeasy) or alternatively the plugin has to be adapted for the selectable output.
Can you tell me what exactly needs to be done. Is it difficult?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 how to convert acceleration to angle

#4 Post by TD-er » 04 Mar 2020, 14:42

Well it looks like there needs to be some changes in the plugin.
Also I'm not really sure how stable the results will be.

So maybe you have enough experience in math to see for yourself how useful it will be and what may be needed?
It looks like the plugin only outputs a single value, or which 3 selections are gyroscopic values per axis.
Not sure what the range of these values is (pct, angle in deg or rad, something else?), but it looks like that one can already be used to compute the angle with some plane.

Maybe that can already be tested first, to get some feeling of whether the sensor is actually usable for your purpose? By using the G-value relative to a single axis, you may limit the detection range, but at least you can see if it is somewhat usable or not.
If it is usable, I can add the computed pitch and roll values as output functions.

For this I do need to know if the sensor does need some kind of "reset" or "calibration" and also the unit of measure this G-value does output.

octavius_8
New user
Posts: 4
Joined: 04 Mar 2020, 08:13

Re: MPU6050 how to convert acceleration to angle

#5 Post by octavius_8 » 04 Mar 2020, 15:52

for example in arduino sketch (s. picture below) the raw data can be called up directly from MPU6050 and displayed in the serial monitor (second picture).
Unfortunately I am not an IT and cannot apply the adjustments to the plagin by my self :( and i will need your help. I am also slightly surprised that so far nobody aks for the Gyro angle option (in degrees).
A reset or calibration is not necessary, because we use the Sleep Mode to save battery and the data can remain absolute.

Code: Select all

#include<Wire.h>
const int MPU6050_addr=0x68; // I2C address of the MPU-6050
int16_t AccX,AccY,AccZ,Temp,GyroX,GyroY,GyroZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU6050_addr,14,true);
  AccX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AccY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AccZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Temp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyroX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyroY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyroZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AccX = "); Serial.print(AccX);
  Serial.print(" || AccY = "); Serial.print(AccY);
  Serial.print(" || AccZ = "); Serial.print(AccZ);
  Serial.print(" || Temp = "); Serial.print(Temp/340.00+36.53);
  Serial.print(" || GyroX = "); Serial.print(GyroX);
  Serial.print(" || GyroY = "); Serial.print(GyroY);
  Serial.print(" || GyroZ = "); Serial.println(GyroZ);
  delay(100);
}

Data of the MPU6050
3 Axis Accelerometer Sensor Gyroscope 6DOF Module 3.3V-5V
chip: MPU-6050
Communication: standard IIC communication agreement
Chip built-in 16 bit AD converter, 16 bits of data output
The gyroscope range: + 250 500 1000 2000 ° /s
Acceleration range: ± 2 ± 4 ± 8 ± 16 g
Attachments
Arduino_MPU6050_Monitor.jpg
Arduino_MPU6050_Monitor.jpg (22.66 KiB) Viewed 17083 times
Last edited by octavius_8 on 04 Mar 2020, 16:37, edited 2 times in total.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 how to convert acceleration to angle

#6 Post by TD-er » 04 Mar 2020, 16:03

Please make an issue for it, as I am sure I will forget it when it is posted on the forum.
And hopefully someone else may also pick up the issue to make a pull request for it. (I'm a bit swamped in other stuff to do right now)

octavius_8
New user
Posts: 4
Joined: 04 Mar 2020, 08:13

Re: MPU6050 how to convert acceleration to angle

#7 Post by octavius_8 » 04 Mar 2020, 16:44

I made a issue for the plugin P045 (Gyro MPU6050), Thank you
https://github.com/letscontrolit/ESPEasy/issues/2922

Wolfsbane
New user
Posts: 1
Joined: 09 Jun 2022, 05:53

Re: MPU6050 how to convert acceleration to angle

#8 Post by Wolfsbane » 09 Jun 2022, 05:56

octavius_8 wrote: 04 Mar 2020, 15:52 for example in arduino sketch (s. picture below) the raw data can be called up directly from MPU6050 and displayed in the serial monitor (second picture).
Unfortunately I am not an IT and cannot apply the adjustments to the plagin by my self :( and i will need your help. I am also slightly surprised that so far nobody aks for the Gyro angle option (in degrees).
A reset or calibration is not necessary, because we use the Sleep Mode to save battery and the data can remain absolute.

Code: Select all

#include<Wire.h>
const int MPU6050_addr=0x68; // I2C address of the MPU-6050
int16_t AccX,AccY,AccZ,Temp,GyroX,GyroY,GyroZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU6050_addr,14,true);
  AccX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AccY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AccZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Temp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyroX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyroY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyroZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AccX = "); Serial.print(AccX);
  Serial.print(" || AccY = "); Serial.print(AccY);
  Serial.print(" || AccZ = "); Serial.print(AccZ);
  Serial.print(" || Temp = "); Serial.print(Temp/340.00+36.53);
  Serial.print(" || GyroX = "); Serial.print(GyroX);
  Serial.print(" || GyroY = "); Serial.print(GyroY);
  Serial.print(" || GyroZ = "); Serial.println(GyroZ);
  delay(100);
}

Data of the MPU6050
3 Axis Accelerometer Sensor Gyroscope 6DOF Module 3.3V-5V
chip: MPU-6050
Communication: standard IIC communication agreement
Chip built-in 16 bit AD converter, 16 bits of data output
The gyroscope range: + 250 500 1000 2000 ° /s
Acceleration range: ± 2 ± 4 ± 8 ± 16 g
Thank you for this example, may I ask how to map the AccX to 180-degree values?

Post Reply

Who is online

Users browsing this forum: No registered users and 35 guests