Arduino Due and LSM303DLHC. More I2C issues?

So, I'm trying to get my Due to connect to an LSM303DLHC.

Pin Assignment (LSM303DLHC : DUE)
SCL:SCL
SDA:SDA
VIN:3.3V
GND:GND

I'm using the library downloaded from: GitHub - pololu/lsm303-arduino: Arduino library for Pololu LSM303 boards

According to the read me / description. The Arduino Due's wire library does not allow the init() method to be called without specifying what breakout board you are using. So, I've done that and set sa0_high (I've tried all 3).

I have a friend who is using this same breakout board and it works well with the omega. I might check with him to see if our breakout board is damaged. But I feel like it may be an issue with the library / code? Anyone have any hints?

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

LSM303 compass;
LSM303::vector<int16_t> running_min = {32767, 32767, 32767}, running_max = {-32768, -32768, -32768};

char report[80];

void setup() {
  Serial.begin(9600);
  Wire.begin();  
  compass.init(LSM303::device_DLHC, LSM303::sa0_high);
  compass.enableDefault();
}

void loop() {  
  compass.read();
  
  running_min.x = min(running_min.x, compass.m.x);
  running_min.y = min(running_min.y, compass.m.y);
  running_min.z = min(running_min.z, compass.m.z);

  running_max.x = max(running_max.x, compass.m.x);
  running_max.y = max(running_max.y, compass.m.y);
  running_max.z = max(running_max.z, compass.m.z);
  
  snprintf(report, sizeof(report), "min: {%+6d, %+6d, %+6d}    max: {%+6d, %+6d, %+6d}",
    running_min.x, running_min.y, running_min.z,
    running_max.x, running_max.y, running_max.z);
  Serial.println(report);
  
  delay(100);
}

My output shows: min: { +0, +0, +0} max: { +0, +0, +0}

  • infinity.

Have you tried connecting to SDA1 and SCL1

You know, I should have tried that last night too.

That's just a wire1() call right?

I'll give it a shot when in a few hours.

Try this:

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

LSM303 compass;
LSM303::vector running_min = {32767, 32767, 32767}, running_max = {-32768, -32768, -32768};

void setup() {
  Serial.begin(9600);
  Wire.begin();
  compass.init(LSM303DLHC_DEVICE, LSM303_SA0_A_HIGH);
  compass.enableDefault();
}

void loop() {  
  compass.read();
  
  running_min.x = min(running_min.x, compass.m.x);
  running_min.y = min(running_min.y, compass.m.y);
  running_min.z = min(running_min.z, compass.m.z);

  running_max.x = max(running_max.x, compass.m.x);
  running_max.y = max(running_max.y, compass.m.y);
  running_max.z = max(running_max.z, compass.m.z);
  
  Serial.print("M min ");
  Serial.print("X: ");
  Serial.print((int)running_min.x);
  Serial.print(" Y: ");
  Serial.print((int)running_min.y);
  Serial.print(" Z: ");
  Serial.print((int)running_min.z);

  Serial.print(" M max ");  
  Serial.print("X: ");
  Serial.print((int)running_max.x);
  Serial.print(" Y: ");
  Serial.print((int)running_max.y);
  Serial.print(" Z: ");
  Serial.println((int)running_max.z);
  
  delay(100);
}

Palliser:
Try this:

#include <Wire.h>

#include <LSM303.h>

LSM303 compass;
LSM303::vector running_min = {32767, 32767, 32767}, running_max = {-32768, -32768, -32768};

void setup() {
 Serial.begin(9600);
 Wire.begin();
 compass.init(LSM303DLHC_DEVICE, LSM303_SA0_A_HIGH);
 compass.enableDefault();
}

void loop() {  
 compass.read();
 
 running_min.x = min(running_min.x, compass.m.x);
 running_min.y = min(running_min.y, compass.m.y);
 running_min.z = min(running_min.z, compass.m.z);

running_max.x = max(running_max.x, compass.m.x);
 running_max.y = max(running_max.y, compass.m.y);
 running_max.z = max(running_max.z, compass.m.z);
 
 Serial.print("M min ");
 Serial.print("X: ");
 Serial.print((int)running_min.x);
 Serial.print(" Y: ");
 Serial.print((int)running_min.y);
 Serial.print(" Z: ");
 Serial.print((int)running_min.z);

Serial.print(" M max ");  
 Serial.print("X: ");
 Serial.print((int)running_max.x);
 Serial.print(" Y: ");
 Serial.print((int)running_max.y);
 Serial.print(" Z: ");
 Serial.println((int)running_max.z);
 
 delay(100);
}

I tried using LSM303DLHC_DEVICE instead of LSM303::device_DLHC last night. The library complains about it. Inside the libraries keywords.txt it's no longer LSM303DLHC_DEVICE as the library online says. It's device_DLHC. Same with the sa0 high / low.

From the keywords.txt file:

device_DLH LITERAL1
device_DLM LITERAL1
device_DLHC LITERAL1
device_D LITERAL1
device_auto LITERAL1
sa0_low LITERAL1
sa0_high LITERAL1
sa0_auto LITERAL1

Updated: Just plugged the LSM303DLHC into an Arduino Mega (2560), and ran the sample code and it worked flawlessly. It's definitely and issue with the libraries / Due. Tried the SCL1 / SDA1, but I think the compasses INIT() method defaults the use to the SDA / SCL on all boards.

Given that this LSM303 library was built for AVR devices, I'd recommend you to change in LSM303.h
the line: #include <Arduino.h> for #include <variant.h> . Sadly I don't have a device at hand to help you more.
Good luck.
P