0

LIS3MDL Readings over I2C
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

LIS3MDL Readings over I2C

by tlee00 on Wed Sep 23, 2020 11:05 am

Hi all,

I recently purchased 3 breakout boards for the LIS3MDL 3-axis magnetometer (Product ID: 4479). I have two problems and would appreciate some feedback/advice! I suspect they may be related, so I am putting both in the same thread.

1. The first is inconsistent i2c communication upon booting up. An i2c scanner can consistently find the device at 0x1C at 100k and 400k bus speeds. I can soft reset (press the button) and hard reset (unplug, replug) with no issues. I tested this on both a MKRZero (4.7k internal pullups) and Arduino Zero (no internal pullups, relying on 10k pullups on breakout). However, if I upload the lis3mdl_demo from the github library (https://github.com/adafruit/Adafruit_LIS3MDL), I can only 'find' the LIS3MDL with a hard reset. It only properly communicates once after a full power cycle - soft reset leads to no device found.

2. If I do a hard reset, I can start trying to set different parameters. I can reliably set and get the different modes (between single, continuous, and power down). I can reliably set and get the performance mode (low, medium, high, ultrahigh). However, I am unable to setRange or setDataRate properly. Whenever I try to change these, the registers now read 11 (for range) and 1111 (for data rate) regardless of the intended value. This problem occurs even if I set the values to be the exact same as the default init (ultra high mode, 155 Hz, 4 gauss, continuous). Control register address, bits, and shifting all looks good compared to datasheet (thank you Adafruit!).

I am unable to get any reasonable values even if I do not attempt to change any settings. The raw readings are stuck at X: -20674 Y: -1 Z: -15169 and the event based readings are X: -1208.30 Y: -0.06 Z: 886.56 uTesla, although they change slightly if I hard reset.

Thanks for your time and hope to hear back! I am stumped.

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by adafruit_support_carter on Fri Sep 25, 2020 12:09 pm

1) By no device found, do you mean the sketch outputs the "Failed to find LIS3MDL chip" message and parks?

2) How are you reading back the register values after setting them?

adafruit_support_carter
 
Posts: 20626
Joined: Tue Nov 29, 2016 2:45 pm

Re: LIS3MDL Readings over I2C

by tlee00 on Fri Sep 25, 2020 3:09 pm

Thanks for your reply.

1) Yes, I mean that the sketch "Failed to find LIS3MDL chip" message and parks in the while loop.

2) I am using the return values of the get functions provided in the library:

Code: Select all | TOGGLE FULL SIZE
lis3mdl_performancemode_t getPerformanceMode(void);
lis3mdl_operationmode_t getOperationMode(void);
lis3mdl_dataRate_t getDataRate(void);
lis3mdl_range_t getRange(void);


and printing them in directly to Serial in binary

Code: Select all | TOGGLE FULL SIZE
Serial.println(lis3mdl.getDataRate(), BIN);


I only started doing this because the setDataRate didn't appear to work, since the switch statement did not print out the value in Hz. No cases were entered because the value appears to be 0b1111, which is not one of the LIS3MDL_DATARATE_XX_Hz values.

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by adafruit_support_carter on Fri Sep 25, 2020 3:23 pm

Let's make sure it's not a hardware issue. Can you post a photo of your setup showing how everything is connected.

adafruit_support_carter
 
Posts: 20626
Joined: Tue Nov 29, 2016 2:45 pm

Re: LIS3MDL Readings over I2C

by tlee00 on Mon Sep 28, 2020 3:04 pm

PXL_20200928_185120640.jpg
Bare Minimum I2C Hookup
PXL_20200928_185120640.jpg (719.06 KiB) Viewed 173 times
Sure thing. I switched to a Trinket M0 for testing (it is the only board I currently have that is Adafruit-made). However, it is still the same SAMD21 and I am having the same problems.

Photo connections (bare minimum):
Trinket --> LIS3MDL
3V --> VIN (red)
GND --> GND (green)
D2 (SCL) --> SCL (white)
D0 (SDA) --> SDA (brown)

