I'm using the Arduino Uno to obtain acceleration data from an LIS3DH accelerometer, the code to do this is posted below:
#include <Wire.h> //I2C Arduino Library
#define address 0x19 //I2C 7bit address
void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put into the correct operating mode
Wire.beginTransmission(address); //open communication with
Wire.write(0x20);
Wire.write(0x27);
Wire.endTransmission();
}
void loop(){
int x,y,z,a,b,c; //triple axis data
Wire.beginTransmission(address);
Wire.write(0xA8);
Wire.endTransmission();
//Read data from each axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = (int)Wire.read();
x |= (int)Wire.read()<<8;
y = (int)Wire.read(); // Note change in operator on this and next three statements
y |= (int)Wire.read()<<8;
z = (int)Wire.read();
z |= (int)Wire.read()<<8;
}
//Print out values of each axis
Serial.print(" ");
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(z);
delay(450);
}
I have the accelerometer soldered to an evaluation board, and the SCL and SDA pins on the evaluation board are connected to the SDA and SCL pins on the Arduino Uno. If you look at the image below, I'm using the I2C SCL and I2C SDA pins at the top left (next to the reset button) NOT the analog SCL and SDA.
I assume the data for the x, y, and z axis (tri-axial accelerometer) comes from the SDA line, but I don't know what pin number to call when trying to interface Matlab and the Arduino. Does anyone know what pin number I should be calling to get the data?
first Question: Are you using a level converter? I just read the Datasheet for the LIS3DH Data Sheet, it a 3.3v device, does the eval board have a 3.3v powersupply, does the eval board have level shifters for the SCL and SDA lines. The Uno is expecting 5V, and the Datasheet says 3.6v is max with destruction at 4.8v
First Problem, from the datasheet page 21
The I2C embedded inside the LIS3DH behaves like a slave device and the following protocol must be adhered to. After the start condition (ST) a slave address is sent, once a slave acknowledge (SAK) has been returned, a 8-bit sub-address (SUB) is transmitted: the 7 LSb represent the actual register address while the MSB enables address auto increment. If the MSb of the SUB field is ‘1’, the SUB (register address) is automatically increased to allow multiple data read/write. The slave address is completed with a Read/Write bit. If the bit was ‘1’ (Read), a repeated START (SR) condition must be issued after the two sub-address bytes; if the bit is ‘0’ (Write) the Master transmit to the slave with direction unchanged. Table 12 explains how the SAD+Read/Write bit pattern is composed, listing all the possible configurations
The important parts I highlighted in Bold. You missed the Second one.
Change your code to do a Repeated-Start instead of a Stop after the register selection in your read statement.
Wire.beginTransmission(address);
Wire.write(0xA8);
Wire.endTransmission(false); // the false cause Wire to issue a repeated-start instead of a STOP
// when the STOP is received by the LIS3DH it 'looses' then register selection and just return zeros
Chuck.
Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.