Adafruit 10-DOF IMU Breakout magnetometer

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
scootergarrett
 
Posts: 6
Joined: Sat Mar 21, 2015 4:33 pm

Adafruit 10-DOF IMU Breakout magnetometer

Post by scootergarrett »

I'm having problems with the magnetometer portion of the LSM303 I try to read the output registers and I get a value that makes sense but then when I try to read it again its allays the same, no matter what. I set the regiseter MR_REG_M (0x02) to 0b00000000 so it should be in continuous mode but I ask it for register 0x03 and 0x04 get values wait and ask it again and it the same. Am I missing something, like why are there only 7 bits shown in the default column of CRA_REG_M and CRB_REG_M registers in table 17 on page 24 of the data sheet?

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by Franklin97355 »

Could you post your code and how you are connecting things together?

User avatar
scootergarrett
 
Posts: 6
Joined: Sat Mar 21, 2015 4:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by scootergarrett »

Yep,
So here is my Circuit and the link the the CircuitLab page (which is a great circuit simulator). And here is my code:

Code: Select all

// for NerdKits with ATmega324 //

// This .h file takes care of the LCD and other stuff unrelated to the problem  //
#include "/home/garrett/Libraries_Mine/Lib324.h"


#define TestPin A,0
#define SCL_CLOCK 100000L

void I2C_INT()
{
    TWBR = ((F_CPU/SCL_CLOCK)-16)/2;        // [Bit rate register] controls signal speed p.232
    TWCR |= (1<<TWEA) | (1<<TWEN) | (1<<TWINT);                      // [Control register] p.232
    TWSR &= ~((1<<TWPS1) | (1<<TWPS0));     // [Status register 0-1] controls signal speed p.234


    return;
}

// Slave address of magnetic field interface p.22 //
#define SlaveAddress 0x3C

/// Sends Data to Register in SlaveAddress ///
uint8_t SendI2CData(uint8_t Register, uint8_t Data)
{
    /// Set start ///
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));   // wait
    if ((TWSR & 0xF8) != 0x08)
    {
        return -1;
    }

    /// Send slave address + w ///
    TWDR = SlaveAddress;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));
    if ((TWSR & 0xF8) != 0x18)
    {
        return -2;
    }

    /// Send sub address ///
    TWDR = Register;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));
    if ((TWSR & 0xF8) != 0x28)
    {
        return -3;
    }

    /// Send  Data ///
    TWDR = Data;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));
    if ((TWSR & 0xF8) != 0x28)
    {
        return -4;
    }

    /// Stop bit ///
    TWCR = (1<<TWINT) | (1<<TWSTO);


    return 0;
}

/// Gets Data in Register in SlaveAddress ///
uint8_t GetI2CData(uint8_t Register, uint8_t *Data)
{
    /// Set start ///
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));   // wait
    if ((TWSR & 0xF8) != 0x08)
    {
        return -1;
    }

    /// Send slave address + w ///
    TWDR = SlaveAddress;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));
    if ((TWSR & 0xF8) != 0x18)
    {
        return -2;
    }

    /// Send  sub address ///
    TWDR = Register;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));
    if ((TWSR & 0xF8) != 0x28)
    {
        return -3;
    }

    /// Set repeat start ///
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));   // wait
    if ((TWSR & 0xF8) != 0x10)
    {
        return -4;
    }


    /// Send slave address + r ///
    TWDR = SlaveAddress | 0x01;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));   // wait
    if ((TWSR & 0xF8) != 0x40)
    {
        return -5;
    }

    /// Get data ///
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));   // wait

    /// Stop bit ///
    TWCR = (1<<TWINT) | (1<<TWSTO);

    *Data = TWDR;


    return 0;
}


