2025-09-08 2:27 AM
I am developing a wireless sensor to detect vehicles using the IIS2MDC. My main logic is:
BOOT -> Init sensor -> Measure environment -> Set detection thresholds and offsets based on environment -> Set interrupts -> Wait for interrupt
On interrupt -> Acquire vehicle heading -> Send RF signal to gateway
After some hours working properly the sensor starts sending continuous signals without a vehicle or other metalic object nearby. How can i correct this behaviour? Any help is welcomed.
Below i'll leave the application code for the IIS2MDC initialization:
CFG_REG_A is always 0x90
CFG_REG_B is always 0x09
CFG_REG_C is always 0x50
INT_CTRL_REG is 0x00 on startup and 0x45 on work mode
#define IIS2MDC_STARTUP_CFG {0x90, 0x09, 0x50, 0x00}
#define IIS2MDC_WORK_CFG {0x90, 0x09, 0x50, 0x45}
bool iis2mdc_startup_mode() {
uint8_t dev_id = 0;
bool error = iis2mdc_i2c_read(WHO_AM_I, &dev_id, 1);
if (dev_id != DEVICE_ID || error) return false;
uint8_t cfg_reg_a = (1<<5); // soft reset
if (iis2mdc_i2c_write(CFG_REG_A, &cfg_reg_a, 1)) return false;
Task_sleep(5);
cfg_reg_a = (1<<6); // reboot
if (iis2mdc_i2c_write(CFG_REG_A, &cfg_reg_a, 1)) return false;
Task_sleep(MS_TO_TICKS(20));
uint8_t config[4] = IIS2MDC_STARTUP_CFG;
if (iis2mdc_i2c_write(CFG_REG_A, config, 4)) return false;
return true;
}
bool iis2mdc_work_mode() {
uint8_t config[4] = IIS2MDC_WORK_CFG;
if (iis2mdc_i2c_write(CFG_REG_A, config, 4)) return false;
return true;
}
bool iis2mdc_learn() {
uint8_t i = 0;
int64_t x = 0;
int64_t y = 0;
int64_t z = 0;
while (i < REF_SAMPLES) {
uint8_t status = 0;
if (iis2mdc_i2c_read(STATUS_REG, &status, 1)) return false;
if (status & 0x08) {
MagneticRawData_t raw_data = {0,0,0};
if (iis2mdc_i2c_read(OUTPUT_REG, (uint8_t*)&raw_data, OUTPUT_REG_LEN)) return false;
x += raw_data.x;
y += raw_data.y;
z += raw_data.z;
i++;
}
}
x = x / REF_SAMPLES;
y = y / REF_SAMPLES;
z = z / REF_SAMPLES;
int16_t offsets[3] = {x,y,z};
iis2mdc_i2c_write(OFFSET_REGISTER, (uint8_t*)offsets, OFFSET_REG_LEN);
uint16_t mag = 4 * sqrt(y*y + z*z) / REF_SAMPLES;
if (iis2mdc_i2c_write(INT_THRESH_REG, (uint8_t*)&mag, 2)) return false;
return true;
}
void main() {
// Abstracted main
iis2mdc_startup_mode();
iis2mdc_learn();
iis2mdc_work_mode();
// Start a task that is released on interrupt
// and send the RF signal
}
2025-09-11 5:46 AM
Hi @francsicosousa ,
Can you log the magnetic sensor output during the operation to check if the output data is drifting, causing the threshold to be not valid anymore?
If this is the case, you can achieve a more stable sensor output by enabling the automatic offset compensation feature. It can be enabled by setting bit 2 of reg CFG_REG_B to '1'.