-->
Page 1 of 1

MPU6050 i2c Register access X,Y,Z - A+G & Temp

PostPosted: Mon Nov 16, 2015 8:21 pm
by GengusKahn
Very simple sketch to access the raw data from the cheap MPU6050 to allow the feedback to be provided to motors or pixels.......or any way you like.......

Code: Select all
#include <ESP8266WiFi.h>
#include <Wire.h>
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);                                   // PWR_MGMT_1 register
  Wire.write(0);                                     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
}

void loop(){
  delay(1000);
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);                                  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 14, true);                   // request a total of 14 registers or What you need
  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);
}


Re: MPU6050 i2c Register access X,Y,Z - A+G & Temp

PostPosted: Tue Oct 24, 2017 2:38 am
by AhmedAli`
I'm trying to use both of the mpu and esp at the same time , but when i do i'm unable to receive date form the mpu (i get -1) . I set the board : generic ESP8266 . do u know what i'm doing wrong ?