Skip to main content
Visitor II
May 15, 2025
Question

LIS2MDL, MAGNETOMETER

  • May 15, 2025
  • 3 replies
  • 625 views

Hello, I am using the LIS2MDL development kit. I have two questions.

  1. When I measure with two development kits in the same environment, I get different values.
    Both are 5 cm away from each other and there is no factor that will disrupt the magnetic field around either of them.
    What could be the reason for this?
  2. When I bring a magnet closer to any of the development kits without changing its position and move it away, I get different measurements than the first values ​​it measured.
    For example:
    x: 500 y: 100 z: 800
    After moving the magnet closer and further away:
    x: 200 y: -200 z: 450
    When I reset the card nothing changes. It still reads the bad value. The position of the development kit does not change and I guarantee that the magnet is far enough away.
    What is the reason for this?

 

    This topic has been closed for replies.

    3 replies

    ST Employee
    May 16, 2025

    Hi @AlpoTemi ,

    Are both development kits configured the same? Such as ODR, operating mode, FS, and so on.

    In order to better understand the issue, could you please send us the kit configuration and some output raw data? Are you able to read the WhoAmI register correctly from both magnetometer? And please share also a picture of you setup.

     

    AlpoTemiAuthor
    Visitor II
    May 20, 2025
    Hello,I couldn't put the other configuration because of data limit but both are like this
     
    esp_err_t lis2mdl_1_init(void)
    {

     

    LIS2MDL_1.max_retries = MAX_RETRIES;
    LIS2MDL_1.name = "LIS2MDL_1";
    MAG_1.hbus = spi_handle;
    MAG_1.cs_pin = SPI_CS_PIN_1;

     

    magnetometer_init(&LIS2MDL_1.stmdev_ctx_t, &MAG_1);

     

    if (LIS2MDL_1.is_configuration_required == true)
    {
    vTaskDelay(pdMS_TO_TICKS(20));
    while (LIS2MDL_1.retry_count < LIS2MDL_1.max_retries && LIS2MDL_1.init_flag == false)
    {
    lis2mdl_device_id_get(&LIS2MDL_1.stmdev_ctx_t, &LIS2MDL_1.whoamI);

     

    if (LIS2MDL_1.whoamI == LIS2MDL_ID)
    {
    ESP_LOGI(TAG, "LIS2MDL_1 Detected! ID-->%02X", LIS2MDL_1.whoamI);

     

    lis2mdl_reset_set(&LIS2MDL_1.stmdev_ctx_t, PROPERTY_ENABLE);
    do
    {
    lis2mdl_reset_get(&LIS2MDL_1.stmdev_ctx_t, &LIS2MDL_1.reset_flag);
    } while (LIS2MDL_1.reset_flag);

     

    ESP_LOGI(TAG, "LIS2MDL_1 Reset Succesfull");

     

    /*********Block Data Update *********/
    uint8_t temp = 0;
    lis2mdl_block_data_update_set(&LIS2MDL_1.stmdev_ctx_t, PROPERTY_ENABLE);
    lis2mdl_block_data_update_get(&LIS2MDL_1.stmdev_ctx_t, &temp);
    ESP_LOGI(TAG, "CONFIG_REG_C Block Data Update: %s", temp == PROPERTY_ENABLE ? "PROPERTY_ENABLE" : "PROPERTY_DISABLE");
    /*********Block Data Update End*********/

     

    /* Set Output Data Rate */
    const char *odr_str;
    lis2mdl_data_rate_set(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_ODR_100Hz);
    lis2mdl_data_rate_get(&LIS2MDL_1.stmdev_ctx_t, (lis2mdl_odr_t *)&temp);
    switch (temp)
    {
    case LIS2MDL_ODR_10Hz:
    odr_str = "LIS2MDL_ODR_10Hz";
    break;
    case LIS2MDL_ODR_20Hz:
    odr_str = "LIS2MDL_ODR_20Hz";
    break;
    case LIS2MDL_ODR_50Hz:
    odr_str = "LIS2MDL_ODR_50Hz";
    break;
    case LIS2MDL_ODR_100Hz:
    odr_str = "LIS2MDL_ODR_100Hz";
    break;
    default:
    odr_str = "Unknown";
    break;
    }
    ESP_LOGI(TAG, "ODR: %s", odr_str);
    /**********Set Output Data Rate End********/
    lis2mdl_power_mode_set(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_HIGH_RESOLUTION);
    /* Set / Reset magnetic sensor mode */
    lis2mdl_set_rst_mode_set(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_SENS_OFF_CANC_EVERY_ODR);
    /* Enable temperature compensation on mag sensor */
    lis2mdl_offset_temp_comp_set(&LIS2MDL_1.stmdev_ctx_t, PROPERTY_ENABLE);
    /* Set magnetometer in continuous mode */
    lis2mdl_operating_mode_set(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_CONTINUOUS_MODE);

     

    lis2mdl_int_on_pin_set(&LIS2MDL_1.stmdev_ctx_t, 1);

     

    lis2mdl_drdy_on_pin_set(&LIS2MDL_1.stmdev_ctx_t, 0);

     

    lis2mdl_sim_t lis2mdl_sim;
    lis2mdl_spi_mode_get(&LIS2MDL_1.stmdev_ctx_t, &lis2mdl_sim);
    ESP_LOGI(TAG, "SPI MODE = %s", lis2mdl_sim == LIS2MDL_SPI_4_WIRE ? "LIS2MDL_SPI_4_WIRE" : "LIS2MDL_SPI_3_WIRE");

     

    lis2mdl_int_crtl_reg_t int_ctrl = {0};
    int_ctrl.ien = 1; // INT enable
    int_ctrl.iel = 0; // 0 is Pulsed, 1 is Latched
    int_ctrl.iea = 0; // 0 is Active Low, 1 is Active High
    int_ctrl.zien = 0; // Z axis enable or disable
    int_ctrl.yien = 1; // X axis enable or disable
    int_ctrl.xien = 0; // Y axis enable or disable

     

    lis2mdl_int_gen_conf_set(&LIS2MDL_1.stmdev_ctx_t, &int_ctrl);

     

    lis2mdl_int_gen_conf_get(&LIS2MDL_1.stmdev_ctx_t, &int_ctrl);
    ESP_LOGI(TAG, "INT_CTRL Register: ien=%d, iea=%d, iel=%d, xien=%d, yien=%d, zien=%d",
    int_ctrl.ien, int_ctrl.iea, int_ctrl.iel, int_ctrl.xien, int_ctrl.yien, int_ctrl.zien);

     

    uint16_t threshold = 300; // 1LSb = 1.5mG 300*1.5mG = 450mG
    lis2mdl_int_gen_threshold_set(&LIS2MDL_1.stmdev_ctx_t, threshold);
    ESP_LOGI(TAG, "INT GEN THRESHOLD: %d", threshold);

     

    int16_t temp_offset[3] = {0, 0, 0}; // 1LSb = 1.5mG 300*1.5mG = 450mG
    lis2mdl_mag_user_offset_set(&LIS2MDL_1.stmdev_ctx_t, temp_offset);
    lis2mdl_mag_user_offset_get(&LIS2MDL_1.stmdev_ctx_t, temp_offset);
    ESP_LOGI(TAG, "Magnetic Offset: X: %d, Y: %d, Z: %d", temp_offset[0], temp_offset[1], temp_offset[2]);

     

    lis2mdl_read_reg(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_CFG_REG_A, (uint8_t *)&temp, 1);
    ESP_LOGI(TAG, "CONFIG_REG_A Register: %d", temp);
    lis2mdl_read_reg(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_CFG_REG_B, (uint8_t *)&temp, 1);
    ESP_LOGI(TAG, "CONFIG_REG_B Register: %d", temp);
    lis2mdl_read_reg(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_CFG_REG_C, (uint8_t *)&temp, 1);
    ESP_LOGI(TAG, "CONFIG_REG_C Register: %d", temp);
    lis2mdl_read_reg(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_INT_CRTL_REG, (uint8_t *)&temp, 1);
    ESP_LOGI(TAG, "INT_CTRL Register: %d", temp);
    lis2mdl_read_reg(&LIS2MDL_1.stmdev_ctx_t, LIS2MDL_INT_SOURCE_REG, (uint8_t *)&temp, 1);
    ESP_LOGI(TAG, "INT_SOURCE Register: %d", temp);

     

    ESP_LOGI(TAG, "INIT DONE");
    LIS2MDL_1.init_flag = true;

     

    return ESP_OK;
    }

     

    ESP_LOGW(TAG, "LIS2MDL_1 Not Detected! Retrying... [%d/%d]", LIS2MDL_1.retry_count + 1, LIS2MDL_1.max_retries);
    LIS2MDL_1.retry_count++;
    vTaskDelay(pdMS_TO_TICKS(10));
    }
    }
    else
    {
    return ESP_OK;
    }

     

    ESP_LOGE(TAG, "LIS2MDL_1 Initialization Failed After %d Attempts!", LIS2MDL_1.max_retries);
    LIS2MDL_1.init_flag = false;
    return ESP_OK;
    }
    Super User
    May 20, 2025
    Super User
    May 16, 2025

    Welcome to the forum.

    Please see How to write your question to maximize your chances to find a solution for best results.

     


    @AlpoTemi wrote:
    1. When I measure with two development kits in the same environment, I get different values.

    By how much do they differ?

    As @AlpoTemi said, some example data and some good, clear photographs of your setup would help.

    AlpoTemiAuthor
    Visitor II
    May 16, 2025

    I will send you my outputs within a few hours. Don't forget to keep your notifications on. Thanks

    AlpoTemiAuthor
    Visitor II
    May 16, 2025

    Sure. My configurations for both sensors are the same.

    I have no issues with any reading or recording configurations.

    The photo is for informational purposes only about my work environment.

    I take my measurements without any magnetic interference around me. I even make sure to keep it well away from my phone or computer.

    WhatsApp Görsel 2025-05-16 saat 12.49.59_e7936252.jpg