Go Down

Topic: GY-80 will not acknowledge, but appears in I2C scanner.  (Read 211 times) previous topic - next topic

arduinowerks

I cannot get my GY-80 to acknowledge my Arduino mega 2650 on I2C when I manually type in the device address or manually store the address in a variable and use that with the Wire.beginTransmission() command  However, the sensors return an acknowledge on the I2C scanner.  I am specifically trying to access the L3G4200D sensor, which has an address of 0x69 in hex and 105 in decimal.  I think it is the format I am entering the address in.  To test my theory I set up a program where I stored each of the found addresses from running an I2C scanner in a variable and used each of those variables to communicate with the sensor.   Here are the results of my test.  However, I am unable to enter the addresses myself and must rely on the I2C scanner to get the L3G4200D to acknowledge.  I have read the datasheet and googled this problem.  Please help.  Here is my test code.  Can someone tell me how to manually address the sensor.

Code: [Select]


[color=blue]//This will find the address for me store it in a variable.   
//But I want to be able to manually address the sensor
//
//Example 1 does not work:
//
//Wire.beginTransmission(105);
//error = Wire.endTransmission();
//
//Example 2 does not work:
//
//Wire.beginTransmission(0x69);
//error = Wire.endTransmission();
//
//The sensor will not acknowledge w[/color]ith the above examples, but
//the code below works beautifully
//[/color]
#include <Wire.h>

 byte error, address, barometer, accelerometer, gyro, magnetometer;
 int nDevices;
 int counter = 0;
 byte transmissionStatus;
 
void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");

  //////////////////////////// Discovers and stores addresses///////////////////
 

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
     
        if(counter == 0){

          magnetometer = address;// stores the address of the sensor in its own variable
         
        }else if(counter == 1){

          accelerometer = address;// stores the address of the sensor in its own variable
 
        }else if(counter==2){

          gyro = address;// stores the address of the sensor in its own variable
         
        }else if(counter == 3){

          barometer = address;// stores the address of the sensor in its own variable
         
        }
     
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
       Serial.println(address,HEX);
      Serial.print(address,HEX);
      Serial.print(" Decimal: ");
      Serial.print(address,DEC);
      Serial.print(" Binary: ");
      Serial.print(address,BIN);
      Serial.print("Transmition Error Code: ");
      Serial.print(error);
      if(error == 0){
        Serial.println(" success");
      }else if(error == 2){
        Serial.println(" received NACK on transmit of address");
      }else if(error == 3){
        Serial.println(" 3:received NACK on transmit of data");
      }else if(error == 4){
        Serial.println(" 4:other error");
      }
      Serial.println("  !");

      nDevices++;
      counter++;
    }
    else if (error==4)
    {
      Serial.print("Unknow error at address 0x");
      if (address<16)
        Serial.print("0");
     
    }   
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

   Serial.println("Results:");
   
   Serial.print("Barometer: ");
   Serial.println(barometer);

   Serial.print("Magnetometer: ");
   Serial.println(magnetometer);

   Serial.print("Accelerometer: ");
   Serial.println(accelerometer);

   Serial.print("Gyro: ");
   Serial.println(gyro);

  delay(5000);
 
 
}


void loop()
{
  Wire.beginTransmission(gyro);
  Wire.endTransmission();
  Serial.println("Transmission Status:");
  Serial.println(transmissionStatus);
  if(transmissionStatus == 0){

    Serial.println("Yeah, Sucess, slave device has acknoledged");
  }
  delay(3000);
}

Koepel

Most of us use the hexadecimal notation for the I2C address. That will be 0x77, 0x1E, and so on.

You could print it with this:
Code: [Select]

  Serial.print("Barometer: 0x");
  Serial.println(barometer, HEX);


The return value of Wire.endTransmission() can be used to check the acknowledge. However, in your loop(), you don't use the return value.

Fix:
Code: [Select]

transmissionStatus = Wire.endTransmission();      // put the return value in 'transmissionStatus'

arduinowerks

I apologize. That is not quite the answer I was looking for.  I probably did not phrase the question properly.  I can print the transmission status and or address just fine.  The problem is I keep getting transmission status "two" when I enter addresses manually.  Transmission status  two is "not acknowledged", a failure.  Transmission status "zero" is successful communication. The sensor shows transmission status is zero when I run the I2C scanner.  But not when I run the following code:

Wire.beginTransmission(0x69);

 transmissionStatus = Wire.endTransmission();

 Serial.println("Transmission Status:");
 Serial.println(transmissionStatus);

Here is the result of this code.

Transmission Status:
2




Koepel

I noticed a bug in your sketch, and now you give some code that is not a working sketch :smiley-eek:

Could you show the output of the i2c_scanner ? So I can see for myself that address 0x69 does exist.
And could you make a real working sketch of the code in your Reply #2 ?

The i2c_scanner does nothing else than the beginTransmission and endTransmission. If you do the same, I'm sure the returned value is zero as well  :smiley-cool: Guaranteed, 100%.

When you say that you can print the transmission status and address just fine, that is not enough for me  ::)  Show a sketch that is correct. Did you see the "Fix:" in my Reply #1 ?

Do you apply 5V from the Arduino Mega to the VCC_IN of the module ? and the VCC_3.3V of the module is not connected ? If you don't use 5V to VCC_IN, then the level shifters on the module don't work. Could you make a photo of it ? so I can see the wiring.

batman_d408

There is no problem even if you enter the address manually and it transmissionStatus should come out to be zero only. Just run the code for I2C scanner separately and mention in the correct address.

You use Wire.write() function after Wire.beginTransmission(0x69) to write a value or register on GY-80 and then check the status for transmission as you did previously.

Probably, then the returned value will be zero if all the connections and wiring is correct as mentioned in the previous post.

arduinowerks

Thanks everyone for helping me with this.  I got the program working.  I figured what the problem was.  The faulty line of code is now commented out. Here is the specific line of code that was causing the problem:  //#define GYRO_ADDRESS 0x69

What is the proper way to use a #define statement.  The full working program is listed below.  Thanks in advance.






#include <Wire.h>

//#define GYRO_ADDRESS 0x69

byte error, address;


void setup()
{

 
 
 Wire.begin();
 Serial.begin(9600);
 while (!Serial);             // Leonardo: wait for serial monitor
 Serial.println("Starting");
}


void loop()
{

   Wire.beginTransmission(0x69);
   error = Wire.endTransmission();

   if (error == 0)
   {
     Serial.println("Device found");
     
   }else{

       Serial.println("Device not found, error code:");
       Serial.println(error);
   }

   Serial.println("done\n");

 delay(5000);           // wait 5 seconds for next scan
}

Koepel

I'm glad you have it working :)
But I don't understand it. The define is okay. It is a text-replacement by the preprocessor, before the compiler actually starts to compile.

Code: [Select]

#define GYRO_ADDRESS 0x69

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy