I'm new to the forum and relatively new to Arduino. I'm sorry for any break of the forum rules! I'm having problems with measuring with the MPU6050 and I haven't been able to find a solution so far!
The idea of the project is measuring my washing machine vibrations with the MPU6050 connected to an Arduino UNO. As my washing machine centrifuges at 1Khz, I was planning on measuring with the gyros at 2.6Khz because the accelerometers are only capable of measuring at 1Khz.
My problem is the following. Any time I start measuring, the process suddenly and unexpectedly stops. The amount of data acquired is always random but small, at most 300 measurements. As I'm trying to measure all the washing process I need quite a few more samples! I have set the built-in led to turn on when the measuring process starts and to turn off when it finished, the finishing can be done by reaching the maximum number of samples required(done automatically) or by an external command entered through the terminal. When the problem occurs the built-in led is still on but no new data is printed and the Arduino is not responding to any commands entered through the terminal.
I have tried the following:
measuring at 500 hz only 1 axis, everything works fine.
measuring at 500 hz more than 1 axis, the problem occurs.
measuring at 1Khz either 1 or more axis, the problem occurs.
I think I know where the process stops because inside the readData function I have placed the printing of some numbers. The program always prints the "1" but never the "2", so I assumed the problem is in the line "Wire.requestFrom(MPU_ADDR, 6)".
I have tried to use Wire.write and Wire.print but either worked.
And I was able to get rid of the String package. However, it's still stopping in the same place.
I was doing some more testing. If I just print the numbers that are commented in my programme (those that are 1,2,3 and 4 inside readData function) but not the data at all, the problem remains in the same place. The line "Wire.requestFrom(MPU_ADDR, 6)". Because the last number I always see is the 1, never the others. Could be there the problem?
Please do not use a Wire.endTransmission() after a Wire.requestFrom(). See Common mistake #2.
Please do not use waiting with a while-statement after a Wire.requestFrom(). See Common mistake #1.
This works well to read the accelerometer data from the MPU-6050:
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 6, true); // request a total of 6 registers
int t = Wire.read() << 8;
ax = t | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
t = Wire.read() << 8;
ay = t | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
t = Wire.read() << 8;
az = t | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Axyz[0] = (float) ax;
Axyz[1] = (float) ay;
Axyz[2] = (float) az;
The line below is not guaranteed to work properly, because there is no requirement for left to right processing in C++, and the byte order may come out incorrectly: