LSM303 and i2c performance issues

Hi, I'm using the arduino uno v3 with the LSM303DLMTR and HMC5883L. With both I've got performance issues. When I readout the LSM303 it's taking 1900 microseconds to do so. With the HMC it's taking 1300 microseconds.

The rest of my code (not very small), is only taking up 80 microseconds. The high latency is causing problems for serial port communication.

Is this normal?

Thanks

Hello Maarten123

Please post your code. If it is too long to fit between code tags, you can attach it as a file.

Regards

Ray

here is my code:

#include <Wire.h>
#include <LSM303.h>

LSM303 compass;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  compass.init();
  compass.enableDefault();
  
  compass.m_min = (LSM303::vector<int16_t>){-32767, -32767, -32767};
  compass.m_max = (LSM303::vector<int16_t>){+32767, +32767, +32767};
}

void loop() {
  unsigned long startTimeMicros = 0;
  unsigned long startTimeMillis = 0;
  unsigned long endTimeMicros = 0;
  unsigned long endTimeMillis = 0;
  startTimeMillis = millis();
  startTimeMicros = micros();

  compass.read(); // 2 millis, 1900 micros
  float heading = compass.heading(); // 1 millis, 900 micros

  endTimeMicros = micros()-startTimeMicros;
  endTimeMillis = millis()-startTimeMillis;
  Serial.print(endTimeMillis);
  Serial.print(" millis, ");
  Serial.print(endTimeMicros);
  Serial.print(" micros, heading: ");
  Serial.println(heading);
  
  delay(500);
}

It is worse than I thought, didn’t realise it took longer then 9999 micros, infact it takes over 3 ms

If you are. concerned about the length of time between updates, then drop the delay(500).

This is code is just to show the delay, of course I don't have a 300ms delay in my actual code.

The 3 axis compass must read the accelerometer to determine the compass axis to use, then does a math computation to determine the heading

that's not really the point here

Maarten123:
that’s not really the point here

What is the point? Have you looked at the read() and heading() code in the LSM303 library? I use the LSM303 and library from Adafruit, and the calculations required to obtain the heading are not as simple as you might imagine.