NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Sensors, Actuators, and Robotics » i2c MPU6050 help
October 24, 2012 by unnamed0404 ![]() |
Hey guys, I'm trying to get the ATMega168 to talk to a MPU6050 accelerometer/gyro via i2c. The micro controller connections are basically the same as nerdkits standard setup, and I have pins 27 and 28 (SCL and SDA) going to the MPU6050. The MPU6050 I have is this breakout module: https://www.sparkfun.com/products/11028 I have my microcontroller running at 5V, while the MPU6050 runs at 3.3V, so I've implemented a level shifter as described here: http://www.kip.uni-heidelberg.de/lhcb/Publications/external/AN97055.pdf also with 3.3k pullup resistors for each side. I'm using peter fleury's avr i2c library, and ive tried to model my code pretty closely to this nunchuck example here: http://www.nerdkits.com/forum/thread/972/ I uploaded the code, and tried to just do a quick initial test, (just reading address from my MPU6050), but it fails. I did checked the voltages on my SCL and SDA lines, and noticed that SDA was stuck at high, while SCL was stuck at low. Has anyone seen this problem before? Any idea what could be wrong? I will upload my code a little later Thanks |
||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
October 24, 2012 by unnamed0404 ![]() |
And here is my code First the main file (which currently doesn't do anything except initialize everything and do a setup of the i2c and MPU6050)
And here is peter fleury's i2cmaster.h, which I kept the same except for adding a couple of new functions towards the end
And here is twimaster.c, once again pretty much the same as Peter Fleury's except for a couple of added functions at the end.
Next is MPU6050.h, which I took from Jeff Rowberg. This header just defines all the register addresses for the MPU6050.
And finally, here is MPU6050.c, which currently only includes functions for the setup and test of the MPU6050
I can't seem to be able to write or read from the MPU6050, and when I check the voltages of the SCL and SDA lines, SCL seems to be stuck pulled low and SDA is stuck pulled high. Anyone see anything wrong w/ my code ?(sorry I know its long, but I only changed small sections of it) |
||||||||||
October 24, 2012 by Noter ![]() |
There is a lot of discussion and at least a few working examples of I2c here in the forum. A simple master/slave example is in the library too. Have you tried to get any of the existing examples working before jumping into your MPU6050 application? |
||||||||||
October 25, 2012 by Ralphxyz ![]() |
Also do you have a data analyzer available. I know you are using a multimeter and thinking you are seeing stuck highs and lows but the multimeter will not see a rapid changing voltage or at best will show you an average of what it thinks it is seeing. If you are on the east end of Long Island I'll loan you my data analyzer, it helped me when I had problems with I2C. Ralph |
||||||||||
October 25, 2012 by esoderberg ![]() |
Unnamed, I'm using the TWI Master/Slave code posted in the library in conjunction with the MPU6050. Some of the major elements in addition to the TWI code in the library that you might find helpful posted below (an init function, calibration function, and a register read function for accel). Eric
|
||||||||||
October 26, 2012 by unnamed0404 ![]() |
hey ralph, unfortunately I live about as far away as I could be from long island (I'm in CA), but thanks for the offer. I did check the voltages w/ an oscilloscope however, so it does look stuck. There seems to be a slight oscillation at a freq of 100MHz with an amplitude of a few tens of millivolts on the SCL line, not sure what that means. Eric, thanks for your suggestion. I remember seeing a post of yours regarding using the ATmega168 w/ MPU6050, which confirmed for me that it could be done. I will try implementing those functions and see if it helps. |
||||||||||
October 29, 2012 by unnamed0404 ![]() |
Per noter's suggestion, I decided to get a wii nunchuck, and just used Rick's initial code in his i2c post here: http://www.nerdkits.com/forum/thread/972/ which is definitely supposed to work. Unfortunately, I get one of the error codes when I run: "2nd start bad." It looks like the first i2c start is good, but when it goes into the while loop, the 2nd start fails. Why would it do that? One concern of mine is that in order to connect the nunchuck up to the breadboard, I had to cut off end of the cable to access the clock and data lines, and it turns out the wires are extremely thin and in a bundle. It was rather difficult to try to stuff them into the breadboard, but i think I managed to do it, albeit sloppily. If wiring or something with my level shifter was wrong though, I would expect the first start to fail too..but it seems its only failing the 2nd. Did any of you guys see this problem when you were implementing the nunchuck? Or any suggestions on what could be wrong? |
||||||||||
November 06, 2012 by Ralphxyz ![]() |
Hi unnamed0404, yep I remember getting that error, but I tend to get every possible error. I can not remember the solution, but just wanted to encourage you to press on, the code definitely works! Ralph |
||||||||||
November 20, 2012 by unnamed0404 ![]() |
Hi Ralph, Do you remember if you had to make changes to the actual code to make it work? Or did it have something to do with your hardware connections? I'm assuming hardware since the code has been proven to work for multiple people. Thanks, Chris |
||||||||||
November 20, 2012 by Ralphxyz ![]() |
If I remember correctly, I "thought" it had to be the code, but eventually figured out it was something I was doing (as usual). It probable was wiring but I really do not remember. The code I am sure works as is (thanks Rick). Ralph |
||||||||||
December 01, 2012 by unnamed0404 ![]() |
Yes! I finally got Rick's code working. Turns out it was the wiring. Initially I had cut up the wire and tried to manually insert them into my breadboard, but it turns out the wires were made of up many tiny wires wrapped together, so cutting it open made it messy and difficult to deal with. I bought the adapter from sparkfun and after that it worked fine. Is there a rule of thumb you guys use for where to add those delays? Or do we just add them randomly? I noticed in Rick's code here: http://www.nerdkits.com/forum/thread/972/ he added a 500 us delay before reading from i2c. |
||||||||||
December 01, 2012 by Ralphxyz ![]() |
If I remember correctly the 500 us delay before reading I2C was to allow everything to settle down. Definitely not a "random" kinda thing but in this spot essential. You could probable play with the timing (350 us etc.) but a delay here is required. Ralph |
||||||||||
December 01, 2012 by unnamed0404 ![]() |
Ah i see, thanks, ill keep that in mind. So since the wii example worked, confirming my level shifter and microcontroller were working, I switched back to the MPU-6050 and made some more tweaks to the code. I was able to get a good first start, writing to the MPU-6050 and getting acknowledgement. However, when I tried to read the address register, it did not seem to give me anything. Since I got an acknowledgement for the first "i2c_start," it means the MPU-6050 should be partially working at least right? I'm a little worried that I might have a defective chip since reading the address from it is like the very first step. |
||||||||||
January 24, 2013 by unnamed0404 ![]() |
Hey Guys, I finally got it working, so now I am reading data from the accelerometer and displaying it on the LCD. I was wondering however, is there a better way for me to debug? Currently, for every change in code I make, I'm uploading the code to the microcontroller (therefore doing an erase and write cycle in flash memory). This seems kind of wasteful considering the microcontroller has a specified number of max erase/write cycles. Is there an environment I can use to simulate both the MCU and MPU-6050, so that i don't have to keep programming the flash for every little change i make? Thanks, Chris |
||||||||||
January 24, 2013 by pcbolt ![]() |
Chris - The only debug environment I've seen that may help you is the AVR Studio and the AVR Dragon programmer. There is a You Tube Video Here that show how it's set up and there is a second video in the series that shows how to do some basic debugging. I'm sure there are other emulators/debuggers but I'm not familiar with them. |
||||||||||
January 24, 2013 by Noter ![]() |
Isn't the flash good for about 100,000 uploads? You'll probably wear out your PC before that happens and even if you reach the limit a new chip is only a few bucks. Personally I don't worry about it because I usually fry my chip long before 100K uploads anyway. |
||||||||||
January 24, 2013 by JimFrederickson ![]() |
The "Program Flash" is good for at least 10,000 write cycles. The "EEPROM" is good for a at least 100,000 write cycles.] So yes if you wrote your program to your Microcontroller everyday of the year about 28 times the flash may fail in that year. As noted there are hardware options that get around that issue/problem. (Avoid...) But maybe using up, killing, a few extra of the DIP style chips may extend the life of the DIP style chips a little longer! (In reality the slight increase probably wouldn't have any affect, and maybe we are only using OLD STOCK now?) I, personally, don't concern myself with that issue/problem. I haven't had any chips fail/wear out yet... (In over 10yrs+ of using the AVR's...) |
Please log in to post a reply.
Did you know that you can make a huge, multi-panel LED display? Learn more...
|