/*
Forum: https://forum.arduino.cc/t/2-mpu6050-any-code-is-notworking-at-all/1211476/16
Wokwi: https://wokwi.com/projects/387368306622724097
Special thanks to Uri Shaked for providing the MPU6050 example which I used as a basis
to demo the use of two MPU6050 with one controller
2024/01/19
ec2021
Source: https://mschoeffler.com/2017/10/05/tutorial-how-to-use-the-gy-521-module-mpu-6050-breakout-board-with-the-arduino-uno/
AD0 (If this pin is LOW, the I2C address of the board will be 0x68.
Otherwise, if the pin is HIGH, the address will be 0x69.)
*/
// Start the simulation, click on the MPU6050 part, and
// change the x/y/z values to see changes in the plotter.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu1;
Adafruit_MPU6050 mpu2;
void setup(void) {
Serial.begin(115200);
// Try to initialize!
if (!mpu1.begin(0x68)) {
Serial.println("Failed to find MPU6050 chip on default address");
while (1) {
delay(10);
}
}
if (!mpu2.begin(0x69)) {
Serial.println("Failed to find MPU6050 chip on default address");
while (1) {
delay(10);
}
}
prepareMPU(mpu1);
prepareMPU(mpu2);
Serial.println("");
delay(100);
}
unsigned long lastPrint = 0;
void loop() {
if (millis() - lastPrint >= 1000) {
lastPrint = millis();
printMPUData(mpu1, '1');
printMPUData(mpu2, '2');
}
}
void prepareMPU(Adafruit_MPU6050 &mpu) {
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
}
void printMPUData(Adafruit_MPU6050 &mpu, char c) {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
/* Print out the values */
Serial.print(millis());
Serial.print('\t');
Serial.print(c);
Serial.print('\t');
Serial.print(a.acceleration.x);
Serial.print(",");
Serial.print(a.acceleration.y);
Serial.print(",");
Serial.print(a.acceleration.z);
Serial.print(", ");
Serial.print(g.gyro.x);
Serial.print(",");
Serial.print(g.gyro.y);
Serial.print(",");
Serial.print(g.gyro.z);
Serial.println("");
}