Hi,
i had previously open a post :
http://forum.arduino.cc/index.php?topic=259533.0
regarding connecting multiple MPU6050.
I need to connect 4 MPU6050 in one arduino uno board. So there are few people provided me few suggestions in the previous post.
According to thier method, I can use a multiplexer (74HC4051N in my case) and select the AD0 pin to address low (0x68) once at a time. So i followed the instruction and the attached diagram is the connection.
I am storing the result into a micro sd card by using the micro sd card shield from sparkfun. I did manage to get results and store in the micro sd card for 4 imu, however I noticed there are many readings are 0 when at rest (which means the raw reading sometime roughly 16384 and sometime 0 when parallel to gravity). And when I tried to only stored the result from 1 imu into micro sd card without using multiplexer, the results is normal (which means the raw reading is roughly 16384 when parallel to gravity) .
So now I am wondering is there anything wrong with the connection or code?
Please help.
The code I am using is as follow:
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <SPI.h>
#include <SD.h>
#define FILE_BASE_NAME "Waist"
const uint8_t CS_PIN = 8;
File file;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";
MPU6050 accelgyro ;
int16_t ax, ay, az, gx, gy, gz, ax1, ay1, az1, gx1, gy1, gz1,ax2, ay2, az2, gx2, gy2, gz2, ax3, ay3, az3, gx3, gy3, gz3;
//ASSIGN DIGITAL PINS (MUX)
int S0 = 5;
int S1 = 6;
int S2 = 7;
unsigned long time;
//MANUALLY CONTROL THE LINE OF MULTIPLEXER TO SELECT THE PIN CONNECTED TO AD0
void out1()
{
digitalWrite(S0, LOW);
digitalWrite(S1, LOW);
digitalWrite(S2, LOW);
}
void out2()
{
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
digitalWrite(S2, LOW);
}
void out3()
{
digitalWrite(S0, LOW);
digitalWrite(S1, HIGH);
digitalWrite(S2, LOW);
}
void out4()
{
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, LOW);
}
void setup()
{
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(38400);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(CS_PIN, OUTPUT);
while(!SD.begin(CS_PIN)) {
;
}
while (SD.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
Serial.println(F("Can't create file name"));
return;
}
}
accelgyro.initialize();
}
void loop()
{
File file = SD.open(fileName, FILE_WRITE);
if (file) {
time=millis();
out1();
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
out2();
accelgyro.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
out3();
accelgyro.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);
out4();
accelgyro.getMotion6(&ax3, &ay3, &az3, &gx3, &gy3, &gz3);
file.print(time);
file.print(",");
file.print(ax);
file.print(",");
file.print(ay);
file.print(",");
file.print(az);
file.print(",");
file.print(gx);
file.print(",");
file.print(gy);
file.print(",");
file.print(gz);
file.print(",");
file.print(ax1);
file.print(",");
file.print(ay1);
file.print(",");
file.print(az1);
file.print(",");
file.print(gx1);
file.print(",");
file.print(gy1);
file.print(",");
file.print(gz1);
file.print(",");
file.print(ax2);
file.print(",");
file.print(ay2);
file.print(",");
file.print(az2);
file.print(",");
file.print(gx2);
file.print(",");
file.print(gy2);
file.print(",");
file.print(gz2);
file.print(",");
file.print(ax3);
file.print(",");
file.print(ay3);
file.print(",");
file.print(az3);
file.print(",");
file.print(gx3);
file.print(",");
file.print(gy3);
file.print(",");
file.print(gz3);
Serial.print(time);
Serial.print(",");
Serial.print(ax);
Serial.print(",");
Serial.print(ay);
Serial.print(",");
Serial.print(az);
Serial.print(",");
Serial.print(gx);
Serial.print(",");
Serial.print(gy);
Serial.print(",");
Serial.print(gz);
Serial.print(",");
Serial.print(ax1);
Serial.print(",");
Serial.print(ay1);
Serial.print(",");
Serial.print(az1);
Serial.print(",");
Serial.print(gx1);
Serial.print(",");
Serial.print(gy1);
Serial.print(",");
Serial.println(gz1);
Serial.print(",");
Serial.print(ax2);
Serial.print(",");
Serial.print(ay2);
Serial.print(",");
Serial.print(az2);
Serial.print(",");
Serial.print(gx2);
Serial.print(",");
Serial.print(gy2);
Serial.print(",");
Serial.print(gz2);
Serial.print(",");
Serial.print(ax3);
Serial.print(",");
Serial.print(ay3);
Serial.print(",");
Serial.print(az3);
Serial.print(",");
Serial.print(gx3);
Serial.print(",");
Serial.print(gy3);
Serial.print(",");
Serial.println(gz3);
file.println();
file.close();
}
else
{
Serial.println("error opening datalog.txt");
}
delay(10);
}
