Problem Interfacing ESP8266 with MPU6050 thru I2C

I have been trying for two days now to interface my ESP8266 with my MPU6050 (in a GY-521 board) and have been unsuccessful. I managed to get it to work perfectly with my Arduino but can’t get it with the ESP. I have reduced my code down to the following bare-bones snippet and it still doesn’t work:

#include "Wire.h"
const int address=0x68;

void setup() {
  Wire.begin(address);
  Serial.begin(9600);
}

void loop() {
  Wire.beginTransmission(address);
  Serial.print("Return code: ");
  Serial.println(Wire.endTransmission());
  delay(3000);
}

The return code that I get is 4, which according to the Wire library is “other error.” So, I can’t even make a basic connection with the board, let alone get any information from it. My circuit diagram is attached as an image in this post. I also tried the same setup in the picture, but with the SDA and SCL lines pulled up to 3v3 with 10k Ohm resistors, but that didn’t work either. In this circuit diagram shown, AD0 is pulled to ground so as to ensure that the address is 0x68.

If anyone has any ideas as to what may be the problem here, it would be hugely appreciated.

Capture.PNG

ESP-8266 is an IC. Which module do you have ?

.

I'm using a NodeMCU

I have managed to make some progress, and have gotten the ESP to communicate with the MPU to retrieve a single byte, using the following code:

#include <Wire.h>

uint8_t adrs = 0x68;

void setup() {
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  Wire.beginTransmission(adrs);
  Wire.write(0x75);
  Serial.print("Request for WHOAMI register, transmission result: ");
  Serial.println(Wire.endTransmission());
  Wire.requestFrom(adrs, (size_t)1, true);
  byte data =  Wire.read();
  Serial.print("Byte received: ");
  Serial.println(data);
}

So, I implemented what seemed to be right about that snippet into my larger project, did a little debugging, and now I am stuck on this. It is throwing a software watchdog timer when I try to run Wire.requestFrom on the ESP (in the updateData() method). I am printing out the error codes for the config which awakes and configures the module so I can see if the awakening and configuring was successful, i.e. an error code of 0 (in the configureMPU() method), and am still getting this requestFrom error even when it is. The code is as follows:

#include <Wire.h>

int adrs = 0x68;
float pitch, roll, yaw;
float gyrox=0, gyroy=0, gyroz=0;
float gyrox_o, gyroy_o, gyroz_o;
float accx, accy, accz;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  configureMPU();
  calibrateGyro();
}
void configureMPU(){
  Wire.beginTransmission(adrs);
  Wire.write(0x6B);
  Wire.write(0x00);
  Serial.println(Wire.endTransmission());
  Wire.beginTransmission(adrs);
  Wire.write(0x1C);
  Wire.write(0x10);
  Serial.println(Wire.endTransmission());
  Wire.beginTransmission(adrs);
  Wire.write(0x1B);
  Wire.write(0x08);
  Serial.println(Wire.endTransmission());
}
void calibrateGyro(){
  Serial.println("Calibrating gyroscope...");
  double xsum=0;
  double ysum=0;
  double zsum=0;
  int nt=2000;
  for(int i=0;i<nt;i++){
    updateData();
    xsum+=gyrox;
    ysum+=gyroy;
    zsum+=gyroz;
  }
  gyrox_o = xsum/nt;
  gyroy_o = ysum/nt;
  gyroz_o = zsum/nt;
  Serial.println("Calibration complete.");
}
void updateData(){
  Wire.beginTransmission(adrs);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(adrs, 14);
  while(Wire.available()<14){};
  accx = Wire.read()<<8|Wire.read();
  accy = Wire.read()<<8|Wire.read();
  accz = Wire.read()<<8|Wire.read();
  float temperature = Wire.read()<<8|Wire.read();
  gyrox = Wire.read()<<8|Wire.read();
  gyroy = Wire.read()<<8|Wire.read();
  gyroz = Wire.read()<<8|Wire.read(); 
}
void loop() {
  updateData();
}

And the decoded stack trace:

0x402014db: twi_readFrom at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_si2c.c line 246
0x4020313c: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/HardwareSerial.h line 174
0x40202fbc: TwoWire::requestFrom(unsigned char, unsigned int, bool) at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\libraries\Wire\Wire.cpp line 118
0x40202fe7: TwoWire::requestFrom(int, int) at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\libraries\Wire\Wire.cpp line 134
0x40202b94: updateData() at C:\Users\mach\Documents\Arduino\Drone_temp/Drone_temp.ino line 51
0x40202cc1: calibrateGyro() at C:\Users\mach\Documents\Arduino\Drone_temp/Drone_temp.ino line 37
0x40202dc4: setup() at C:\Users\mach\Documents\Arduino\Drone_temp/Drone_temp.ino line 14
0x402038c4: loop_wrapper() at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_main.cpp line 122

In case it is relevant, I also occasionally get the following warning on trying to upload the sketch. It doesn’t even happen all the time, just seemingly randomly:

warning: espcomm_send_command: wrong direction/command: 0x01 0x00, expected 0x01 0x08

Any help would be seriously appreciated at this point, this has been driving me crazy for 3 days now.