Hello,
I am trying to interface nrf52840 with HMC5883L magnetometer sensor to read magnetic field x, y, z values.
When it comes to results, as far as i understood, values are not being read properly from the registers. I am configuring the sensor registers via i2c interface and trying to read the data.
I am not sure of the results. I am getting different values when the sensor is interfaced with ESP32. But with nrf52840 the results for z axis are almost constant everywhere.
I tried using the code in this forum. Reading from HMC5883L using I2C
Please find the code and output below and could you please let me know where i am going wrong and suggest any methods to read correct values from the registers and converting them to micro tesla
Magnetometer Test Result :
TWI scanner started.
TWI device detected at address 0x1e.
Raw Data ==> x : 526.000000, y : -228.000000, z : 0.000000
Result Data ==> x : 47.818180, y : -20.727272, z : 0.000000
*****************************
Raw Data ==> x : 528.000000, y : -229.000000, z : -3.000000
Result Data ==> x : 48.000000, y : -20.818182, z : -0.306122
*****************************
Raw Data ==> x : 526.000000, y : -228.000000, z : -1.000000
Result Data ==> x : 47.818180, y : -20.727272, z : -0.102041
*****************************
Raw Data ==> x : 527.000000, y : -228.000000, z : -2.000000
Result Data ==> x : 47.909088, y : -20.727272, z : -0.204082
*****************************
Code:
Definitions and Initializations:
#define HMC5883L_ADDR (0x3CU>>1)
#define HMC5883L_REG_CONF_A (0x00)
#define HMC5883L_REG_CONF_B (0x01)
#define HMC5883L_REG_MODE (0x02)
#define HMC5883L_REG_X_MSB (0x03)
#define HMC5883L_REG_X_LSB (0x04)
uint8_t reg[2] = { HMC5883L_REG_CONF_A, 0x70};
uint8_t reg1[2] = { HMC5883L_REG_CONF_B, 0x20};
uint8_t reg2[2] = { HMC5883L_REG_MODE, 0x00};
uint8_t reg3[1] = {HMC5883L_REG_X_MSB};
In Main:
//i2c is already initialized and did not paste that part of the code here.
err_code = nrf_drv_twi_tx(&m_twi, HMC5883L_ADDR, reg2, sizeof(reg2), false);
APP_ERROR_CHECK(err_code);
nrf_delay_ms(100);
err_code = nrf_drv_twi_tx(&m_twi, HMC5883L_ADDR, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
nrf_delay_ms(100);
err_code = nrf_drv_twi_tx(&m_twi, HMC5883L_ADDR, reg1, sizeof(reg1), false);
APP_ERROR_CHECK(err_code);
nrf_delay_ms(100);
uint8_t buff_t[6];
float combined_x, combined_y, combined_z, result_x, result_y, result_z;
while (true)
{
nrf_delay_ms(1000);
err_code = nrf_drv_twi_tx(&m_twi, HMC5883L_ADDR, reg3, sizeof(reg3),false); // Point to register
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_twi_rx(&m_twi, HMC5883L_ADDR, buff_t, 6); // Read register and fill buffer
APP_ERROR_CHECK(err_code);
combined_x = (int16_t)((buff_t[1]) | (buff_t[0] << 8 ));
combined_y = (int16_t)((buff_t[5]) | (buff_t[4] << 8 ));
combined_z = (int16_t)((buff_t[3]) | (buff_t[2] << 8 ));
printf("\n Raw Data ==> x : %f,\ty : %f,\tz : %f\n", combined_x, combined_y, combined_z);
result_x = combined_x / 1100 * 100;
result_y = combined_y / 1100 * 100;
result_z = combined_z / 980 * 100;
printf("\n Result Data ==> x : %f,\ty : %f,\tz : %f\n",result_x, result_y, result_z);
printf("*****************************\n");
}