vier GY-521 Module an einen ESP32

Hallo,

kann mir jemand sagen, wie ich vier identische GY-521 Module (MPU6050 Sensor) mit einen ESP32 ansteuern kann?

Auf der folgenden Webseite ist beschrieben, wie mehrere Module über eine I2C Verbindung angesteuert werden können.

Meine Idee wäre:
Zwei GY-521 Module an die I2C-Pins 22/21 mit verschiedenen Registeradressen (0x68 u. 0x69) und zwei GY-521 Module an die I2C-Pins 16/17 mit verschiedenen Registeradressen (0x68 u. 0x69) an den ESP32 anzuschließen.

Leider weiß ich nicht wie ich das ein einem Sketch realisieren soll...

Das mindeste was du tun kannst ist hier im Forum einen Sketch für einen oder zwei GY-521 module posten
und eine Vermutung dazuschreiben wie es gehen könnte.

(deleted)

Vielen Danke für eure Hilfen!

Ich habe jetzt einen Sketch geschrieben der zwei Sensoren auslesen soll.
Beide sind über Pin 21/22 angeschlossen.
Der erste Sensor hat die Ardesse 0x68 und der zweite 0x69 (hier VCC an AD0).

#include <Wire.h>

#define SDA1 21
#define SCL1 22

#define SDA2 17
#define SCL2 16

TwoWire I2COne = TwoWire(0);
TwoWire I2CTwo = TwoWire(1);

int16_t accXOne, accYOne, accZOne;
int16_t tempOne;
int16_t gyrXOne, gyrYOne, gyrZOne;
int16_t accXTwo, accYTwo, accZTwo;
int16_t tempTwo;
int16_t gyrXTwo, gyrYTwo, gyrZTwo;
unsigned long setMillis1;
int timeDelayPrint = 100;

void read11() {
 I2COne.beginTransmission(0x68);
 I2COne.write(0x3B); // ACCEL_XOUT_H
 I2COne.endTransmission(false);
 I2COne.requestFrom(0x68, 7*2, true);
 accXOne = Wire.read()<<8 | Wire.read(); //0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
 accYOne = Wire.read()<<8 | Wire.read(); //0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
 accZOne = Wire.read()<<8 | Wire.read(); //0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
 tempOne = Wire.read()<<8 | Wire.read(); // 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
 gyrXOne = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
 gyrYOne = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
 gyrZOne = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
}

void read12() {
 I2COne.beginTransmission(0x69);
 I2COne.write(0x3B); // ACCEL_XOUT_H
 I2COne.endTransmission(false);
 I2COne.requestFrom(0x68, 7*2, true);
 accXTwo = Wire.read()<<8 | Wire.read(); //0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
 accYTwo = Wire.read()<<8 | Wire.read(); //0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
 accZTwo = Wire.read()<<8 | Wire.read(); //0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
 tempTwo = Wire.read()<<8 | Wire.read(); // 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
 gyrXTwo = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
 gyrYTwo = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
 gyrZTwo = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
}

void setup() {
 Serial.begin(115200);
 I2COne.begin(SDA1,SCL1,400000);
 //I2CTwo.begin(SDA2,SCL2,400000);
}

void loop() {
  read11();
  read12();
  if ((millis() - setMillis1) >= timeDelayPrint) {
    Serial.print("accXOne: "); Serial.print(accXOne); Serial.print("\t");
    Serial.print("accYOne: "); Serial.print(accYOne); Serial.print("\t");
    Serial.print("accZOne: "); Serial.print(accZOne); Serial.print("\t");       
    Serial.print("temperatureOne: "); Serial.print(tempOne); Serial.print("\t");
    Serial.print("gyrXOne: "); Serial.print(gyrXOne); Serial.print("\t");
    Serial.print("gyrYOne: "); Serial.print(gyrYOne); Serial.print("\t");
    Serial.print("gyrZOne: "); Serial.print(gyrZOne); Serial.print("\t");                
    Serial.print("\r\n");
    Serial.print("accXTwo: "); Serial.print(accXTwo); Serial.print("\t");
    Serial.print("accYTwo: "); Serial.print(accYTwo); Serial.print("\t");
    Serial.print("accZTwo: "); Serial.print(accZTwo); Serial.print("\t");       
    Serial.print("temperatureTwo: "); Serial.print(tempTwo); Serial.print("\t");
    Serial.print("gyrXTwo: "); Serial.print(gyrXTwo); Serial.print("\t");
    Serial.print("gyrYTwo: "); Serial.print(gyrYTwo); Serial.print("\t");
    Serial.print("gyrZTwo: "); Serial.print(gyrZTwo); Serial.print("\t");                
    Serial.print("\r\n");
    setMillis1 = millis();
  }
}

