ITG3200 ADXL345 Sensor Demo Test of Posture Recognition

Posted by: Chao Category: Network Tags: , , , Comments: 6

ITG3200 ADXL345 Sensor Demo Test of Posture Recognition

Posture recognition are widely used in many field, such us self balanced car, helicopter, two-feed robot, etc. Now we use arduino + ITG3205+ADXL345 to fulfill the posture recognition, and using processing as output to display posture in real time.

ITG3205 and ADXL345 module are the final built version, you can use directly with arduino, no need to design and built your own PCB. You can buy it from our site.

First, let’s wiring the analog 5 to SCL pin of  the I2C Jack, and analog 4 of arduino to SDA of I2C jack. Since both module use I2C jack, so both has the same wiring (parallel connection), and just let arduino choose by I2C codes when they are using. Don’t forget connect VCC and GND, VCC only use 3.3V, not 5V.

Wiring:

Demo Code

#include   // Get I2C library
 
//  ADXL345
#define ACC (0x53)    //define ADXL345 address
#define A_TO_READ (6)        //read bytes (2) every time
 
 
// gryo ITG3200 
#define GYRO 0x68 // configue address, connect AD0 to GND, address in Binary is 11101000 (this please refer to your schematic of sensor)
#define G_SMPLRT_DIV 0x15
#define G_DLPF_FS 0x16
#define G_INT_CFG 0x17
#define G_PWR_MGM 0x3E
 
#define G_TO_READ 8 // x,y,z every axis 2 bytes
 
// for the offest
int g_offx = 67;
int g_offy = 5;
int g_offz = 41;
 
// for the offest
int a_offx = -30;
int a_offy = -8;
int a_offz = 0;
 
char str[512]; 
 
void initAcc() {
  //get ADXL345
  writeTo(ACC, 0x2D, 0);      
  writeTo(ACC, 0x2D, 16);
  writeTo(ACC, 0x2D, 8);
  // set the default value at +-2g
}
 
void getAccelerometerData(int * result) {
  int regAddress = 0x32;    //The setting of data ofthe first axis of ADXL345
  byte buff[A_TO_READ];
 
  readFrom(ACC, regAddress, A_TO_READ, buff); //read the data from adxl345
 
  //the value of every axis has 10 resolution, which means 2 bytes
  //we have to convert 2 bytes into 1 int value.
  result[0] = (((int)buff[1]) << 8) | buff[0] + a_offx;   
  result[1] = (((int)buff[3]) << 8) | buff[2] + a_offy;
  result[2] = (((int)buff[5]) << 8) | buff[4] + a_offz;
}
 
//initilize gyro
void initGyro()
{
  /*****************************************
   * ITG 3200
   * power managerment setting
   * clock setting = internal clock
   * no reset mode, no sleep mode
   * no standby mode
   * resolution = 125Hz
   * parameter is + / - 2000 degreee/second
   * low pass filter=5HZ
   * no interruption
   ******************************************/
  writeTo(GYRO, G_PWR_MGM, 0x00);
  writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF
  writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19
  writeTo(GYRO, G_INT_CFG, 0x00);
}
 
 
void getGyroscopeData(int * result)
{
  /**************************************
   * gyro ITG- 3200 I2C
   * registers:
   * temp MSB = 1B, temp LSB = 1C
   * x axis MSB = 1D, x axis LSB = 1E
   * y axis MSB = 1F, y axis LSB = 20
   * z axis MSB = 21, z axis LSB = 22
   *************************************/
 
  int regAddress = 0x1B;
  int temp, x, y, z;
  byte buff[G_TO_READ];
 
  readFrom(GYRO, regAddress, G_TO_READ, buff); //read gyro itg3200 data
 
  result[0] = ((buff[2] << 8) | buff[3]) + g_offx;
  result[1] = ((buff[4] << 8) | buff[5]) + g_offy;
  result[2] = ((buff[6] << 8) | buff[7]) + g_offz;
  result[3] = (buff[0] << 8) | buff[1]; // temperature
 
}
 
 
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  initAcc();
  initGyro();
}
 
 
void loop()
{
  int acc[3];
  int gyro[4];
  getAccelerometerData(acc);
  getGyroscopeData(gyro);
 
  sprintf(str, "%d,%d,%d,%d,%d,%d,%d", acc[0], acc[1], acc[2], gyro[0], gyro[1], gyro[2], gyro[3]);  
  Serial.print(str);
  Serial.print(10, BYTE);
 
  //delay 50  millisecond
}
 
 
//---------------- function
//write val into the address register of accelerometer
void writeTo(int DEVICE, byte address, byte val) {
  Wire.beginTransmission(DEVICE); //send to sensor
  Wire.send(address);        // send register address 
  Wire.send(val);        // send the value which needed to write
  Wire.endTransmission(); //end transmission 
}
 
 
//read data from the buffer array of address registers in the accelerometer sensor
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  Wire.beginTransmission(DEVICE); //start to send to accelerometer sensor
  Wire.send(address);        //send address which are read
  Wire.endTransmission(); //end transmission
 
  Wire.beginTransmission(DEVICE); //start to send to ACC
  Wire.requestFrom(DEVICE, num);    // require sending 6 bytes data from accelerometer sensor
 
  int i = 0;
  while(Wire.available())    //Error when the return value is smaller than required value
  { 
    buff[i] = Wire.receive(); // receive data
    i++;
  }
  Wire.endTransmission(); //end transmission
}

Click the run button of processing.
See the video how it works during break.

Share this post

Comments (6)

  • Hamid Reply

    Hi,

    I have a question regarding the offset for for both sensors, how did you calculate them?

    Thanks

    July 18, 2012 at 10:35 pm
  • Chao Reply

    We just simply leave it on a flat ground, and see the offset value, then use this one..

    July 18, 2012 at 10:40 pm
  • Manuel Ricardo Alfonso Reply

    Hi All. I am developing a IMU with those sensors, but I still have a drift on both sensors, If have leave this over the table for a few minutes without movements, drift appears. Have you seen this drift on your tests?

    August 28, 2012 at 2:08 pm
  • Pedro Calçada Reply

    Hello, I am having difficulty in calculating the gyro when moving more than one axis at the same time, an error in the angle does not return to zero. Anyone know how to help me? thank you

    October 27, 2012 at 1:14 am
  • kremen Reply

    Hi Chao, where is the processing code for this demo.??
    Thanks

    December 1, 2013 at 10:40 am
  • poulbran Reply

    That is some nice code. I think that this would be cool on a self-balancing robot.

    September 12, 2016 at 8:18 am

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.