#define DataAddress 0x05
FILE lcd_stream;
int main()
{
    uint8_t k;
    int16_t AccelX;
    FILE lcd_stream;

    OUTPUT(TestPin);

    // Set up the LCD screen //
    lcd_init();
    fdev_setup_stream(&lcd_stream, lcd_putchar, 0, _FDEV_SETUP_WRITE);
    lcd_clear_and_home();

    // Set up the I2C //
    I2C_INT();

    delay_ms(100);

    // Make register MR_REG_M 0x00 so magnetic field  sensor is in 'Continuous conversion mode'
    // as shown on p.38 ///
    if( SendI2CData(0x02, 0b00000000) )
        goto error;


    // Loop through getting register 0x05 and 0x06 for magnetic filed data ///
    while(true)
    {
        if ( GetI2CData(DataAddress, &k) )
            goto error;

        lcd_line_two();
        fprintf_P(&lcd_stream, PSTR("Address 0x05: %X  "), k );

        if ( GetI2CData(DataAddress + 0x01, &k) )
            goto error;

        lcd_line_three();
        fprintf_P(&lcd_stream, PSTR("Address 0x06: %X  "), k );

        delay_ms(1000);
        TOGGLE(TestPin);
    }


    error:
    fprintf_P(&lcd_stream, PSTR("ERROR"));
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR("TWSR & 0xF8: %X  "), TWSR & 0xF8 );

    while(1)
    {
        TOGGLE(TestPin);
        delay_ms(100);
    }


    return 0;
}
Again the problem being I only get magnetometer data once, then its locked in and I have power the whole thing down to get a new set. Sorry I don't use an arduino and just use libraries I'm a NerdKits guy.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by Franklin97355 »

The Nerdkit uses a different processor than the Arduino. You should ask over on their site if this works. Also if you are using the Adafruit libraries make sure you have the latest versions of all the libraries so you don't have version conflicts.

User avatar
scootergarrett
 
Posts: 6
Joined: Sat Mar 21, 2015 4:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by scootergarrett »

The code is compatible with a 328p (which the Arduino Uno uses) so I set it up on one as such with the same results. I never really mentioned I have the accelerometer portion of the sensor working fine its just the magnetometer that I can only get one sample from. The NerdKits forums are great an all but there are not that many people I doubt someone there will have that much experience with this specific sensor. I'm goint to keep reading on this end.
Thnaks

User avatar
scootergarrett
 
Posts: 6
Joined: Sat Mar 21, 2015 4:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by scootergarrett »

I got it to work, I will post my results but I should really spend some time outside

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by Franklin97355 »

I'm glad you got it working and will be interested in what you found. Please keep us posted and enjoy your time outside.

User avatar
scootergarrett
 
Posts: 6
Joined: Sat Mar 21, 2015 4:33 pm

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by scootergarrett »

So the issue I was having was the magnetometer wants to give out all the data from each axis before going back to the first axis. I have a more comprehensive write up here .

I have used my fair share of sensors including pressure sensors but the math required to get the raw pressure data into barometric pressure pressure is like nothing I have ever seen before. How can this device not do that math internal and send out a 32bit pressure value?

Anyways every thing is working and I learned a lot on the way

Thanks

User avatar
drone100
 
Posts: 9
Joined: Fri Apr 10, 2015 4:50 am

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by drone100 »

Dear All
We get some trable with the 10 DOF in pitch and roll data. if we get some shaking with the sensor on the table we read movement on pitch and roll ...
We post the arduino serial monitor images.
Hope someone can help us
many thanks and have a nice day
Attachments
10 Dof shaking problems
10 Dof shaking problems
im.png (44.22 KiB) Viewed 571 times

User avatar
drone100
 
Posts: 9
Joined: Fri Apr 10, 2015 4:50 am

Re: Adafruit 10-DOF IMU Breakout magnetometer

Post by drone100 »

Our project is to let one led ligth's up wen the gyro sensor go up or down on picth and roll on bank at more or less then 60° . the redengs is fine if the adafruit 10 dof is moving slow ( very slow) if you give some orizontel shaking without any pitch and roll inclinations, the pitch and roll value is going crazy.
in attahcment some pictures and arduino sketch.
many thanks
best regards to all
Attachments
sketch.txt
(3.93 KiB) Downloaded 182 times
image5.jpeg
image5.jpeg (109.53 KiB) Viewed 569 times
image2.jpeg
image2.jpeg (114.79 KiB) Viewed 569 times

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”