Arduino Forum

Using Arduino => Sensors => Topic started by: gevo12321 on Sep 24, 2012, 06:03 am

Title: Using GY-521 Accelerometer + Gyro
Post by: gevo12321 on Sep 24, 2012, 06:03 am
HI,

I a completely new to Arduino and this is my first time posting on this forum.

I have the Arduino Uno R3 and I bought the GY-521: http://arduino.cc/playground/Main/MPU-6050

So after doing some research, i found a post that showed how to connect gy-521 to the Arduino:
Arduino +5V to VCC
Arduino GND to GND
Arduino analog input pin A4 to SDA
Arduino analog input pin A5 to SCL

I guess this is the standard way to connect to a i2c device?

anyway, I ran the i2c scanner found here: http://arduino.cc/playground/Main/I2cScanner
but for some reason, it doesn't find the device.

I was hoping someone could tell me what I am doing wrong.

Thanks!
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: doughboy on Sep 24, 2012, 07:33 pm
you must disable the internal pullups to A4 and A5 if you are not using a logic level converter.
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: gevo12321 on Sep 25, 2012, 03:06 am
how would I disable the internal pullups? (sorry, very new to this)
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: doughboy on Sep 25, 2012, 04:53 am
http://arduino.cc/en/Reference/DigitalWrite
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: gevo12321 on Sep 25, 2012, 05:13 am
ok so I tried: (I also tried setting the values to HIGH)
  digitalWrite(A4,LOW);
  digitalWrite(A5,LOW);
it didn't work. I am guessing because :

"This sensor board has a voltage regulator. So it can be used with 3.3V and with 5V. It has pull-up resistors of 2k2 on the I2C-bus. The value of 2k2 is rather low. If it is combined with other sensor board which have also pull-up resistors, the total pull-up impedance might be too low."

Anything else I can try?
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: doughboy on Sep 25, 2012, 05:24 am
did you configure both A4 and A5 as input first?
It does not matter what the pullup on your board is. what matters is the cpu internal pullup is to 5v and your sensor board pullup is to 3.3v and you don't want to mix them. A4 and A5 are special pins that are open collector specifically designed so if you disable the internal pull up, only the external pullup wll be used. its all in the atmega328 datasheet if you want to read about it.
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: Krodal on Oct 01, 2012, 09:57 am
You did the right thing : Just 4 connections (+5V to VCC, GND to GND, A4 to SDA, A5 to SCL).
After that the i2c_scanner should see the device.

Do you have another i2c device ?
Perhaps you should try that first, to see if the Arduino is working.
Use the i2c_scanner to test it.

If the i2c_scanner can't find anything, some connection is bad, or your sensor board has a problem.
Test everything very careful, and check the sensor board with a magnifying glass or take a picture with a camera with macro.
Check every solder point. Measure the 3.3V on the sensor board.

Title: Re: Using GY-521 Accelerometer + Gyro
Post by: gevo12321 on Oct 25, 2012, 05:05 am
Thanks for your help guys. Just wanted to leave a record of how I fixed the problem for future reference.

I did not want to change any of the source code in case I needed the same code without the changes in the future so I ended up getting a logic level converter(they are very cheap).

I got the Sparkfun logic level converter and there is one little detail that should be kept in mind when using it. Since i2c lines need 2 way communication, make sure to use the bidirectional Tx lines on the logic level converter and not the Rx lines.

Anyway, in the end I did not change any of the code and my setup was as follows:

Arduino +5V to VCC on GY-521 and to Hv on the Logic Level Converter
Arduino +3.3V to Lv on the Logic Level Converter
Arduino GND to GND on Gy-521 and both GNDs on the Logic Level Converter
Arduino analog input pin A4 to ch1 Tx on Hv side and ch1 Tx on the Lv side to SDA on GY-521
Arduino analog input pin A5 to ch2 Tx on Hv side and ch2 Tx on the Lv side to SCL on GY-521

thanks again for your help
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: jjspierx on Dec 27, 2012, 08:29 pm
I am having problems getting the i2C scanner to recognize my GY-521.  I am trying to connect it without using a logic level converter if possible.

