[Solved] IMU GY-85 w/ Arduino Leonardo cannot work

Hi, I'm truly a newbie w/ IMU. What I want to try first is at least just make sure the accelerometer can be connected to Arduino using I2C. I'm using arduino Leonardo anyway.

I think I've already made it correct about the hardware, that just connect these ones:
IMU -> Arduino
3.3V -> 3.3V
A4 -> SDA
A5 -> SCL
GND -> GND

And this one is my code (just ignore the gyro and magnetometer ones):

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <math.h>
#include <Wire.h>

#define ITG3200_Address 0x69  // 68 
#define ADXL345_Address 0x53 //
#define HMC5883L_Address 0x1E

float GyroX,GyroY,AccX,AccY,AccZ;
float roll_acc,pitch_acc;
float *pitch,*roll,*yaw;
int MagX,MagY,MagZ;
float x_caliba,y_caliba;
float u1;

void setup()
{ 
  // initial baud rate of UART as 19200	
  Serial.begin(19200);
  // initial using i2c by Arduino command
  Wire.begin();
  pinMode(13, OUTPUT);
  // initial accelerometer
  Intial(ADXL345_Address,0x2D,0x08); // set PWR
  //Intial(ADXL345_Address,0x31,0x09); // set scale +/- 4g
  Intial(ADXL345_Address,0x31,0x09);
  
  delay(12);
  Calibrating_Acc();
  delay(5);
  // initial gyroscope
  Intial(ITG3200_Address,0x3E,0x82);
  Intial(ITG3200_Address,0x15,0x09);
  Intial(ITG3200_Address,0x16,0x1B);
  Intial(ITG3200_Address,0x17,0x01);
  delay(10);
  Calibrating_Gyro();
  delay(10); 
  // initial Digital compass
  Intial(HMC5883L_Address,0x02,0x00);
  delay(10);
  // initial PWM and interrupt timer
}

void loop()
{
  // Read sensor
  AccelerometerRead();
  GyroRead();
  MagRead();
  
  // Calculate
  pitch_acc = atan2(sqrt(pow((AccY*1/128),2)+pow((AccZ*1/128),2)),(AccX*1/128))*RAD_TO_DEG - 90;
  roll_acc =  atan2(sqrt(pow((AccX*1/128),2)+pow((AccZ*1/128),2)),(AccY*1/128))*RAD_TO_DEG*-1 + 90;

  delay(10); 
  Serial.println(AccX);
  Serial.print("\t");
  Serial.print(AccY);
  Serial.print("\t");
  Serial.println(AccZ);
  delay(10);
}

void Intial(int16_t address, int16_t regiter, int16_t value)
{
	  Wire.beginTransmission(address);
	  Wire.write(regiter);
	  Wire.write(value);
	  Wire.endTransmission();
	  delay(10);
}

void Calibrating_Acc()
{
	float AccX_caliba = 0;
	float AccY_caliba = 0;
	float AccZ_caliba = 0;
	
	float output_x = 0;
	float output_y = 0;
	float output_z = 0;
	
	for(int i=0; i<100;i++)
	{
		AccelerometerRead();
		output_x += AccX;
		output_y += AccY;
		output_z += AccZ;
	}
    output_x /= 100;
	output_y /= 100;
	output_z /= 100;
	
	AccX_caliba = -(output_x)/4;
	AccY_caliba = -(output_y)/4;
	AccZ_caliba = -((output_z-256))/4;
	
	Intial(ADXL345_Address,0x1E,AccX_caliba);
	Intial(ADXL345_Address,0x1F,AccY_caliba);
	Intial(ADXL345_Address,0x20,AccZ_caliba);
}
void Calibrating_Gyro()
{   
	x_caliba = 0;
	y_caliba = 0;
	//z_caliba = 0;
	
	for (int i=0; i<100; i++)
	{
		GyroRead();
		x_caliba += GyroX;
		y_caliba += GyroY;
		//z_caliba += GyroZ;
	}
	x_caliba /= 100;
	y_caliba /= 100;
	//z_caliba /= 100;
}
	  
