martedì 15 maggio 2012

Accelerometro LIS3LV02DQ su Arduino

La prova reale dell'accelerometro LIS3LVV02DQ collegato come I2C ad una Arduino
Le resistenze di Pull-Off sono da 1.5 KOhm


Lo sketch caricato e' il seguente (ripreso da qui)
-----------------
#include <Wire.h>


// TWI (I2C) sketch to communicate with the LIS3LV02DQ accelerometer


//Modified code from http://research.techkwondo.com/blog/julian/279
//Thanks Julian.
 
void setup()

{

  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);

  Serial.println("Wire.begin");
  Wire.beginTransmission(0x1D);  Wire.send(0x20); // CTRL_REG1 (20h)
  Wire.send(0x87); // Device on, 40hz, normal mode, all axis's enabled
  Wire.endTransmission();
}

void loop()
{
#define i2cID 0x1D#define outXhigh 0x29
#define outYhigh 0x2B
#define outZhigh 0x2D
#define outXlow 0x28
#define outYlow 0x2A
#define outZlow 0x2C
byte z_val_l, z_val_h, y_val_l, y_val_h, x_val_l, x_val_h; 
//----------X Values-----------------------
  int x_val;
//-------------------------------   Wire.beginTransmission(i2cID);
  Wire.send(outXlow);
  Wire.endTransmission(); 
Wire.requestFrom(i2cID, 1); while(Wire.available())
 {
   x_val_l = Wire.receive();
 }
 //------------------------------- 
  Wire.beginTransmission(i2cID);
  Wire.send(outXhigh);
  Wire.endTransmission();
Wire.requestFrom(i2cID, 1);if(Wire.available())
 {
   x_val_h = Wire.receive();
 }
//------------------------------- 
 x_val = x_val_h; x_val <<= 8;
 x_val += x_val_l;




//----------Y Values-----------------------
  int y_val;
//-------------------------------   Wire.beginTransmission(i2cID);
  Wire.send(outYlow);
  Wire.endTransmission(); 
Wire.requestFrom(i2cID, 1); while(Wire.available())
 {
   y_val_l = Wire.receive();
 }
 //------------------------------- 
  Wire.beginTransmission(i2cID);
  Wire.send(outYhigh);
  Wire.endTransmission();
   
Wire.requestFrom(i2cID, 1);
if(Wire.available())
 {
   y_val_h = Wire.receive();
 }
//------------------------------- 
 y_val = y_val_h; y_val <<= 8;
 y_val += y_val_l;




 //----------Z Values-----------------------
  int z_val;
//-------------------------------   Wire.beginTransmission(i2cID);
  Wire.send(outZlow);
  Wire.endTransmission(); 
Wire.requestFrom(i2cID, 1); while(Wire.available())
 {
   z_val_l = Wire.receive();
 }
 //------------------------------- 
  Wire.beginTransmission(i2cID);
  Wire.send(outZhigh);
  Wire.endTransmission();
   
Wire.requestFrom(i2cID, 1);
if(Wire.available())
 {
   z_val_h = Wire.receive();
 }
//------------------------------- 
 z_val = z_val_h; z_val <<= 8;
 z_val += z_val_l;

Serial.print("x_val= "); Serial.println(x_val, DEC);

Serial.print("y_val= "); Serial.println(y_val, DEC);
Serial.print("z_val= "); Serial.println(z_val, DEC);

delay(250);
}
-----------------

I risultati a video (ogni tanto sollevo e faccio ricadere la board sul tavolo per vedere l'effetto)