Unable to read magnetometer data from MPU9250. Using ESP32 with ESP-IDF framework

89 Views Asked by At

I've been trying to read magnetometer data from MPU9250 for quite a while now. I have done the basic stuff like setting the INT_PIN_CFG register in MPU9250 and I am able to read the WIA register of the AK8963 magnetometer. My problem starts when I try to read the data. Continuous modes 1 and 2 didnt work for me so I decided to use Single Measurement Mode 1 and then read accordingly. I have also followed the step to read the ST2 register after I read the data registers, as mentioned in section 8.3.5 in the AK8963 sensor datasheet, but to no avail. What should I do now? I have attached my code in this post. I am willing to troubleshoot this in Arduino IDE as well. I just want to read the data at a high sampling rate.

 uint8_t mx_b[2];
        uint8_t mx_lsb,mx_msb;//check for ak8963
        int16_t mx;
        uint8_t st1,st2;

        err = ak8963_register_write(AK8963_CNTL1, 0x11);//setting 0b00010001 for single measurement
        if (err != ESP_OK) {
            printf("Failed to set magnetometer configuration: %d\n", err);
        } else {
            err = ak8963_register_read(AK8963_ST1,&st1,1);//reading ST1 register
            if(err != ESP_OK)
            {
                printf("Failed to read ST1");
            }
            else
            {
                if(st1 & 0x01)//checking if DRDY bit of st1 reg is 1
                {
                    err = ak8963_register_read(AK8963_HXL,&mx_b,2);//reading mx data
                    if(err != ESP_OK)
                    {
                        printf("Failed to read mag data");
                    }
                    else
                    {
                        mx = mx_b[0] | (mx_b[1]<<8);
                        printf("Mx: %d\n",mx);
                    }
                }
            }
        }

        err = ak8963_register_read(AK8963_ST2,&st2,1);//read st2, last step to reset mag data
        if(err != ESP_OK)
        {
            printf("Failed to read ST2");
        }

I followed the steps mentioned in the datasheet like mentioned and yet I am not able to get the data. Any help would be greatly appreciated. Thanks!

0

There are 0 best solutions below