This is a Sketch to Support the LIS302DL (least expensive 3Axis Accellerator at Sparkfun)
Tested with 5.6K pullups on data lines
SCL to 5 (5.6K pull-up)
MIS1 to 4 (5.6K pull-up)
MIS0 to Vcc
Vcc = 5vdc
Please enjoy and improve.
Wish List:
Better signal detect
Read Data only if Present (status bits)
#include <Wire.h>
#define i2cID 0x1D
//TWI (I2C) sketch to communicate with the LIS302DL accelerometer - Modified and tested Ben Gatti - 6/9/2008
//http://www.st.com/stonline/products/literature/ds/12726.pdf
//Note 5.6K pullup resister on data lines.
//Device is 3 volt was tested at 5 volts no level shifting.
//Reference claims 6 volts is pin max.
//Modified from // TWI (I2C) sketch to communicate with the LIS3LV02DQ accelerometer
//http://www.nearfuturelaboratory.com/2007/01/11/arduino-and-twi/
//Modified code from http://research.techkwondo.com/blog/julian/279
//Thanks Julian.
// Using the Wire library (created by Nicholas Zambetti)
// http://wiring.org.co/reference/libraries/Wire/index.html
// On the Arduino board, Analog In 4 is SDA, Analog In 5 is SCL
// These correspond to pin 27 (PC4/ADC4/SDA) and pin 28 (PC5/ADC5/SCL) on the Atmega8
// The Wire class handles the TWI transactions, abstracting the nitty-gritty to make
// prototyping easy.
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(57600);
Serial.println("Wire.begin");
Wire.beginTransmission(i2cID);
Wire.send(0x21); // CTRL_REG2 (21h)
Wire.send(B01000000);
//SPI 4/3 wire
//1=ReBoot - reset chip defaults
//n/a
//filter off/on
//filter for freefall 2
//filter for freefall 1
//filter freq MSB
//filter freq LSB - Hipass filter (at 400hz) 00=8hz, 01=4hz, 10=2hz, 11=1hz (lower by 4x if sample rate is 100hz)
Wire.endTransmission();
Wire.beginTransmission(i2cID);
Wire.send(0x20); // CTRL_REG1 (20h)
Wire.send(B01000111);
//sample rate 100/400hz
//power off/on
//2g/8g
//self test
//self test
//z enable
//y enable
//x enable
Wire.endTransmission();
}
void loop()
{
#define outXhigh 0x29
#define outYhigh 0x2B
#define outZhigh 0x2D
#define statusReg 0x27
boolean goodRead;
goodRead = false;
//----------Status Register-----------------------
byte status;
status = B00000000;
//
//-------------------------------
Wire.beginTransmission(i2cID);
Wire.send(statusReg);
Wire.endTransmission();
Wire.requestFrom(i2cID, 1);
if(Wire.available())
{
status = Wire.receive();
}
//----------X Values-----------------------
byte x_val;
//-------------------------------
Wire.beginTransmission(i2cID);
Wire.send(outXhigh);
Wire.endTransmission();
Wire.requestFrom(i2cID, 1);
if(Wire.available())
{
x_val = Wire.receive();
}
//----------Y Values-----------------------
byte y_val;
//-------------------------------
Wire.beginTransmission(i2cID);
Wire.send(outYhigh);
Wire.endTransmission();
Wire.requestFrom(i2cID, 1);
if(Wire.available())
{
y_val = Wire.receive();
}
//----------Z Values-----------------------
byte z_val;
//-------------------------------
Wire.beginTransmission(i2cID);
Wire.send(outZhigh);
Wire.endTransmission();
Wire.requestFrom(i2cID, 1);
if(Wire.available())
{
z_val = Wire.receive();
goodRead=true;
}
//-------------------------------
if (goodRead==true)
{
//Serial.print("x_val_l= "); Serial.println(x_val_l, DEC);
//Serial.print("x_val_h= "); Serial.println(x_val_h, DEC);
//Serial.print("x_val= "); Serial.println(x_val, DEC);
//Serial.print("y_val_l= "); Serial.println(y_val_l, DEC);
//Serial.print("y_val_h= "); Serial.println(y_val_h, DEC);
//Serial.print("y_val= "); Serial.println(y_val, DEC);
//Serial.print("z_val_l= "); Serial.println(z_val_l, DEC);
//Serial.print("z_val_h= "); Serial.println(z_val_h, DEC);
Serial.print("status "); Serial.println(status, BIN);
Serial.print(" val= "); Serial.print(x_val, DEC);
Serial.print(":"); Serial.print(y_val, DEC);
Serial.print(":"); Serial.println(z_val, DEC);
}
else
Serial.print("no data");
delay(100);
}