1

I am trying to receive the MPU6050 data in the Arduino IDE Serial monitor via NodeMCU 8266.

This is the code that I uploaded to the NodeMCU :

#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(115200);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(1000);
}

The output that I am getting at the Serial Port Monitor is enter image description here

The reason why I am using NodeMCU, is so that I can directly push the data to Firebase Cloud, and then retrieve it in an iOS Application.

Where am I going wrong..??

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • I will recommend using proper brackets for this statements where you are constructing 16bit number from 2 registers, like `AcX=Wire.read()<<8|Wire.read()`. Try it like `AcX = ((Wire.read() << 8) | (Wire.read()));` – svtag Nov 01 '18 at 17:21
  • -1 suggests 0xff (all high - i2c slave not responding). Does wire library report any errors (there will be no ACK from slave, if there's some hardware issue)? Schematic would also help. How is AD0 of MPU6050 connected? – domen Nov 02 '18 at 08:49
  • You can use I2C scanner to check whether you are requesting from the right address or not – vaibhav sharma Nov 03 '18 at 07:24

1 Answers1

1

First of all, don't declare any global variable to store your acceleration and gyro value, it can cause a problem in case you have nested loops. It's better to declare them inside the loop only or use the static keyword.

static int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

I had the same problem I resolved it in this way. Your addresses are right but still, you can refer to the datasheet

you can find the full code in this Github link. Here we have got the gyro and accelerometer values from the sensor. and the detailed project is mentioned here

vaibhav sharma
  • 170
  • 1
  • 12