Connecting two MPU6050 to Arduino Mega

Hello everyone,

I’ve been trying to connect two MPU6050-GY521 sensors to my Arduino Mega 2560 and looking it up but every time I run it with some code I wrote, it shows both MPU6050 values as one reading.

My Connections
3.3V → MPU 1 VCC, MPU 2 VCC, MPU 1 AD0
GND → MPU 1 GND, MPU 2 GND, MPU 2 AD0
SDA(PIN 20) → MPU 1 SDA, MPU 2 SDA
SCL(PIN 21) → MPU 1 SCL, MPU 2 SCL

I also have the code attached that prints the AY-value of each MPU but I’m not sure where I went wrong. Could someone please take me step-by-step through this process with the wiring and the code, I really need it to finish a project I’m working on. Thanks so much!

#include “Wire.h”
#include “I2Cdev.h”
#include “MPU6050.h”

MPU6050 accel1(0x68);
MPU6050 accel2(0x69);
int16_t ax1, ay1, az1;
int16_t gx1, gy1, gz1;
int16_t ax2, ay2, az2;
int16_t gx2, gy2, gz2;

void setup ( )
{
Wire.begin ( );
Serial.begin(38400);
Serial.println ( “Initializing the sensors” );
accel1.initialize();
accel2.initialize();
Serial.println(“Testing device connections…”);
Serial.println(accel1.testConnection() ? “MPU6050 #1 connection successful” : “MPU6050 connection failed”);
Serial.println(accel2.testConnection() ? “MPU6050 #2 connection successful” : “MPU6050 connection failed”);
delay (1000);
Serial.println ( “Taking Values from the sensor” );
delay (1000);
}

void loop ( )
{
accel1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
accel2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);
// display tab-separated accel/gyro x/y/z values
Serial.print(“MPU1:\t”);
Serial.print(ay1); Serial.print("\t");
// display tab-separated accel/gyro x/y/z values
Serial.print(“MPU2:\t”);
Serial.print(ay2); Serial.println("\t");
delay(100);
}

Post the code inline, using code tags, as described in How to use this forum.

Please use code tags when posting code.

The MPU2 ay value is zero, so check all your connections. Remove MPU1 and test MPU2 separately.

Also, check the device address assignments of "MPU1" and "MPU2", as you appear to have the address pin connections swapped.

Thanks so much, I resolved the issue quite quickly. Thanks again!