Irgendwas scheint hier aber nicht zu stimmen, da immer "-1" ausgegeben wird.

accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1	
accXOne: -1	accYOne: -1	accZOne: -1	temperatureOne: -1	gyrXOne: -1	gyrYOne: -1	gyrZOne: -1	
accXTwo: -1	accYTwo: -1	accZTwo: -1	temperatureTwo: -1	gyrXTwo: -1	gyrYTwo: -1	gyrZTwo: -1

Habt ihr eine Idee was ich falsch mache und wie ich es richtig machen kann?

Ergebnis des I2C Scanners:

Scanning I2C Addresses Channel 1
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. 68 69 .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
Scan Completed, 2 I2C Devices found.

Scanning I2C Addresses Channel 2
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
Scan Completed, 0 I2C Devices found.

Folgernder Sketch gibt die Werte des ersten Sensors aus:

#include "Wire.h"

int16_t accXOne, accYOne, accZOne;
int16_t tempOne;
int16_t gyrXOne, gyrYOne, gyrZOne;
unsigned long setMillis1;
int timeDelayPrint = 100;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.beginTransmission(0x68);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
}

void loop() {
  Wire.beginTransmission(0x68);
  Wire.write(0x3B); 
  Wire.endTransmission(false);
  Wire.requestFrom(0x68, 7*2, true);

  accXOne = Wire.read()<<8 | Wire.read(); //0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accYOne = Wire.read()<<8 | Wire.read(); //0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accZOne = Wire.read()<<8 | Wire.read(); //0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  tempOne = Wire.read()<<8 | Wire.read(); // 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyrXOne = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyrYOne = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyrZOne = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

  if ((millis() - setMillis1) >= timeDelayPrint) {
    Serial.print("accXOne: "); Serial.print(accXOne); Serial.print("\t");
    Serial.print("accYOne: "); Serial.print(accYOne); Serial.print("\t");
    Serial.print("accZOne: "); Serial.print(accZOne); Serial.print("\t");       
    Serial.print("temperatureOne: "); Serial.print(tempOne); Serial.print("\t");
    Serial.print("gyrXOne: "); Serial.print(gyrXOne); Serial.print("\t");
    Serial.print("gyrYOne: "); Serial.print(gyrYOne); Serial.print("\t");
    Serial.print("gyrZOne: "); Serial.print(gyrZOne); Serial.print("\t");                
    Serial.print("\r\n");
    setMillis1 = millis();
  }
}

Serial Monitor:

