Go Down

Topic: Multiple MPU6050 Sensors on One Arduino (Read 883 times) previous topic - next topic

matthewclso

Apr 16, 2018, 04:33 am Last Edit: Apr 16, 2018, 04:54 am by matthewclso
I am doing a project that requires 14 GY521 IMU sensors, all interfaced through one Arduino Mega 2560.  After browsing through Arduino Playground and other forum posts, it seems the only way to interface this many is to assign each AD0 to a separate digital output on the Arduino, and then set one sensor at a time to low to read each one individually.  Here is my code:
Code: [Select]
#include <Wire.h>  //imports Wire library
#include <MPU6050.h>
#include <Servo.h>
MPU6050 gyro;  //creates new MPU6050 object
int16_t ax, ay, az;  //creates angle variables
int16_t gx, gy, gz;

void setup() {  //setup method
  Serial.begin(9600);
  Wire.begin();  //starts I2C communication
  gyro.initialize();  //initializes MPU6050 sensor
  for (int i = 22; i <= 35; i++) {
    pinMode(i, OUTPUT);  //defines pins as output pins
  }
}

void loop() {  //main method
  for (int j = 22; j <= 35; j++) {
    for (int k =  22; k <= 35; k++) {
      digitalWrite(k, HIGH);  //sets all sensors to high address
    }
    digitalWrite(j, LOW);  //sets one sensor to low address
    gyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);  //calls values from sensor
    ax = map(ax, -17000, 17000, 0, 180);  //maps x axis rotation values from 0 to 180
    Serial.println(" ");
    Serial.print(ax);
    ay = map(ay, -17000, 17000, 0, 180);
    Serial.print("  ");
    Serial.print(ay);
    az = map(az, -17000, 17000, 0, 180);
    Serial.print("  ");
    Serial.println(az);
  }
}


For some reason I am not getting any output whatsoever when I run this program.  I'm pretty sure all the sensors work, but is this the right way to go about the programming?  I'm a bit new to Arduino, but it seems to me that this is the only way to go about this.

jremington

#1
Apr 16, 2018, 04:42 am Last Edit: Apr 16, 2018, 04:44 am by jremington
How is it supposed to work, that you set pinMode for pins 22-35, then write to pins 0-14?

matthewclso

#2
Apr 16, 2018, 04:53 am Last Edit: Apr 16, 2018, 05:00 am by matthewclso
Wow, I did not realize how stupid that was.  However, after I fixed that, it still does not print continuously.  It prints 4 lines at most and then stops.  Maybe 14 sensors is too much for I2C to take?

Wawa

Maybe 14 sensors is too much for I2C to take?
No, but 14  4k7 or 2k2 pull up resistors on the SDA and SCL lines in parallel might be too much.

Always post links to the exact modules you're using.
And a diagram, and/or a clear picture of the setup.
Leo..

matthewclso

Here is the schematic for the IC I am using.


Attached is a basic diagram of my setup.  Only 4 sensors are drawn, but it's the same setup with 14.  VCC, GND, SDA, SCL are all wired in series to the Arduino.  Each AD0 is attached to a different digital output.

Wawa

So you're using a module with 4k7 pull up resistors.
That's a combined pull up resistance of 4700 / 14 = ~335 ohm,
while the minimum value for 3.3volt standard I2C is 1100ohm (3mA).
But wait, you're using a 5volt Arduino (Mega) for a 3.3volt module. How did you solve that.
Or did you use modules with I2C level shifters (I asked for a link to the modules you're using).
Leo..

matthewclso

This is the unit I bought:
https://m.aliexpress.com/item/32276121395.html

I'm a bit new to electronics, won't it still work with 5V?

Wawa

Module seems to have a 3.3volt regulator (5-pin chip), but no level shifters on the I2C and address lines.
You might already have damaged the module(s) by connecting it to a 5volt processor.

The SDA and SCL lines have 2k2 resistors, so a combined pull up resistance of 2200/14 = 157 ohm.

I think the easiest way to solve this is with two translating (level shifting) I2C muxers.
https://www.adafruit.com/product/2717
If you go that way, you can also use a much smaller Arduino Nano/ProMini.
Leo..


Koepel

If 5V was pushed into the AD0 of all the MPU-6050, then they all might be broken / damaged / no longer reliable.

Can you buy new sensors ?
You could also buy a 3.3V Arduino board.

How often do you want the data ? The I2C bus is slow and 14 sensors means 14 times slower.

What about a 3.3V Arduino board like the Arduino Due and the MPU-9250 in SPI mode ?

matthewclso

#9
Apr 16, 2018, 08:02 pm Last Edit: Apr 16, 2018, 08:04 pm by matthewclso
I understand - because the Arduino has 5V outputs, but sensor requires 3.3V signals, it's not working?

So, assuming that all my sensors are working, if I replace the Arduino's voltage regulator to a 3.3V one, following this procedure: https://learn.adafruit.com/arduino-tips-tricks-and-techniques/3-3v-conversion , it should work?

Thanks for the replies.

Koepel

The Arduino Mega 2560 has pins that can deliver 5V up to 40 mA. Pushing that into a pin of the MPU-6050 can damage it, because the MPU-6050 runs at 3.3V.

I don't like that Adafruit conversion. They remove the polyfuse. The ATmega328P can not run at 16 MHz at 3.3 V according to the datasheet. In most cases it will work though.

Wawa

If you use a 3.3volt Arduino, then you still have the problem of the I2C pull up resistors.
You can only leave two 2k2 sets.
Leo..

jremington

#12
Apr 17, 2018, 12:46 am Last Edit: Apr 17, 2018, 12:48 am by jremington
Quote
I am doing a project that requires 14 GY521 IMU sensors
What project could possibly require 14 IMUs, and even if so, why does it have to go through one Arduino? You can buy a Pro Mini on eBay for $2!

matthewclso

#13
Apr 17, 2018, 12:57 am Last Edit: Apr 17, 2018, 12:59 am by matthewclso
What project could possibly require 14 IMUs, and even if so, why does it have to go through one Arduino? You can buy a Pro Mini on eBay for $2!
I'm making a motion tracker as input for a robotic hand.  I figured it'd be easier to route all the data through one device, instead of say, 7 Pro Minis.

If you use a 3.3volt Arduino, then you still have the problem of the I2C pull up resistors.
You can only leave two 2k2 sets.
Leo..
I still don't understand what you mean by the I2C pull up resistors.  Using 3.3V Arduino, you can set the AD0 on the sensors, no?

jremington

#14
Apr 17, 2018, 01:12 am Last Edit: Apr 17, 2018, 01:29 am by jremington
Quote
I still don't understand what you mean by the I2C pull up resistors.
Every sensor, and possibly also the Arduino, has a pullup resistor on each of the I2C lines. They are all in parallel, and that demands far too much current from the I/O lines.

You will have to remove both pullup resistors on at least 13 of the 14 sensors if you insist on this arrangement.

Quote
I figured it'd be easier to route all the data through one device, instead of say, 7 Pro Minis.
Obviously, it is not so easy.


Go Up