I tried setting the A4 and A5 pins as inputs, setting them low to disable the internal pull-ups using the following code:

pinMode(A4, INPUT);
pinMode(A5, INPUT);
 
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);

However, when I upload this code, the serial monitor doesn't even update, and TX light on the Arduino Uno does not blink.  This happens with either the pinMode or digitalWrite functions commented out as well.  However, with both of them commented out, the I2C scanner works, but does not see any I2C devices

I also tried adding 4.7k resistors as pullup resistors between SDA and SCL and pins A4 and A5.  When I do this the scanner does not find any I2C devices. The GY-521 is on (blue led is lit up) but nothing I do seems to make the I2C scanner find a I2C device.

Any other ideas?

Thank you in advance.


Title: Re: Using GY-521 Accelerometer + Gyro
Post by: jjspierx on Dec 27, 2012, 09:30 pm
I just got the scanner to recognize the GY-521.  I didn't realize the Uno R3 has SDA and SCL pins (next to AREF).  Once I hooked up to the dedicated SDA and SCL pins, the device is now recognized.  Hopefully this helps somebody else using an Uno R3.
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: samu_87 on Jan 25, 2013, 02:02 am
Hi, i have an arduino pro mini, and a breackout board GY-521, the board is perfectly recognized from the arduino and i can read the values.
But i haven't undestand if are needed the pull up resistor on SDA and SCL. I don't want broke the board.

Thanks a lot!
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: Krodal on Jan 25, 2013, 04:09 am
If I look at a photo of the GY-521, I can see two pull-up resistors of 2k2 (the black smd components with the text "222"). So you don't need to add them yourself.
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: nahidalam on Mar 28, 2013, 05:14 am
I am following this same approach below. But I get error "no I2C device found"
Any thoughts?


Thanks for your help guys. Just wanted to leave a record of how I fixed the problem for future reference.

I did not want to change any of the source code in case I needed the same code without the changes in the future so I ended up getting a logic level converter(they are very cheap).

I got the Sparkfun logic level converter and there is one little detail that should be kept in mind when using it. Since i2c lines need 2 way communication, make sure to use the bidirectional Tx lines on the logic level converter and not the Rx lines.

Anyway, in the end I did not change any of the code and my setup was as follows:

Arduino +5V to VCC on GY-521 and to Hv on the Logic Level Converter
Arduino +3.3V to Lv on the Logic Level Converter
Arduino GND to GND on Gy-521 and both GNDs on the Logic Level Converter
Arduino analog input pin A4 to ch1 Tx on Hv side and ch1 Tx on the Lv side to SDA on GY-521
Arduino analog input pin A5 to ch2 Tx on Hv side and ch2 Tx on the Lv side to SCL on GY-521

thanks again for your help
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: nahidalam on Mar 28, 2013, 06:41 pm
ah, got it.
directly connecting UNO R3 SCL, SDA to  to GY-521. Wasn't getting power earlier.
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: guest566 on Jul 10, 2013, 05:49 pm
Krodal,

Thanks for the MPU6050 sketch, I have it up and running on my Arduino. I am looking at the sketch more closely now, and was wondering what all of the definitions at the beginning of the sketch are used for. Do they correspond to inherent properties of the MPU6050 and you are defining them here, or are they there to be used later in code? I saw how the register addresses correspond to the addresses in the MPU6050 register map, but am curious about how the other definitions are used. For example is defining MPU6050_AFS_SEL_2G setting some sensitivity to the device? Because many of the constants aren't used again in the sketch I'm wondering if the definitions have some direct effect on the chip. If you know of other documentation for the MPU6050 other than the product specifications and the register map that may also help.

Thank you!
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: Krodal on Jul 10, 2013, 08:02 pm
I used the datasheet(s) as a framework to set up a basic sketch.
The idea is that if the code has been proven to be okay (it is), someone might use it to extend it and put it in a class.

So all the defines are the registers and the bits in the registers. I named them the same as in the datasheet(s).
You have to keep the datasheet next to it, to know what it is about.

