I'm having a really hard time trying to get MPU6050 accelerometer (breakout board GY-521) to work with i2cdevlib (i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub) on Arduino Mega 2560. My main problem seems to be with interrupts: sometimes I can read data without any problems, but after resetting Arduino, all the sketch reads is 0. Strange thing is that even though interrupt should be at pin 2, I can connect to pins next to it (3 or sometimes 4) and I get data again.
When using DMP6 example in the library, it gets stuck on "DMP ready! Waiting for first interrupt..." - that's why I think problem is in the interrupts. I can read raw data without problems.
The parts of my code related to the accelerometer are pretty much identical to the DMP6 example, only my baudrate is 9600 because I'm also using GPS module.
G, I've just started working with the MPU6050 myself, and will take a look at your code. I would recommend one thing, however, up front.
Divide and conquer. Don't try to get long multi-purpose programs to work until you have proofed the individual parts separately. IOW, throw away all of the BMP180 and unrelated stuff, and just get the 6050/DMP stuff to work properly. Once this is done, only then combine that code with everything else. This will make code development many times easier.
I tried that, it works/doesn't work the same way with and without detachInterrupt. Like I said, I only tried if it would make any difference and then forgot it there.
I tried that, it works/doesn't work the same way with and without detachInterrupt. Like I said, I only tried if it would make any difference and then forgot it there.
Understand. But when other problems are removed, it may still be an issue. With it included, the ISR will only be called on the first interrupt. The ISR will detach the interrupt so won't be called again. And I can't see anywhere else in the program where the interrupt gets attached (apart from in setup).
Well, I removed the detachInterrupt and no changes. When I run it with values printing, the fifoCount is 0 and packetSize is always 42. But then it gets to the while loop, which seems to be infinite. When I comment out the while loop (not a good idea, I know), I get some data, however, sometimes it displays values that are way off.
I think what happens is that Arduino is waiting for the interrupt which for some reason never happens. Any ideas why?
When using DMP6 example in the library, it gets stuck on "DMP ready! Waiting for first interrupt..." - that's why I think problem is in the interrupts. I can read raw data without problems.
So when you try the example program from the library (MPU6050_DMP6.ino) without any changes, it also gets stuck waiting for an interrupt. Is that right?
What program did you use to "read the raw data without problems"? Can you post it so we can compare?
About your hardware ... can you post a drawing (photo of hand drawn diagram is fine) of how you have the breakout board connected to the Arduino.
And if you run this I2C scanner, what address(es) does it report?
Hackscribble:
So when you try the example program from the library (MPU6050_DMP6.ino) without any changes, it also gets stuck waiting for an interrupt. Is that right?
Yes, that's exactly what's going on.
Hackscribble:
What program did you use to "read the raw data without problems"? Can you post it so we can compare?
I used the other example from the library, MPU6050_raw.ino. This one doesn't use interrupts and shows some data, so I don't think the problem is in the I2C bus
Hackscribble:
About your hardware ... can you post a drawing (photo of hand drawn diagram is fine) of how you have the breakout board connected to the Arduino.
Sure, I attached a fritzing image.
Hackscribble:
And if you run this I2C scanner, what address(es) does it report?
I get two addresses, one is 0x68 (MPU6050 default address) and the other is 0x77 - pressure sensor I have connected. That one only uses I2C, so it shouldn't matter.
Using the DMP6 example program as the starting point, try modifying the while loop in loop() to check whether the interrupt is happening:
unsigned long lastPrint = millis();
while (!mpuInterrupt && fifoCount < packetSize)
{
if (millis() - lastPrint > 200UL) // print status every 200ms
{
lastPrint = millis();
Serial.print(F("mpuInterrupt: ")); Serial.print(mpuInterrupt);
Serial.print(F(" fifoCount: ")); Serial.print(fifoCount);
Serial.print(F(" packetSize: ")); Serial.println(packetSize);
}
}
If mpuInterrupt is always 0, then check the wiring from INT on the GY-521 to pin 2. And maybe try triggering a spoof interrupt manually using a jumper lead.