Hi everyone. I’m really not good with this stuff but I need to make this work for a project. I tried something but I have little knowledge of programming… Can anyone help me with the code? I only need read the axis. Thank you.
#include <Wire.h> //I2C Arduino Library
#define address 0x18 //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; //triple axis data
Wire.beginTransmission(address);
Wire.write(0x28);
Wire.endTransmission();
//Read data from each axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8;
x |= Wire.read();
z = Wire.read()<<8;
z |= Wire.read();
y = Wire.read()<<8;
y |= Wire.read();
}
//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(250);
}
Couple of comments on the code, based on what I can see in the datasheet.
Wire.write(0x28);
Since you go on to read multiple bytes starting from this register address (for OUT_X_L), I think you need to set bit 7 in the address. So the value should be 0xA8.
The OUT_X_H register comes after OUT_X_L. High byte is below low byte.
So this code …
x = Wire.read()<<8;
x |= Wire.read();
needs to become …
x = Wire.read();
x |= Wire.read() <<8;
Lastly, the registers are in the order X, Y then Z. Your code is reading x, z then y. Need to swap the order.
Sorry, I forgot to write the CS, it is connected to the ground.
link to the breakout board
#include <Wire.h> //I2C Arduino Library
#define address 0x18 //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; //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 = Wire.read();
x |= Wire.read() <<8;
y |= Wire.read();
y = Wire.read()<<8;
z |= Wire.read();
z = Wire.read()<<8;
}
//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(450);
}
Try these changes to your assignment statements. What output do you get?
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;
Do the values change if you move the board around?
In particular, what readings do you get if you turn the board upside down?
And to confirm if the I2C is working OK, try this code which should just read and print the ID register on the device. It should give a result of 0x33.