accXOne: -776	accYOne: -136	accZOne: 17052	temperatureOne: -2768	gyrXOne: -354	gyrYOne: 164	gyrZOne: -100	
accXOne: -688	accYOne: -156	accZOne: 17032	temperatureOne: -2736	gyrXOne: -338	gyrYOne: 118	gyrZOne: -127	
accXOne: -736	accYOne: -88	accZOne: 16968	temperatureOne: -2800	gyrXOne: -336	gyrYOne: 147	gyrZOne: -141	
accXOne: -772	accYOne: -124	accZOne: 16968	temperatureOne: -2800	gyrXOne: -334	gyrYOne: 150	gyrZOne: -124	
accXOne: -780	accYOne: -116	accZOne: 17044	temperatureOne: -2800	gyrXOne: -343	gyrYOne: 141	gyrZOne: -149	
accXOne: -780	accYOne: -132	accZOne: 16928	temperatureOne: -2784	gyrXOne: -354	gyrYOne: 127	gyrZOne: -154	
accXOne: -668	accYOne: -116	accZOne: 17004	temperatureOne: -2752	gyrXOne: -327	gyrYOne: 143	gyrZOne: -139	
accXOne: -796	accYOne: -32	accZOne: 17036	temperatureOne: -2784	gyrXOne: -350	gyrYOne: 156	gyrZOne: -149	
accXOne: -664	accYOne: -160	accZOne: 17064	temperatureOne: -2752	gyrXOne: -353	gyrYOne: 172	gyrZOne: -123	
accXOne: -648	accYOne: -184	accZOne: 16860	temperatureOne: -2768	gyrXOne: -353	gyrYOne: 156	gyrZOne: -149	
accXOne: -748	accYOne: -196	accZOne: 16932	temperatureOne: -2800	gyrXOne: -343	gyrYOne: 172	gyrZOne: -145	
accXOne: -664	accYOne: -68	accZOne: 16972	temperatureOne: -2768	gyrXOne: -354	gyrYOne: 156	gyrZOne: -119	
accXOne: -680	accYOne: -128	accZOne: 17124	temperatureOne: -2768	gyrXOne: -364	gyrYOne: 156	gyrZOne: -133	
accXOne: -704	accYOne: -168	accZOne: 17140	temperatureOne: -2784	gyrXOne: -352	gyrYOne: 136	gyrZOne: -132	
accXOne: -684	accYOne: -88	accZOne: 16904	temperatureOne: -2752	gyrXOne: -342	gyrYOne: 139	gyrZOne: -130	
accXOne: -804	accYOne: -220	accZOne: 17184	temperatureOne: -2752	gyrXOne: -342	gyrYOne: 152	gyrZOne: -126	
accXOne: -700	accYOne: -100	accZOne: 16928	temperatureOne: -2752	gyrXOne: -330	gyrYOne: 141	gyrZOne: -116	
accXOne: -784	accYOne: -144	accZOne: 17036	temperatureOne: -2752	gyrXOne: -344	gyrYOne: 131	gyrZOne: -149	
accXOne: -612	accYOne: -192	accZOne: 16980	temperatureOne: -2752	gyrXOne: -340	gyrYOne: 140	gyrZOne: -155	
accXOne: -684	accYOne: -204	accZOne: 16940	temperatureOne: -2752	gyrXOne: -341	gyrYOne: 126	gyrZOne: -131	
accXOne: -692	accYOne: -168	accZOne: 16856	temperatureOne: -2784	gyrXOne: -363	gyrYOne: 146	gyrZOne: -151	
accXOne: -752	accYOne: -172	accZOne: 17092	temperatureOne: -2784	gyrXOne: -341	gyrYOne: 132	gyrZOne: -125	
accXOne: -728	accYOne: -236	accZOne: 16968	temperatureOne: -2784	gyrXOne: -355	gyrYOne: 123	gyrZOne: -145	
accXOne: -764	accYOne: -156	accZOne: 17024	temperatureOne: -2784	gyrXOne: -339	gyrYOne: 167	gyrZOne: -137	
accXOne: -656	accYOne: -108	accZOne: 17044	temperatureOne: -2752	gyrXOne: -343	gyrYOne: 169	gyrZOne: -111	
accXOne: -752	accYOne: -156	accZOne: 17048	temperatureOne: -2752	gyrXOne: -348	gyrYOne: 141	gyrZOne: -122

Step1: I2C Scanner laden und schauen, ob die Geräte Adressen ansprechbar sind.
Step2: Einen Sensor auslesen
Step3: Zwei Sensoren auslesen
Step4: Vier Sensor auslesen

Step 3:

#include "Wire.h"

int16_t accXOne, accYOne, accZOne;
int16_t tempOne;
int16_t gyrXOne, gyrYOne, gyrZOne;
int16_t accXTwo, accYTwo, accZTwo;
int16_t tempTwo;
int16_t gyrXTwo, gyrYTwo, gyrZTwo;
unsigned long setMillis1;
int timeDelayPrint = 100;

void setup() {
  Serial.begin(115200);
}