But that is not all, there are also undocumented registers.
If you take a look at the code by Jeff Rowberg, you see he has more registers.
http://www.i2cdevlib.com/
https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/MPU6050.h
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: gebell on Oct 17, 2013, 02:25 am
[EDIT: problem solved.  It *was* a lose connection.  On to the next problem I can't solve . . . thanks to anyone who contemplated my problem.]


I am having this exact same problem, but I'm at a lost as to how to 'fix' it.  

I run the scanner and get no devices found.  I switch the connection to go to the board's dedicated SCL and SDA slots, but it didn't fix it.  

I'm really at a loss, any thoughts, ideas or suggestions would be really appreciated.

I am very much a noobie at all this.  

could it be as simple as a loose connection?

Some pictures of the setup:

Title: Re: Using GY-521 Accelerometer + Gyro
Post by: blaxe on Oct 20, 2013, 05:47 am
hiii gebell,

how u calibrate your sensor?mind to share :)
Title: Gyro 521
Post by: aakyuerek on May 15, 2014, 11:08 am
Ich bin leider neu im Arduino. Ich frage Euch deshalb, weil ich mir gedacht habe, dass Ihr vielleicht etwas wissen würdet, und zwar  habe ich einen Arduino Uno und einen Gyrosensor den GY-521. Ich mache gerade eine Diplomarbeit über einen Fitnesstracker. Die Aufgabe ist es, dass wir den Gyrosensor z.b. auf den Handglenek tragen und das halt bei jeder Bewegung ein Signal rauskommt. Ich habe mir auch die Wire Bibliothek angeschaut, weiß aber trotzdem nicht, wie ich einen Code schreiben soll, damit ich so ein Signal herauskriege. Könntet Ihr mir helfen, indem Ihr vielleicht ein ganz einfachen Code  schicken, wie man das programmiert oder irgendwie sagen könntet, was ich machen muss.
Danke und mit freundlichen Grüßen
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: mrlowalowa on Jul 01, 2014, 11:43 pm
aakyuerek:

German:
du bist hier im falschen unterforum.. bzw musst du hier in die deutsche Sparte um solche Sachen zu fragen. Schätzte jedoch dass es bereits zu spät ist um dir noch zu helfen wenn das überhaupt noch ginge, wenn nicht schreib doch mal im deutschen Forum

english:
I just told him, that he has to move to the German Part of the forum, if he wants an answer on a german question.

greetings

P.S. to the hint that the R3 has it's own IC2 ports... :D I had this problem myself too ......... ^^
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: napoleaninlondon on Jan 06, 2015, 01:44 am
So here is a quick info for GY-521 to work with Arduino Uno R3 for I2C scanner

Arduino     gy-521
+5V==>VCC
GND==>GND
SCL==>SCL
SDA==> SDA
Digital pin 2 ===> INT


Once this setup is completed, write the following code in the I2C setup function

pinMode(A4, INPUT);
pinMode(A5, INPUT);
 
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);

Now you'll definitely see the I2C device being scanned. Else you will have to pull all the hair from your head :)
Title: Re: Using GY-521 Accelerometer + Gyro
Post by: WilliamW on Mar 10, 2015, 04:31 pm
I will add my experience in case it is helpful to someone.  I also had trouble getting my Arduino to recognize the GY-521 at all (and then, when I first got it recognized, I would get the error code = 2).

Like appears to have been the case for a couple others here, my troubles all turned out to be due to poor connections.

Using a breadboard, I set up my pins as other have outlined here, but could not get I2CScanner or anything else to recognize the sensor.  Eventually with much jiggling of wires and pin/connections, I was able to get the Arduino to intermittently recognize the sensor - but I was now getting "error code = 2" and bad readings.  I also noticed that the LED light on the GY-521 was weak and pulsing, which didn't seem right.

Eventually I chucked the breadboard and connected the sensors directly (i.e., no breadboard, just pins right through the holes on the GY-521), and everything works - a nice steady bright LED on the GY-521, no error code, and readings that appear to be accurate.

Not sure if the GY-521's connections are finicky, or if it was just my lack of experience.
 
Hope that is helpful to someone!