I'm trying to get values from multiple MPU6050 gyro sensors with one Arduino board. I've tried I2C communication, but the two sensors are giving their values into one variable instead of the two separate ones I made.
I'm very new to Arduino and I would appreciate easy to understand explanations
here's the code I'm using:
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "SoftwareSerial.h"
#define BT_RXD 8
#define BT_TXD 7
SoftwareSerial bluetooth(BT_RXD,BT_TXD);
MPU6050 mpu1(0x68);
MPU6050 mpu2(0x69);
int16_t ax1, ay1, az1;
int16_t gx1, gy1, gz1;
int16_t ax2, ay2, az2;
int16_t gx2, gy2, gz2;
void setup()
{
Serial.begin(9600);
bluetooth.begin(9600);
Wire.begin();
mpu1.initialize();
if (mpu1.testConnection()) {
Serial.println("MPU6050 1 connected");
} else {
Serial.println("MPU6050 1 connection failed");
}
mpu2.initialize();
if (mpu2.testConnection()) {
Serial.println("MPU6050 2 connected");
} else {
Serial.println("MPU6050 2 connection failed");
}
}
void loop()
{
// Read data from the first MPU6050
mpu1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
// Read data from the second MPU6050
mpu2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);
// Map raw values to -32 to 32 range
int mappedAx1 = map(ax1, -32768, 32767, -32, 32);
int mappedAy1 = map(ay1, -32768, 32767, -32, 32);
int mappedAz1 = map(az1, -32768, 32767, -32, 32);
int mappedGx1 = map(gx1, -32768, 32767, -32, 32);
int mappedGy1 = map(gy1, -32768, 32767, -32, 32);
int mappedGz1 = map(gz1, -32768, 32767, -32, 32);
// Map raw values to -32 to 32 range
int mappedAx2 = map(ax2, -32768, 32767, -32, 32);
int mappedAy2 = map(ay2, -32768, 32767, -32, 32);
int mappedAz2 = map(az2, -32768, 32767, -32, 32);
int mappedGx2 = map(gx2, -32768, 32767, -32, 32);
int mappedGy2 = map(gy2, -32768, 32767, -32, 32);
int mappedGz2 = map(gz2, -32768, 32767, -32, 32);
Serial.print("X1, Y1, Z1 = "); Serial.print(mappedAx1); Serial.print(" "); Serial.print(mappedAy1); Serial.print(" "); Serial.println(mappedAz1);
Serial.print("X2, Y2, Z2 = "); Serial.print(mappedAx2); Serial.print(" "); Serial.print(mappedAy2); Serial.print(" "); Serial.println(mappedAz2);
Serial.print("X2 relative to X1 = "); Serial.println(mappedAx2-mappedAx1);
Serial.print("Y2 relative to Y1 = "); Serial.println(mappedAy2-mappedAy1);
Serial.print("Z2 relative to Z1 = "); Serial.println(mappedAz2-mappedAz1);
delay(500);
}
very rough depiction of the wiring