cancel
Showing results for 
Search instead for 
Did you mean: 

IIS2DH Threshold, Duration and Activation settings and understanding questions Brief: Customer using IIS2DH, required to sense 5KPH / 3.1 MPH over 5 seconds before trigging interrupt.

Mike Riley
Associate

Good morning. Up front I apologize if these questions are simplistic in nature but I just really want to make sure I am understanding the datasheet and implementing correctly.

Below is the code I am using to init the sensor:

int32_t snt_iis2dh_init()
{
  int32_t retval = -EINVAL;
  uint8_t whoamI = 0;
  int32_t err = 0;
 
  // Get the I2C device from the device tree
  iis2dh_dev.i2c_dev = device_get_binding(SNT_I2C_BUS_NAME);
 
  if (!iis2dh_dev.i2c_dev)
  {
    retval = -ENODEV;
  }
  else
  {
    snt_iis2dh_accel_data_t accel_data = {0};
    // Set up the ST IIS2DH device context used by the ST drivers
    iis2dh_dev.dev_ctx.write_reg = platform_write;
    iis2dh_dev.dev_ctx.read_reg = platform_read;
    iis2dh_dev.dev_ctx.handle = (void *)iis2dh_dev.i2c_dev;
 
    /* Check device ID */
    iis2dh_device_id_get(&iis2dh_dev.dev_ctx, &whoamI);
 
    if (whoamI != IIS2DH_ID)
    {
      // Invalid Device ID
      retval = -EIO;
    }
    else
    {
      uint32_t pin_mask = 0;
 
      /* Enable Block Data Update */
      iis2dh_block_data_update_set(&iis2dh_dev.dev_ctx, PROPERTY_ENABLE);
 
      /* update the bandwidth setting
       * this may be optional but is helpful during analysis of sensor performance
       *   IIS2DH_AGGRESSIVE  = 0,
       *   IIS2DH_STRONG      = 1,
       *   IIS2DH_MEDIUM      = 2,
       *   IIS2DH_LIGHT       = 3,
       */
      iis2dh_high_pass_bandwidth_set(&iis2dh_dev.dev_ctx, IIS2DH_AGGRESSIVE);
      LOG_INF("\033[36mSNSR - Accell bandwidth set to %s\033[0m", iis2dh_bandwidth_to_str(IIS2DH_AGGRESSIVE));
 
      /* updated data rate
       * desired - IIS2DH_ODR_1Hz     = 0x01,
       * desired - IIS2DH_ODR_10Hz    = 0x02,
       * default - IIS2DH_ODR_50Hz	= 0x04,
       */
      /* Set Output Data Rate to 1Hz */
      iis2dh_data_rate_set(&iis2dh_dev.dev_ctx, IIS2DH_ODR_10Hz);
      LOG_INF("\033[36mSNSR - Accell data rate set to %s\033[0m", iis2dh_data_rate_to_str(IIS2DH_ODR_10Hz));
 
      /* Set full scale to 2g
       * desired - IIS2DH_2g
       * optional - IIS2DH_4g
       * optional - IIS2DH_8g
       * optional - IIS2DH_16g
       */
      iis2dh_full_scale_set(&iis2dh_dev.dev_ctx, IIS2DH_2g);
      LOG_INF("\033[36mSNSR - Accell range set to %s\033[0m", iis2dh_range_to_str(IIS2DH_2g));
 
      /* Set device in continuous mode with 12 bit resol. */
      /* update the resolution
       *   default - IIS2DH_HR_12bit
       *   optional - IIS2DH_NM_10bit
       *   desired - IIS2DH_LP_8bit
       */
      iis2dh_operating_mode_set(&iis2dh_dev.dev_ctx, IIS2DH_LP_8bit);
      LOG_INF("\033[36mSNSR - Accell op mode set to %s\033[0m", iis2dh_op_mode_to_str(IIS2DH_LP_8bit));
 
      // See Kconfig.sensor_module for explanation
      snt_iis2dh_threshold_set(0x10);   // Here is where most of my confusiong starts

After this it goes on to get the device binding and set gpio's. I have not modified that section.

Question 1: Interrupts - First a dumb question. Do I need both INT1 and INT2? I have only configured INT1.

Question 2: Threshold Values - Second is my real question. Simply put I am having trouble understanding what the datasheet is saying in table 57 and what value I should be using in snt_iis2dh_threshold_set() to achieve my requirements stated above.

0693W00000NsiacQAB.pngHere is the math I am using:

- Requirements state that 5kph or 3.1mph is the magnitude to determine movement
	  -- 5kph = 3.1 mph = 1.38582 m/s
	  --  v = f(t) = 1.38582t^2, a = f'(t) = 2.77164t, f'(1) = 2.77164 m/s^2 
	  - then to convert m/s^2  to mG:
	  	((2.77164 m/s^2  / 9.80665) * 1000.00) = 282.629 mG ~ 283 mG
 - iis2dh_int1_gen_threshold_set() then converts this to a range in mG to a range in uin8_t:  
	  -- (283 + 15) / 16 = 18.625 mG ~ 19 mG

Looking through this site I find THIS discussion and in that discussion I see the following in the accepted answer:

/* Set interrupt threshold to 0x10 -> 250 mg */
  iis2dh_int1_gen_threshold_set(&dev_ctx, 0x10);

and in the original posters question:

INT2_THS = 0b00011000; //24LSB = 1.488 g
...
INT1_THS = 0b00001100; //12LSB = 192 mg

Also on this site I have found THIS discussion where I see the following:

"Thank you. That means if I set the threshold to 0x08 for +-2g full scale than 16*8=128mg value is set for threshold value. Am I right or wrong? "

So in the first example:

/* Set interrupt threshold to 0x10 -> 250 mg */
  iis2dh_int1_gen_threshold_set(&dev_ctx, 0x10);

If the value passed to this function is then used in the following conversion:

0x10 = 16 (base10)

(16+15)/16 = 1.9 ~ 2 -> 0x02 written to the register so how does this equal 250mg?

Here is my understanding of mg/LSB:

Resolution = 8 bit so 256 possible measurements

+/- 2g is actually 4g's so each LSB is 4/256 = 0.015625 G = 15.625 mG which is roughly equal to 16mG which is what the datasheet says.

So in the register how does 00 0010 become 250mG?

16 * 2^2 = 32

In the other example where "16*8=128mg" if the 0x08 were written to the register and 1000 = 8 then 16*8 does in fact equal 128mG.

What I have come up with to pass to the threshold_set function is the following:

I need ~283mG but due to 16mG/LSB not possible but 288mG is so

to write 0x12 to the register I need to pass 273 to the threshold_set function. Is this correct?

Question 3: Activation - Thirdly, just be clear after INT1 triggers and accel values are read the interrupt flag is cleared, I don't really want repeated interrupts over and over again, so I think I set ACT_THS and ACT_DUR to put the sensor to sleep for a bit and avoid this. Is my understanding correct?

Question 4: Duration - And Finally, just want to make sure my understanding about duration is correct in the following code.

// note 50/10 = 5 seconds I think that is how it is being used. 
      iis2dh_int1_gen_duration_set(&iis2dh_dev.dev_ctx, 50);

Thank you all for your time and consideration and again I apologize for the long read and if the content seems duplicated by other posts.

0 REPLIES 0