I managed to create a script with 3 running MPU-6050's on a TCA9548A Multiplexer, attached code below.
I need 8 MPU-5060 in total however I can already tell that the sampling freq. will decrease significantly see graph below.
Can I somehow increase the sampling frequency?
PLOTTER:
#include <Wire.h>
#include <MPU6050.h>
#define TCA_ADDRESS 0x70 // Address of the TCA9548A multiplexer
#define NUM_MPU 3
MPU6050 mpu[NUM_MPU];
int16_t accelX[NUM_MPU], accelY[NUM_MPU], accelZ[NUM_MPU];
void setup() {
Wire.begin();
Serial.begin(115200);
delay(1000);
Serial.println("Begin");
// Initialize all MPU sensors
for (int i = 0; i < NUM_MPU; i++) {
TCASelect(TCA_ADDRESS, i);
mpu[i].initialize();
}
}
void loop() {
// Read accelerometer data from all connected MPU sensors
for (int i = 0; i < NUM_MPU; i++) {
TCASelect(TCA_ADDRESS, i); // Select the channel for the current MPU
readAccelData(mpu[i], accelX[i], accelY[i], accelZ[i]);
}
// Print the acceleration values for all sensors on the same line with tabs
for (int i = 0; i < NUM_MPU; i++) {
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(":\t");
printWithPrecision(accelX[i] / 16384.0, 6);
printWithPrecision(accelY[i] / 16384.0, 6);
printWithPrecision(accelZ[i] / 16384.0, 6);
if (i < NUM_MPU - 1) {
Serial.print("\t| ");
}
}
Serial.println(); // Move to the next line for the next set of readings
delay(100); // Adjust the delay based on your application
}
void TCASelect(uint8_t i2c_address, uint8_t bus) {
Wire.beginTransmission(i2c_address);
Wire.write(1 << bus);
Wire.endTransmission();
}
void readAccelData(MPU6050& mpu, int16_t& accelX, int16_t& accelY, int16_t& accelZ) {
mpu.getAcceleration(&accelX, &accelY, &accelZ);
}
void printWithPrecision(float value, int digits) {
Serial.print(value, digits);
Serial.print("\t");
}
LOGDATA:
#include <Wire.h>
#include <MPU6050.h>
#include <SD.h>
#define TCA_ADDRESS 0x70 // Address of the TCA9548A multiplexer
#define NUM_MPU 1
MPU6050 mpu[NUM_MPU];
int16_t accelX[NUM_MPU], accelY[NUM_MPU], accelZ[NUM_MPU];
const int CS_PIN = 53; // Chip Select pin for SD card module
File dataFile; // File object to write data to SD card
uint32_t counter = 0;
unsigned long startTime;
const unsigned long loggingDuration = 10 * 1000000; // Logging duration in microseconds (10 seconds)
bool loggingStarted = false;
void setup() {
Wire.begin();
Serial.begin(115200);
// Initialize SD card
if (!SD.begin(CS_PIN)) { // Use the CS (Chip Select) pin number you have connected to on your Arduino
Serial.println("SD initialization failed!");
while (1); // Stop further execution if SD initialization fails
}
// Initialize all MPU sensors
for (int i = 0; i < NUM_MPU; i++) {
TCASelect(TCA_ADDRESS, i);
mpu[i].initialize();
}
delay(1000);
Serial.println("Type 'Start' to begin data collection.");
delay(1000);
}
void loop() {
if (!loggingStarted) {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
if (input == "Start") {
startDataCollection();
}
}
} else {
// Read accelerometer data from all connected MPU sensors
for (int i = 0; i < NUM_MPU; i++) {
TCASelect(TCA_ADDRESS, i); // Select the channel for the current MPU
readAccelData(mpu[i], accelX[i], accelY[i], accelZ[i]);
}
// Open the file in append mode
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
// Print the acceleration values for all sensors along with the current time to the file
dataFile.print(micros() - startTime);
dataFile.print("\t");
for (int i = 0; i < NUM_MPU; i++) {
dataFile.print(accelX[i] / 16384.0, 6);
dataFile.print("\t");
dataFile.print(accelY[i] / 16384.0, 6);
dataFile.print("\t");
dataFile.print(accelZ[i] / 16384.0, 6);
if (i < NUM_MPU - 1) {
dataFile.print("\t ");
}
}
dataFile.println();
dataFile.close();
} else {
Serial.println("Error writing to data.txt");
}
// Check if the logging duration has been reached
if (micros() - startTime >= loggingDuration) {
Serial.println("Logging complete. Exiting...");
while (1); // Stop further execution
}
}
delay(0.001); // Adjust the delay based on your application
}
void TCASelect(uint8_t i2c_address, uint8_t bus) {
Wire.beginTransmission(i2c_address);
Wire.write(1 << bus);
Wire.endTransmission();
}
void readAccelData(MPU6050& mpu, int16_t& accelX, int16_t& accelY, int16_t& accelZ) {
mpu.getAcceleration(&accelX, &accelY, &accelZ);
}
void startDataCollection() {
Serial.println("Recording data...");
dataFile = SD.open("data.txt", FILE_WRITE);
// Check if the file opened successfully
if (dataFile) {
dataFile.println("Time \t AccelX1 \t AccelY1 \t AccelZ1 \t AccelX2 \t AccelY2 \t AccelZ2 \t AccelX3 \t AccelY3 \t AccelZ3");
dataFile.close();
loggingStarted = true;
startTime = micros(); // Record the start time in microseconds
} else {
Serial.println("Error opening data.txt");
}
}