I've been staring at this data sheet for my magnetic sensor for a while now, and this is my first time every using the I2C protocol. When attempting to translate the steps shown in "Example Measurement" I'm quite sure I am doing this incorrectly . . .
#include <Wire.h>
#define address 0b110000 // find the actual address, it is at 0x30, which is 0b00110000
void setup() {
Serial.begin(9600);
Wire.begin(address);
}
void loop() {
int x, y, z;
Wire.beginTransmission(address);
Wire.requestFrom(0b00110000, 1); // START condition?, starts with a 0 to indicate a WRITE request
if(Wire.available() > 0) {
Serial.println("1");
Wire.read();
}
Serial.println(Wire.read());
Wire.write(0b00000111);
if(Wire.available() > 0) {
Serial.println("2");
Wire.read();
}
Wire.write(0b00000001);
if(Wire.available() > 0) {
Serial.println("3");
Wire.read();
}
Wire.requestFrom(0b00110000, 1); // write request
if(Wire.available() > 0) {
Serial.println("4");
Wire.read();
}
Wire.write(0b00000110);
if(Wire.available() > 0) {
Serial.println("5");
Wire.read();
}
Wire.requestFrom(0b10110000, 1); // READ request
if(Wire.available() > 0) {
Serial.println("6");
Wire.read();
}
// not sure what to do for the 7th cycle???
// am I even doing the previous correct?
// Wire.requestFrom(0b10110000, 6);
//
// if(Wire.available() >= 6) {
// Serial.println("good");
// x = Wire.read()<<8; //X msb
// x |= Wire.read(); //X lsb
// z = Wire.read()<<8; //Z msb
// z |= Wire.read(); //Z lsb
// y = Wire.read()<<8; //Y msb
// y |= Wire.read(); //Y lsb
// }
//
// //Print out values of each axis
// Serial.print("x: ");
// Serial.print(x);
// Serial.print(" y: ");
// Serial.print(y);
// Serial.print(" z: ");
// Serial.println(z);
// Wire.endTransmission(address);
delay(1000);
}
I believe that the first 6 cycles are incorrect, but I'm not sure of any other way to approach this. I used this I2C scanner to scan for the address of my device, and it was found to be 0x30, which is 0b00110000. Some things I am confused about,
- What does it mean to scan the SCL line, and how would this be done with the Wire library?
- I'm never actually getting acknowledgements back from the device, i.e: after each requestFrom()/write(), I'm never entering the if statement afterwards.
Any help would be greatly appreciated!!!