MPU6050 high Gyro value

Hi I am going to read Gyro value using ESP32. Below code worked well using Arduino and raw Gyro showed small value within 100.. but I ran same code on ESP32 and it showed very high value upto 65,000. Here is chart of Arduino vs ESP32 raw values.

ESP32 Serial monitor plot


Arduino Serial monitor plot

Below is code I used. Can anyone help me how to read good raw value using ESP32?

#include <Wire.h>

//Declaring some global variables
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;

void setup() {
  Wire.begin();                                                        //Start I2C as master
  Wire.setClock(400000);
  Serial.begin(115200);                                               //Use only for debugging
  
  setup_mpu_6050_registers();                                          //Setup the registers of the MPU-6050 (500dfs / +/-8g) and start the gyro
}

void loop(){

  read_mpu_6050_data();                                                //Read the raw acc and gyro data from the MPU-6050

 
  Serial.print("Pitch:");
  Serial.print(gyro_x);
  Serial.print(",");
  Serial.print("Roll:");
  Serial.println(gyro_y);
  delay(10);                                                          //Delay 3us to simulate the 250Hz program loop
  
}


void read_mpu_6050_data(){    
      Wire.beginTransmission(0x68); 
      Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 
      Wire.endTransmission();                                              //End the transmission
      Wire.requestFrom(0x68,14);                                           //Request 14 bytes from the MPU-6050
      acc_x = Wire.read()<<8|Wire.read();// 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
      acc_y = Wire.read()<<8|Wire.read();//0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      acc_z = Wire.read()<<8|Wire.read();// 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      temperature = Wire.read()<<8|Wire.read();// 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      gyro_x = Wire.read()<<8|Wire.read();// 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      gyro_y = Wire.read()<<8|Wire.read();// 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      gyro_z = Wire.read()<<8|Wire.read();// 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      
}
     

void setup_mpu_6050_registers(){
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x6B);                                                    //Send the requested starting register
  Wire.write(0x00);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the accelerometer (+/-8g)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1C);                                                    //Send the requested starting register
  Wire.write(0x10);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the gyro (500dps full scale)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1B);                                                    //Send the requested starting register
  Wire.write(0x08);                                                    //0x00 for ±250dps/ 0x08 for ±500dps/ 0x10 for ±1000dps 0x18 for ±2000dps
  Wire.endTransmission();                                              //End the transmission
}

From your first image: you read signed int and display it as unsigned int. 65535 == -1

Hi @brianmoon welcome to the forum.

You might want to look at this How to get the best from this from this Forum before you proceed any further.

It tells you how to post code.

It seems your problem is because an ESP32 processor works off 3V3 where as an Arduino Uno works from 5V.

That means while a Uno can measure a voltage up to 5V, the ESP32 can only measure a voltage up to 3V3 before it saturates and shows the value is a maximum of 3V3.
You need to use a voltage divider to cut down your input signal from 5V to 3V3 before you feed it into the ESP32.

HI Mike
Appreciate your reply. I fixed the posting following your link. Thanks.
Actually I used 3.3V power from ESP32 as shown below.
I tried other ESP chips but still same.

Hi Zwieblum

Thanks for reply.
I am biginner in code. Can you help me to fix this issue?
Is there something wrong on definding values?
if so, why this is working on Arduino but not working on ESP32.
Thanks a lot..

You have this in your code:

acc_x = Wire.read()<<8|Wire.read();

Wire.read() returns int. Depending on the length of "int" this works (ardunio 8 bit, int is 2 byte) or it does not work (esp32, int is 4 byte). So you'd need to use explict 2 byte ints for this to work across different platforms.

Hi Zwieblum
I changed the variable type as below by your advice and it works!!! WOW

int16_t gyro_x, gyro_y, gyro_z; 

Is this correct fixing following your advice?

Well done :+1:

1 Like

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