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
}