Maker Pro
Maker Pro

Mpu6050 initial value

Mazeen

Jul 3, 2023
3
Joined
Jul 3, 2023
Messages
3
I am using mpu6050 with arduino uno and oled
The mpu6050 working great and i can measure pitch , roll and yaw value with no problem

Code:
/* Get tilt angles on X and Y, and rotation angle on Z
 * Angles are given in degrees, displays on SSD1306 OLED
 * 
 * License: MIT
 */
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MPU6050_light.h>
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
 
 
MPU6050 mpu(Wire);
unsigned long timer = 0;
 
void setup() {
  Serial.begin(115200);                           // Ensure serial monitor set to this value also    
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  // Address 0x3C for most of these displays, if doesn't work try 0x3D 
  { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);                                      // Don't proceed, loop forever
  } 
  display.setTextSize(1);             
  display.setTextColor(SSD1306_WHITE);            // Draw white text
  display.clearDisplay();                         
  Wire.begin();
  mpu.begin();
  display.println(F("Calculating gyro offset, do not move MPU6050"));
  display.display();        
  mpu.calcGyroOffsets();                          // This does the calibration
  display.setTextSize(2);          
}
 
void loop() {
  mpu.update();  
  if((millis()-timer)>10)                         // print data every 10ms
  {                                           
    display.clearDisplay();                       // clear screen
    display.setCursor(0,0);                         
    display.print("P : ");
    display.println((int)mpu.getAngleX());
    display.print("R : ");
    display.println((int)mpu.getAngleY());
    display.print("Y : ");
    display.print((int)mpu.getAngleZ());
    display.display();                            // display data
    timer = millis();  
  }
}

The mpu6050 working great and i can measure pitch , roll and yaw value with no problem

But it shows the angles according to the gavity

I want it shows the zero angles at startup. So, regardless of the initial orientation of the MPU6050, the angles are zero. Moving it updates the angles relative to the initial orientation not gravity orientation

How to show the initial angles at startup zero then update the angles relative to the initial angles regardless the gravity orientation?

Means if i started it while iam holding it with 90° on the floor, the initial degree show is zero not 90°
 

Bluejets

Oct 5, 2014
6,862
Joined
Oct 5, 2014
Messages
6,862
Startout by reading the spec sheet.
Very similar query elsewhere...... ;)
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,650
Joined
Nov 17, 2011
Messages
13,650
At startup aka setup(), read the angle and save it in a variable. Then in the main loop subtract this initial angle from each measurement. This will give 0 for the initial position and will give the difference between actual position and initial position else.
 

bidrohini

Feb 1, 2023
164
Joined
Feb 1, 2023
Messages
164
You can try this code:
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <MPU6050_light.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); MPU6050 mpu(Wire); unsigned long timer = 0; float initialAngleX = 0.0; float initialAngleY = 0.0; void setup() { Serial.begin(115200); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for (;;) {} // Don't proceed, loop forever } display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.clearDisplay(); Wire.begin(); mpu.begin(); display.println(F("Calculating gyro offset, do not move MPU6050")); display.display(); mpu.calcGyroOffsets(); display.setTextSize(2); // Store initial angles initialAngleX = mpu.getAngleX(); initialAngleY = mpu.getAngleY(); } void loop() { mpu.update(); if ((millis() - timer) > 10) { display.clearDisplay(); display.setCursor(0, 0); // Calculate updated angles relative to initial orientation float currentAngleX = mpu.getAngleX() - initialAngleX; float currentAngleY = mpu.getAngleY() - initialAngleY; display.print("P : "); display.println((int)currentAngleX); display.print("R : "); display.println((int)currentAngleY); display.print("Y : "); display.print((int)mpu.getAngleZ()); display.display(); timer = millis(); } }
 

Mazeen

Jul 3, 2023
3
Joined
Jul 3, 2023
Messages
3
You can try this code:
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <MPU6050_light.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); MPU6050 mpu(Wire); unsigned long timer = 0; float initialAngleX = 0.0; float initialAngleY = 0.0; void setup() { Serial.begin(115200); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for (;;) {} // Don't proceed, loop forever } display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.clearDisplay(); Wire.begin(); mpu.begin(); display.println(F("Calculating gyro offset, do not move MPU6050")); display.display(); mpu.calcGyroOffsets(); display.setTextSize(2); // Store initial angles initialAngleX = mpu.getAngleX(); initialAngleY = mpu.getAngleY(); } void loop() { mpu.update(); if ((millis() - timer) > 10) { display.clearDisplay(); display.setCursor(0, 0); // Calculate updated angles relative to initial orientation float currentAngleX = mpu.getAngleX() - initialAngleX; float currentAngleY = mpu.getAngleY() - initialAngleY; display.print("P : "); display.println((int)currentAngleX); display.print("R : "); display.println((int)currentAngleY); display.print("Y : "); display.print((int)mpu.getAngleZ()); display.display(); timer = millis(); } }
no worked unfortunately
 

Mazeen

Jul 3, 2023
3
Joined
Jul 3, 2023
Messages
3
At startup aka setup(), read the angle and save it in a variable. Then in the main loop subtract this initial angle from each measurement. This will give 0 for the initial position and will give the difference between actual position and initial position else.

do you mean manually set the variable to the angle value of the relative to some other orientation? if so it would not work as i want because i will need to measure many thins with different orientations can not manually change the variable every time.

is it possible to save the values from the original orientation in one set of variables automatically by coding?, means if it started 90° the code automatically set a this value as variable and so on
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,650
Joined
Nov 17, 2011
Messages
13,650
do you mean manually set the variable to the angle value of the relative to some other orientation?
No, not manually.
When you start the program (turn on the device), the device will be at an arbitrary angle. In the setup() routine you read this angle into a variable, e.g.
C:
startup_angle = readangle ()
.
This assumes you put the code to read the angle into a subroutine called readangle(). You haven't done so in your code, but that is good practice. This way you have to write the code for reading angle only once but can use this many times.
In the main loop you determine the actual angle by
C:
actual_angle = readangle()-startup_angle

is it possible to save the values from the original orientation in one set of variables automatically by coding?, means if it started 90° the code automatically set a this value as variable and so on
That is exactly what I originally proposed and tried to visualize with the above code snippets.
And what @bidrohini showes in a more elaborate way in his code.
 
Top