raym3d
March 15, 2022, 3:35am
1
I am trying to start 2 MPU6050 sensors with 1 ESP32. My code works perfectly with one, but I don't know how to make it work with 2.
As you know, the connections of an MPU6050 are simple:
VCC > 3.3V
SDA > SDA
SCL > SCL
GND > GND
Someone help me? My code is:
// Basic demo for accelerometer readings from Adafruit MPU6050
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
Serial.println("");
delay(100);
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
//Acceleration
//Serial.print("Acceleration X: ");
//Serial.print(a.acceleration.x);
//Serial.print(", Y: ");
Serial.println(a.acceleration.y);
//Serial.print(", Z: ");
//Serial.print(a.acceleration.z);
//Serial.println(" m/s^2");
//Serial.print("Rotation X: ");
//Serial.print(g.gyro.x);
//Serial.print(", Y: ");
//Serial.print(g.gyro.y);
//Serial.print(", Z: ");
//Serial.print(g.gyro.z);
//Serial.println(" rad/s");
//Serial.print("Temperature: ");
//Serial.print(temp.temperature);
//Serial.println(" degC");
//Serial.println("");
delay(200);
}
When I connect 2, this just show 1: 0x68
raym3d
March 15, 2022, 6:43am
2
I think this can work. Correct me if I'm wrong. Why does he connect the AD0 and not the VCC on the 2nd MPU6050?
And this: how to connect multiple mpu6050 with a single arduino board - YouTube
After you've done an I2C scan and find there is only one MPU connected, even though you wired them both into the circuit, on one of the modules, ground ADO to change its I2C address. Run the I2C scan again to see if 2 modules show up as being detected.
1 Like
ftjuh
March 15, 2022, 1:00pm
4
If you want to run multiple devices on one I2C bus, each has to have a distinct I2C address.
It can be changed to 0x69 by pulling the AD0 pin up to +3.3V (not low as stated by @Idahowalker ). This is what you see in your youtube screenshot, only that they forgot to power the upper device, so connect +3.3V to AD0 and VCC. To make sure that the other device is 0x68, AD0 should be pulled low to GND, even if it usually will work without that.
In your code, you'll need two objects of type Adafruit_MPU6050, one initiated with the default value 0x68 (no parameter), the other explicitly with 0x69. Look up the library's documentation to find out how to do that.
Jan
1 Like
raym3d
March 15, 2022, 1:33pm
5
Hello. I already got it working with this one: MuttipleMPU/MuttipleMPU.ino · master · Shuvashish Sarker / batikkrom.com · GitLab
But, the values that I receive are a bit strange, I prefer to use the Adafruit library, can you help me to divide the sensors?
One is 0x69 and the other is 0x68.
I already uploaded the adafruit code above.
In the documentation I found only this: https://github.com/adafruit/Adafruit_MPU6050/blob/master/Adafruit_MPU6050.h#L191
Source: Multiple MPUs in the same code? · Issue #4 · adafruit/Adafruit_MPU6050 · GitHub
I don't understand this, explain it to me, please. Should I connect VCC to 3.3V and AD0? Because I only connected AD0 and apparently it's fine, it powered up fine.
ftjuh
March 15, 2022, 1:42pm
6
See my above reply about the code and here on how to set up the second sensor with another address .
Yes, your device needs power. If it runs anyway, it's a coincidence. You'd have to look at your boards schematics to understand why.
raym3d
March 15, 2022, 2:05pm
7
I'm completely inexperienced, so please correct me if I'm wrong.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 accelgyroIC1(0x68);
Adafruit_MPU6050 accelgyroIC2(0x69);
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!accelgyroIC1.begin() && !accelgyroIC2.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
....
Is that the only thing I should place or should I do something else?
That is, both VCC and AD0 are connected to the 3.3V input, right?
ftjuh
March 15, 2022, 2:15pm
8
No, this class's constructor doesn't use parameters .
make it
Adafruit_MPU6050 accelgyroIC1;
Adafruit_MPU6050 accelgyroIC2;
raym3d:
accelgyroIC2.begin())
make that accelgyroIC2.begin(0x69))
else both will use the default 0x68 (see the docs linked above), and put it into a seperate failure-check (or use ||, not &&).
After that, you'll need to do everything twice, once with accelgyroIC1, once with accelgyroIC2.
To the 3.3V pin, yes.
raym3d
March 15, 2022, 2:29pm
9
I did not understand this part, but tell me if I have any errors in the code, please
Adafruit_MPU6050 accelgyroIC1;
Adafruit_MPU6050 accelgyroIC2;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!accelgyroIC1.begin() || !accelgyroIC2.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
ftjuh
March 15, 2022, 2:51pm
10
if (!accelgyroIC1.begin() || !accelgyroIC2.begin(0x69)) {
Serial.println("Failed to find one or both MPU6050 chip(s)");
while (1) {
delay(10);
}
}
Serial.println("Both MPU6050s found!");
raym3d
March 15, 2022, 2:57pm
11
I'm going to share my code with you, but it doesn't work. That is, it does not throw errors, but it does not print anything in the console, only the scripts of the loop
I'm going to share my code with you, but it doesn't work. That is, it does not throw errors, but it does not print anything in the console, only the scripts of the loop
// Basic demo for accelerometer readings from Adafruit MPU6050
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
//Adafruit_MPU6050 mpu;
Adafruit_MPU6050 MPU1;
Adafruit_MPU6050 MPU2;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!MPU1.begin() || !MPU2.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
MPU1.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (MPU1.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
MPU1.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (MPU1.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
MPU1.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (MPU1.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
MPU2.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (MPU2.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
MPU2.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (MPU2.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
MPU2.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (MPU2.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
Serial.println("");
delay(100);
}
void loop() {
GetMPU1;
GetMPU2;
Serial.println("-----------");
delay(500);
}
void GetMPU1(){
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
MPU1.getEvent(&a, &g, &temp);
/* Print out the values */
Serial.print("Acceleration X: ");
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2");
Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" rad/s");
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" degC");
}
void GetMPU2(){
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
MPU2.getEvent(&a, &g, &temp);
/* Print out the values */
Serial.print("Acceleration X: ");
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2");
Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" rad/s");
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" degC");
}
ftjuh
March 15, 2022, 3:16pm
12
Your output tells you that nothing is happening between
raym3d:
Serial.println("");
and
Serial.println("-----------");
which should tell you that your two functions are never called, because these
raym3d:
GetMPU1;
GetMPU2;
are no function calls. These are:
GetMPU1();
GetMPU2();
Edit: Set your compiler warnings to "all" in the Arduino settings to be warned about stuff like this.
And you forgot the 0x69 again, here:
!MPU2.begin()
I feel this is a good moment to leave you on your own, as your initial problem should be solved. Good luck with your project.
1 Like
raym3d
March 15, 2022, 3:22pm
13
Now it does output data, but I think there is an error:
If I move one, the other also changes.
The values are not the same as they were when I had only 1 connected.
I don't understand this part, what should I do?
I have this:
if (!MPU1.begin() || !MPU2.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
ftjuh
March 15, 2022, 3:26pm
14
You should have this .
Over and out.
1 Like
raym3d
March 15, 2022, 3:28pm
15
THANK YOU VERY MUCH. Everything works perfectly.
if (!MPU1.begin() || !MPU2.begin(0x69)) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Look at the connections, one must be connected in ADO, not in VVC.
can u share the code boz I'm unable to get data from the second mpu
1 Like
I tried to modify and put the 0x69 in the code and it didn't run, it says it's out of scope. Do you know of any possible solution? or could you share your code?
1 Like
another thing, what pins did you use to connect?
used the same on both accelerometers?
if yes which ones?
i used pin 22 for SCL and pin 21 for SDA on both sensors on esp 32
1 Like
system
Closed
April 22, 2023, 9:13pm
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.