Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1149

Problem with MPU6050 sensor

$
0
0

I've connected two MPU6050 sensors to one Arduino UNO board, and connected the ADO pins to GND and 3.3V respectively. For some reason, both sensor values go to the same ADO value and whenever I wave the MPU6050 sensor a bit too much, both values go to 0 and don't move from there. What could be causing this issue?

here's the code I'm using:

#include <Wire.h>
#include <SoftwareSerial.h>
#include <MPU6050.h>
SoftwareSerial BTSerial(10, 11);

MPU6050 mpu1(0x68); // Address 0x68
MPU6050 mpu2(0x69); // Address 0x69

void setup() {
  Wire.begin();
  BTSerial.begin(9600);
  Serial.begin(9600);
  mpu1.initialize();
  mpu2.initialize();

  if (!mpu1.testConnection()) {
    Serial.println("MPU6050 #1 connection failed");
  } else {
    Serial.println("MPU6050 #1 connected");
  }

  if (!mpu2.testConnection()) {
    Serial.println("MPU6050 #2 connection failed");
  } else {
    Serial.println("MPU6050 #2 connected");
  }
}

void loop() {
  int16_t ax1, ay1, az1;
  int16_t gx1, gy1, gz1;
  int16_t ax2, ay2, az2;
  int16_t gx2, gy2, gz2;

  mpu1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
  mpu2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);

  // Calculate angles (implement your own algorithm here)
  float angle1 = atan2(ay1, az1) * 180 / PI;
  float angle2 = atan2(ay2, az2) * 180 / PI;
  float relativeAngle = angle2 - angle1;

  Serial.print("Angle1: ");
  Serial.print(angle1);
  Serial.print(" Angle2: ");
  Serial.print(angle2);
  Serial.print(" Relative Angle: ");
  Serial.print(relativeAngle);

  if(relativeAngle<-70 && relativeAngle>-110) {
    Serial.println("one joint");
    //BTSerial.write("O");
  } 
  else if(relativeAngle<-130 && relativeAngle>-180) {
    Serial.println("two joint");
    //BTSerial.write("T");
  }
  else {
    Serial.println("no joint");
    //BTSerial.write("N");
  }

  delay(100);
}

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1149

Trending Articles