void AccelerometerRead()
{
	Wire.beginTransmission(ADXL345_Address); // address of the accelerometer
	Wire.write(0x32); // set read pointer to data
	Wire.endTransmission();
	Wire.requestFrom(ADXL345_Address, 6);
	
	// read in the 3 axis data, each one is 16 bits
	int i = 0;
	byte result[6];
	while(Wire.available())
	{
		result[i] = Wire.read();
		i++;
	}
	Wire.endTransmission();
	AccX = ((result[1] << 8) | result[0]);
	AccY = ((result[3] << 8) | result[2]);
	AccZ = ((result[5] << 8) | result[4]);
}

void GyroRead()
{
	Wire.beginTransmission(ITG3200_Address); // address of the gyro
	Wire.write(0x1B); // set read pointer
	Wire.endTransmission();
	Wire.requestFrom(ITG3200_Address, 8);
	int i = 0;
	byte buff[8];
	while(Wire.available())
	{
		buff[i] = Wire.read();
		i++;
	}
	Wire.endTransmission();
	GyroX = ((buff[2] << 8) | buff[3]);
	GyroY = ((buff[4] << 8) | buff[5]);
	   //GyroZ = ((buff[6] << 8) | buff[7]);
	   //GyroTemp = (buff[0] << 8) | buff[1]; // temperature
}

void MagRead()
{
	//Tell the HMC5883 where to begin reading data
	Wire.beginTransmission(HMC5883L_Address);
	Wire.write(0x03); //select register 3, X MSB register
	Wire.endTransmission();
	//Read data from each axis, 2 registers per axis
	Wire.requestFrom(HMC5883L_Address, 6);
	
	if(6<=Wire.available())
	{
		MagX = Wire.read()<<8; //X msb
		MagX |= Wire.read(); //X lsb
		MagZ = Wire.read()<<8; //Z msb
		MagZ |= Wire.read(); //Z lsb
		MagY = Wire.read()<<8; //Y msb
		MagY |= Wire.read(); //Y lsb
    }
}

But the result in serial window is just like this:

3843.00 3587.00 3331.0
3843.00 3587.00 3331.0
3843.00 3587.00 3331.0
3843.00 3587.00 3331.0

(I cannot attach picture, something error).


I also try this code anyway

#include "Wire.h"

#define DEVICE (0x53)
byte byte1 = 0;


void setup(){
  Wire.begin();
  #ifndef cbi
  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  #endif
  cbi(PORTC,5);
  cbi(PORTC,4);
  Serial.begin(115200);
  
}

void loop(){
  Wire.beginTransmission(DEVICE);
  Wire.send(0x00);
  Wire.endTransmission();
  Wire.requestFrom(DEVICE,1);
  byte1 = Wire.receive();
  Serial.println(byte1,HEX);
  delay(2000);
}

It says to detech the REGISTERID every 2 seconds. But what I got is FF FF FF FF....

Is there anyone know that means? Or can help me with this? I have tried many open source code from internet, but I cannot make even accelero or gyro working. It always give zero results 0 0 0. I truly give up with this. Or maybe is there any chance that my IMU is broken? I really just bought it some days ago though..

Truly big thanks

Arduino & IMU - Newbie

dude have you tried to use pull up resistor from SDA and SCL to vcc ?

electronicsfreak:
dude have you tried to use pull up resistor from SDA and SCL to vcc ?

Yes, I've already used pull up 10K & 5K to both SDA & SCL to 3.3v. But it still has the same result, zero reading from accelero. Dunno what's wrong.

hmm seems there is something wrong with your imu or your i2c procedure. why don't you try in another microprosessor or platform like atmega or arm

AHHH Ive found it. I am sorry I am truly newbie with this.

When I check this: Wire - Arduino Reference

It using pin 2 & 3 to connect I2C, not A4 & A5

:grin:

afifizzatullah
Which set of code above did you finally get working?