I was able to get data from the Pololu LIS3MDL library, without any reset issues. However, I had to pull the interrupt pin high (INT --> VIN) to improve consistency. However, this did not help with my Adafruit library problems above. It shouldn't, since I am disabling interrupt for now as I debug. I have tested with all three LIS3MDL breakouts.

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by adafruit_support_carter on Mon Sep 28, 2020 3:29 pm

Thanks for the photo. Now, with that setup, try running this I2C scanner sketch:
https://playground.arduino.cc/Main/I2cScanner/
and see if it at least sees the LIS3MDL address.

adafruit_support_carter
 
Posts: 20626
Joined: Tue Nov 29, 2016 2:45 pm

Re: LIS3MDL Readings over I2C

by tlee00 on Tue Sep 29, 2020 10:38 am

The i2c scanner can consistently find the device at 0x1C at 100k and 400k bus speeds. I can soft reset (press the button) and hard reset (unplug, replug) with no issues.

I was able to get data from the Pololu LIS3MDL library, without any reset issues. However, I am very curious about why the Adafruit library is not working, and it has many nice helper functions for changing settings in a readable manner.

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by adafruit_support_carter on Tue Sep 29, 2020 1:54 pm

Really weird issue. You're seeing the device with the I2C scan. You can get it to work with another library. I just tested a LIS3MDL with a Trinket M0 here and it worked fine:
Screenshot from 2020-09-29 10-44-16.png
Screenshot from 2020-09-29 10-44-16.png (27.98 KiB) Viewed 158 times


Check and make sure all your libraries are up to date with latest versions.

adafruit_support_carter
 
Posts: 20626
Joined: Tue Nov 29, 2016 2:45 pm

Re: LIS3MDL Readings over I2C

by tlee00 on Thu Oct 08, 2020 11:38 am

Sorry for the delay. I just now reinstalled Adafruit BusIO (1.5.0), Adafruit Unified Sensor (1.1.4), and Adafruit LIS3MDL (1.0.7). I reuploaded lis3mdl_demo to my Trinket M0.

These problems are fixed:
-be able to get and set values correctly
-data rate properly recognized and printed to screen

These problems persist:
-LIS3MDL not recognized on reset button (only on first power cycle)
-printed readings sit at weirdly high values, don't change with motion

I can see that your data rate was recognized at 155 Hz (which is now happening for me). The readings in uTesla seem quite high and are not changing over the small screenshot. Is this what you expect? Did the values settle closer to Earth's magnetic field and vary (at least a bit?). Or is the default value printed when the event is not ready? Even when sitting at rest on the table I expect a little bit of variation in the readings.

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by adafruit_support_carter on Thu Oct 08, 2020 11:41 am

These problems persist:
-LIS3MDL not recognized on reset button (only on first power cycle)
-printed readings sit at weirdly high values, don't change with motion

This may be a race condition or something. But let's focus on the other behavior first.

If you have everything powered up, and then upload the example sketch I ran above, what do you get in the serial monitor?

adafruit_support_carter
 
Posts: 20626
Joined: Tue Nov 29, 2016 2:45 pm

Re: LIS3MDL Readings over I2C

by tlee00 on Thu Oct 08, 2020 11:46 am

Sure, using the same settings/demo file:

port.PNG
port.PNG (26.32 KiB) Viewed 131 times

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by tlee00 on Thu Oct 08, 2020 11:52 am

Hello again,

I just decided to become suspicious of my breadboard, since the readings moved when I held it vertically.

Ditched the breadboard and connected LIS3MDL directly to my Trinket M0 with female jumpers. Here is the new output:

port.PNG
port.PNG (28.51 KiB) Viewed 131 times


I believe this is good news, and still investigating, but wanted to let you know asap. This might be a good argument for the qwiic connectors...

tlee00
 
Posts: 7
Joined: Wed Sep 23, 2020 10:48 am

Re: LIS3MDL Readings over I2C

by adafruit_support_carter on Thu Oct 08, 2020 12:11 pm

Interesting. Good check. Breadboards can definitely wear out over time. Another thing to try is to simply stage things in a different area on the breadboard.

adafruit_support_carter
 
Posts: 20626
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.