Adding a second MPU-6050

Background information: I am currently playing around with this code that someone made that uses one mpu 6050 sensor to get angles in the x, y, and z directions and then display them to the user nicely. The code can be found here MEASURING INCLINATION ANGLE (ARDUION+MPU6050) ~ ECWORKS

The question I have is how would you write the code to add a second mpu 6050 sensor to the program? The program works great but I am trying to learn how to use more than one of these sensors. Any help is greatly appreciated and thank you for your patience if I am posting this in the wrong area.

Here is the code:

#include<Wire.h>
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;

void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
axis_X=Wire.read()<<8|Wire.read();
axis_Y=Wire.read()<<8|Wire.read();
axis_Z=Wire.read()<<8|Wire.read();
int xAng = map(axis_X,minVal,maxVal,-90,90);
int yAng = map(axis_Y,minVal,maxVal,-90,90);
int zAng = map(axis_Z,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
Serial.print("Angle of inclination in X axis = ");
Serial.print(x);
Serial.println((char)176);
Serial.print("Angle of inclination in Y axis= ");
Serial.print(y);
Serial.println((char)176);
Serial.print(“Angle of inclination in Z axis= “);
Serial.print(z);
Serial.println((char)176);
Serial.println(”-------------------------------------------”);
delay(1000);
}

Please use code tags when posting code ("</>" button).

The MPU6050 has a device address pin AD0. If you connect that to Vcc (3.3V) on the MPU6050 module, the I2C address becomes 0x69.

The MPU6050 is a 3.3V device, so be sure to use 3.3 to 5V level shifters with a 5V Arduino, if they are not already on the sensor module.

Note: the tutorial you are following has many, many errors. Here is one that uses the correct equations for the tilt angles.

Thanks for the quick reply! :slight_smile:

Thank you for your patience with me. I was clicking the </> button before pasting every line of code but I see that I was not doing it right.

Just to make sure I understand what you’re saying…
If I add another MPU 6050, the hex address of the device will be 0x69?
I see that the current code looks like it is setting the MPU 6050 address to 0x68.

Also, thank you for sharing that tutorial. The reason I was using the one I have is because it did not require any third party libraries.

If I add another MPU 6050, the hex address of the device will be 0x69?

No. The I2C address of the device defaults to 0x68. Connect the AD0 pin to 3.3V to set address 0x69.

I see what you're saying. So if I have the two separate MPU6050s wired correctly I would basically copy and paste the void setup part of the code. One chunk for 0x68 and one chunk for 0x69.