1

Below are the X,Y,Z registers for HMC5883L. HMC5883L registers

while this is the Arduino code for HMC5883L to acquire X,Y,Z reading. If you look at this code, we do not mention the X, Y, Z address specifically.

  Wire.beginTransmission(0x1E);
  Wire.write(0x02);
  Wire.write(0b00000001);
  Wire.endTransmission();
  delay(10);

  Wire.requestFrom(0x1E,6);
  if (Wire.available() > 5)
  {
    x = readValue();
    y = readValue();
    z = readValue();
  }
  else 
  {
    Serial.println("No data found");
  }
int readValue()
{
  int val = Wire.read()<<8; 
      val |= Wire.read();

  return val;
}

We only write Wire.request() and do not mention X,Y,Z addresses specifically for HMC5883L. While in the case of ADXL345 we mention every register of X,Y,Z separately to acquire data. Which is exactly my question like why not simply use Wire.requestFrom() for ADXL345 too. When will we access each X,Y,Z register separately? and when will we do not?

Below is Arduino ADXL345 code for reading X axis data.

  Wire.beginTransmission(0x53); 
  //Ask the particular registers for data
  Wire.write(0x32);
  Wire.write(0x33);  
  Wire.endTransmission(); 
  Wire.requestFrom(0x53,2);
  if(Wire.available()<=2) {  
    X0 = Wire.read(); 
    X1 = Wire.read(); 
    X1=X1<<8;
    X_out =X0+X1;
    Xa=X_out/256.0; 
  }

Datasheets for both sensors are available on the link.

The code above is not mine. https://drive.google.com/open?id=1AswNTdBz55Rfg3nu4-YsLFj4ANrsCL5I

  • use the datasheet to determine the correct way to retrieve data from the device – jsotola Apr 14 '20 at 16:44
1

If you read the datasheet for the mentioned magnetometer, it is clearly mentioned that the data is to be read at once. Below is the implementation of pseudo code mentioned in the datasheet.

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  //send 0x3C 0x00 0x70 (8-average, 15 Hz default, normal measurement)
  Wire.begin();
  Wire.beginTransmission(0x1E);
  Wire.write(0x00);
  Wire.write(0x70);
  Wire.endTransmission();
  delay(10);
  //send 0x3C 0x01 0xA0 (Gain=5, or any other desired gain)
  Wire.beginTransmission(0x1E);
  Wire.write(0x01);
  Wire.write(0xA0);
  Wire.endTransmission();
  delay(10);
  //send 0x3C 0x02 0x00 (Continuous-measurement mode)
  Wire.beginTransmission(0x1E);
  Wire.write(0x02);
  Wire.write(0x00);
  Wire.endTransmission();
  delay(10);

}
void loop() 
{


  Wire.requestFrom(0x1E,6);
  if (Wire.available() > 5)
  {
    x = readValue();
    y = readValue();
    z = readValue();
  }
  else 
  {
    Serial.println("No data found");
  }
  data = String(x) + "   ,   " + String(y) + "   ,   " + String(z);
  //Send 0x3C 0x03 (point to first data register 03)
  Serial.println(data);
  Wire.beginTransmission(0x1E);
  Wire.write(0x03);
  Wire.endTransmission();
  delay(100);
}

Similarly, it is mentioned in the datasheet for ADXL345 as well that the data should be read separately from each axis register separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.