mpu6050 sensor not working

hi guys,
i am using esp8266 with mpu6050 to get values from it but all i am getting is temperature value x,y,z values are showing zero

here the code

#include <Wire.h>

// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;
const uint8_t scl = 12; 
const uint8_t sda = 13;

// sensitivity scale factor respective to full scale setting provided in datasheet 
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;

// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV   =  0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL    =  0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1   =  0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2   =  0x6C;
const uint8_t MPU6050_REGISTER_CONFIG       =  0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG  =  0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG =  0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN      =  0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE   =  0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H =  0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET  = 0x68;

int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;

void setup() {
  Wire.begin(scl , sda);

void loop() {
  double Ax, Ay, Az, T, Gx, Gy, Gz;
  Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
  //divide each with their sensitivity scale factor
  Ax = (double)AccelX/AccelScaleFactor;
  Ay = (double)AccelY/AccelScaleFactor;
  Az = (double)AccelZ/AccelScaleFactor;
  T = (double)Temperature/340+36.53; //temperature formula
  Gx = (double)GyroX/GyroScaleFactor;
  Gy = (double)GyroY/GyroScaleFactor;
  Gz = (double)GyroZ/GyroScaleFactor;

  Serial.print("Ax: "); Serial.print(Ax);
  Serial.print(" Ay: "); Serial.print(Ay);
  Serial.print(" Az: "); Serial.print(Az);
  Serial.print(" T: "); Serial.print(T);
  Serial.print(" Gx: "); Serial.print(Gx);
  Serial.print(" Gy: "); Serial.print(Gy);
  Serial.print(" Gz: "); Serial.println(Gz);


void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){

// read all 14 register
void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
  Wire.requestFrom(deviceAddress, (uint8_t)14);
  AccelX = (((int16_t)<<8) |;
  AccelY = (((int16_t)<<8) |;
  AccelZ = (((int16_t)<<8) |;
  Temperature = (((int16_t)<<8) |;
  GyroX = (((int16_t)<<8) |;
  GyroY = (((int16_t)<<8) |;
  GyroZ = (((int16_t)<<8) |;

//configure MPU6050
void MPU6050_Init(){
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00);

i have no idea whats wrong
thank you in advance

according to the pinout of the ESP8266 you should be connecting SDA to pin GPIO4 and SCL to GPIO5
and edit the sketch appropriately

I tried but i still get the same result
I took the pin change part from the program

Check your pins for SDA and SCL and run a I2C Scanner sketch.
Please always show us the full new sketch, even after a very small change.
The I2C Scanner sketch should find the sensor. There is no need to try something else if the I2C Scanner sketch can not find it.

Can you attach a photo with the wiring, so we can check it for ourselves ?
Please use the order: SDA, SCL.
You had the wrong order with Wire.begin().

Use SDA, SCL order here:

const uint8_t sda = 4;
const uint8_t scl = 5;

and here:

Wire.begin(sda , scl);

However, I prefer:

const uint8_t sda = D2;
const uint8_t scl = D1;

Wire.begin( sda, scl);


Wire.begin( D2 , D1);

As far as I know, the default is:
SDA = pin D2 = GPIO4
SCL = pin D1 = GPIO5

I think the Wire.begin() without parameters will set the default:


Here is an example with only Wire.begin() without parameters: ESP8266 NodeMCU MPU-6050 Accelerometer and Gyroscope (Arduino) | Random Nerd Tutorials.
The Wire.begin() without parameters is inside a Adafruit library.

Did you write the sketch yourself ? I can often spot a few problems with the Wire library, but your sketch is okay :smiley: The confusion with the pin numbers seems to be the only problem.

thank you i used the code from mpu library and connection from the website you recomended and also changed my esp8266 to a new one and it worked. Dont know which was the solution but its working

thank you for your help

Dont know which was the solution but its working

Good enough for me ! I'm glad it is working :smiley:

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