(I've started few months ago)
Hello, this is my first time using gyroscope sensor in a project. I wanted to try it, so I made the basic I2C setup, and ran the provided ELEGOO script. Though from what I understand, gyroscope should show the sensor's orientation, which isn't the case here. It seems that when I move it, it changes its value by a large amount, and when I leave it along, it returns to the 'normal value'. Instead, the values provided by the accelerometer, the another sensor built in the MPU6050 that the script is showing its value, seems to be the one I am looking for, it changes its value based on the orientation (That's what I am thinking at least). Basically, the values are the opposite for the two sensors. Many questions I have here, but to begin with let me start with the 3 data Serial monitor provide me with:
Sensor looking at one side of the Arduino + at rest (Right dir):
| AcX = 1420
| AcY = 14160
| AcZ = -7492
| Tmp = 37.42
| GyX = -156
| GyY = -373
| GyZ = -197
Data during the movement to the other side (Up dir):
Sensor looking at the other side of the Arduino + at rest (Left dir):
| AcX = 616
| AcY = 16524
| AcZ = 1980
| Tmp = 39.02
| GyX = 725
| GyY = -358
| GyZ = 31
To begin with, is the data the normal right output? Am I understanding things incorrectly?
Also, please note that the sensor is wired, so when I rotate it, and I am also technically moving it. (Think of it as a protractor where the Arduino is in the middle, and 'Right' is 180 degrees, Up is 90, Left is 0. This is just for clarity to make sure I am not missing any information I forgot to provide.) Am I overthinking? Someone please light up the question...
No, the sensor reports acceleration (including gravity) and rate of rotation on three axes.
The accelerometer can be used to estimate tilt angles, while the sensor is still, following this tutorial. Minimum code to do that using the MPU-6050:
// minimal MPU-6050 tilt and roll (sjr). Works with MPU-9250 too.
// works perfectly with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
//
// Tested with 3.3V eBay Pro Mini with no external pullups on I2C bus (worked with internal pullups)
// Add 4.7K pullup resistors to 3.3V if required. A4 = SDA, A5 = SCL
#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;
void setup() {
Wire.begin(); //begin the wire communication
Wire.beginTransmission(MPU_addr1); //begin, send the slave adress (in this case 68)
Wire.write(0x6B); //make the reset (place a 0 into the 6B register)
Wire.write(0);
Wire.endTransmission(true); //end the transmission
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(MPU_addr1);
Wire.write(0x3B); //send starting register address, accelerometer high byte
Wire.endTransmission(false); //restart for read
Wire.requestFrom(MPU_addr1, 6); //get six bytes accelerometer data
int t = Wire.read();
xa = (t << 8) | Wire.read();
t = Wire.read();
ya = (t << 8) | Wire.read();
t = Wire.read();
za = (t << 8) | Wire.read();
// formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
roll = atan2(ya , za) * 180.0 / PI;
pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied
Serial.print("roll = ");
Serial.print(roll,1);
Serial.print(", pitch = ");
Serial.println(pitch,1);
delay(400);
}
Bro, like literally, I can't thank you enough. Every single ounce of my confusion is now completely gone...Your message included everything I wanted to hear, thanks so much
Hello @jremington , the code worked perfectly, and I went through the link you provided. But there's a problem where the serial monitor stops giving me the values after few readings (EDIT: To be specific, the values of both, pitch and roll, becomes 0 after few seconds and readings. I have to reset the Arduino to get it back to work)... Any idea of the source problem? I'm using Arduino Mega, and 4 male-to-female jumper wires directly connected to the sensor. Sorry for the additional trouble...
EDIT 2: Not sure what I have done, but it worked somehow after resetting multiple times and changing the wires. Maybe something along the the lines made it work out. Thanks though!
The MPU-6050 was discontinued years ago by the original manufacturer, and you have some sort of clone or imitation. It may be defective, or there is an intermittent wiring problem.