void loop() {
  Wire.begin();
  Wire.beginTransmission(0x68);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Wire.beginTransmission(0x68);
  Wire.write(0x3B); 
  Wire.endTransmission(false);
  Wire.requestFrom(0x68, 7*2, true);

  accXOne = Wire.read()<<8 | Wire.read(); //0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accYOne = Wire.read()<<8 | Wire.read(); //0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accZOne = Wire.read()<<8 | Wire.read(); //0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  tempOne = Wire.read()<<8 | Wire.read(); // 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyrXOne = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyrYOne = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyrZOne = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

  Wire.begin();
  Wire.beginTransmission(0x69);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Wire.beginTransmission(0x69);
  Wire.write(0x3B); 
  Wire.endTransmission(false);
  Wire.requestFrom(0x69, 7*2, true);

  accXTwo = Wire.read()<<8 | Wire.read(); //0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accYTwo = Wire.read()<<8 | Wire.read(); //0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accZTwo = Wire.read()<<8 | Wire.read(); //0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  tempTwo = Wire.read()<<8 | Wire.read(); // 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyrXTwo = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyrYTwo = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyrZTwo = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

  if ((millis() - setMillis1) >= timeDelayPrint) {
    Serial.print("accXOne: "); Serial.print(accXOne); Serial.print("\t");
    Serial.print("accYOne: "); Serial.print(accYOne); Serial.print("\t");
    Serial.print("accZOne: "); Serial.print(accZOne); Serial.print("\t");       
    Serial.print("temperatureOne: "); Serial.print(tempOne); Serial.print("\t");
    Serial.print("gyrXOne: "); Serial.print(gyrXOne); Serial.print("\t");
    Serial.print("gyrYOne: "); Serial.print(gyrYOne); Serial.print("\t");
    Serial.print("gyrZOne: "); Serial.print(gyrZOne); Serial.print("\t");                
    Serial.print("\r\n");

    Serial.print("accXTwo: "); Serial.print(accXTwo); Serial.print("\t");
    Serial.print("accYTwo: "); Serial.print(accYTwo); Serial.print("\t");
    Serial.print("accZTwo: "); Serial.print(accZTwo); Serial.print("\t");       
    Serial.print("temperatureTwo: "); Serial.print(tempTwo); Serial.print("\t");
    Serial.print("gyrXTwo: "); Serial.print(gyrXTwo); Serial.print("\t");
    Serial.print("gyrYTwo: "); Serial.print(gyrYTwo); Serial.print("\t");
    Serial.print("gyrZTwo: "); Serial.print(gyrZOne); Serial.print("\t");                
    Serial.print("\r\n");
    
    setMillis1 = millis();
  }
}

Serial Monitor:

accXOne: -728	accYOne: -180	accZOne: 17140	temperatureOne: -2656	gyrXOne: -349	gyrYOne: 133	gyrZOne: -114	
accXTwo: 392	accYTwo: -56	accZTwo: 16160	temperatureTwo: -976	gyrXTwo: -31	gyrYTwo: -217	gyrZTwo: -114	
accXOne: -664	accYOne: -156	accZOne: 16876	temperatureOne: -2672	gyrXOne: -327	gyrYOne: 170	gyrZOne: -125	
accXTwo: 384	accYTwo: -48	accZTwo: 16360	temperatureTwo: -976	gyrXTwo: -32	gyrYTwo: -205	gyrZTwo: -125	
accXOne: -736	accYOne: -60	accZOne: 17028	temperatureOne: -2656	gyrXOne: -335	gyrYOne: 158	gyrZOne: -141	
accXTwo: 308	accYTwo: -76	accZTwo: 16196	temperatureTwo: -992	gyrXTwo: -51	gyrYTwo: -185	gyrZTwo: -141	
accXOne: -600	accYOne: -120	accZOne: 17024	temperatureOne: -2656	gyrXOne: -334	gyrYOne: 149	gyrZOne: -130	
accXTwo: 352	accYTwo: 60	accZTwo: 16312	temperatureTwo: -976	gyrXTwo: -40	gyrYTwo: -197	gyrZTwo: -130	
accXOne: -640	accYOne: -140	accZOne: 17108	temperatureOne: -2688	gyrXOne: -337	gyrYOne: 159	gyrZOne: -153	
accXTwo: 276	accYTwo: 80	accZTwo: 16340	temperatureTwo: -960	gyrXTwo: -21	gyrYTwo: -196	gyrZTwo: -153	
accXOne: -692	accYOne: -212	accZOne: 16988	temperatureOne: -2688	gyrXOne: -357	gyrYOne: 134	gyrZOne: -120	
accXTwo: 280	accYTwo: -96	accZTwo: 16188	temperatureTwo: -912	gyrXTwo: -29	gyrYTwo: -205	gyrZTwo: -120	
accXOne: -632	accYOne: -88	accZOne: 17012	temperatureOne: -2672	gyrXOne: -353	gyrYOne: 153	gyrZOne: -115	
accXTwo: 244	accYTwo: -76	accZTwo: 16208	temperatureTwo: -944	gyrXTwo: -45	gyrYTwo: -205	gyrZTwo: -115

Habe es jetzt hinbekommen!

Vielen Dank für die Hilfe!

Und danach wäre es schön, wenn Du den funktionierenden Sketch hier vorstellst.

Gruß Tommy