Reading Data from I2C Magnetometer

This is my first time posting to this form. I am trying to access data from an IMU
(SparkFun 9DoF SEN-13944), focusing on the Magnetometer. The I2C address is listed as 0X1e. I have borrowed and adapted code that had been posted by others on this form working on I2c communication (thanks to all those who have shared their efforts). I am receiving data from this address. However, the data does not change as I move the sensor and does not make sense when translated from hex to decimal. I have tried changing the transmission speed for the code and for the serial monitor with no luck. I have also changed the number of Bytes being read - this does change the output but does not make the data any more reasonable.

The code I am using is:

#include <stdio.h>
#include <string.h>
#include "daisy_seed.h"
#include "daisy_core.h"

using namespace daisy;

DaisySeed hw;


{
    static DaisySeed seed;
    unsigned char address = 0x1e;
    uint8_t testData[33];
    long counter = 1L;

    seed.Configure();
    seed.Init();

    seed.StartLog(false);
    System::Delay(5000);

    static constexpr I2Cint main(void)Handle::Config _i2c_config
        = {I2CHandle::Config::Peripheral::I2C_1,
           {{DSY_GPIOB, 8},  // SCL op Daisy Seed Pin 12
            {DSY_GPIOB, 9}}, // SDA op Daisy Seed Pin 13
           I2CHandle::Config::Speed::I2C_100KHZ,
           I2CHandle::Config::Mode::I2C_MASTER};


    I2CHandle _i2c;
    _i2c.Init(_i2c_config);

    while(1) {
        // Ask for 31 bytes.
        uint8_t number = 32;
        uint8_t sensor_Data = 0;
        seed.PrintLine("%05ld Asking for %d bytes on address %0x.", counter, number, address);
        I2CHandle::Result i2cResult = _i2c.TransmitBlocking(address, &number, 1, 500);
        
        
        if (i2cResult == I2CHandle::Result::OK) {
            seed.PrintLine("%05ld Successfully transmitted requested number of bytes: %0x.", counter, number);

            // Receive the requested number of bytes + CRC
            seed.PrintLine("%05ld Receiving %0d bytes from address %0x.", counter, number + 1, address);                                  
            i2cResult = _i2c.ReceiveBlocking(address, testData, number + 1, 500);
            if(i2cResult == I2CHandle::Result::OK) {
                seed.PrintLine("%05ld %0d bytes were received from address %0x.", counter, number, address);
                seed.PrintLine("%05ld Received %0x.", counter, testData);
                               
                for(int i = 0; i < 32; i++) {
                    //uint8_t digit = &testData[i];
                    seed.PrintLine("assel digit %0x", testData[i]);
                }
                
            }
        } else {
            seed.PrintLine("%05ld Request for data not acknowledged.", counter);
        }
        
        System::Delay(1000);
        counter++;
    }
}

The data I am receiving looks like this

00013 Asking for 32 bytes on address 1e.
00013 Successfully transmitted requested number of bytes: 20.
00013 Receiving 33 bytes from address 1e.
00013 32 bytes were received from address 1e.
00013 Received 2001ffb0.
assel digit 18
assel digit 0
assel digit 3
assel digit 0
assel digit 0
assel digit 0
assel digit 0
assel digit 0
assel digit 8b
assel digit 8
assel digit 5b
assel digit ff
assel digit 4
assel digit f5
assel digit 8b
assel digit 8
assel digit 5b
assel digit ff
assel digit 4
assel digit f5
assel digit 8b
assel digit 8
assel digit 5b
assel digit ff
assel digit 4
assel digit f5
assel digit 8b
assel digit 8
assel digit 5b
assel digit ff
assel digit 4
assel digit f5

Any suggestions would be greatly appreciated.

Hey Greg!

Hopefully there is somebody else who has this same sensor out there on the forum. You may encounter them in the Discord forum too if you haven’t checked it out (Daisy). Please feel free to ask there too.

In the meantime, it looks like this sensor has an Arduino library, so it could be worth your time to double check if the hardware is hooked up correctly and working by using DaisyDuino. Or with a normal Arduino if you have one around. Apologies if you already done this.

I had good luck using DaisyDuino for an I2C sensor and used an external library that their developers made for it.
DaisyDuino also comes with bunch of great sounding examples, so it should be good enough to at least get a solid prototype/proof-of-concept going in the meantime :slight_smile:

1 Like

Thanks for the suggestion. That is the direction I was planning at this point and it does seem to be working so far. I would still love to get this or a similar sensor working using the daisy library if possible, but can finish this project off using DaisyDuino .

1 Like

Hey Greg!

I hope your project is going well.

One thing that I just remembered about I2C component yesterday is that Daisy does not have a pull-up resistor.
So, adding pull-up resistors could be a potential solution if you haven’t done so yet! Please reference this post on how to do that. Adding 4+ More DACs/CVs with Quad DAC! (Also, I2C tips!). Quad DAC is an I2C component so it’ll apply to what you’re doing.

Good luck!!