Skip to main content
Visitor II
August 6, 2019
Question

Using LSM6SDO library in C

  • August 6, 2019
  • 2 replies
  • 1193 views

Hi I am trying to program the LSM6SDO sensor in C. I would like to get the pedometer to count the number of steps taken. However, it always shows zero, and does not respond to any step taken.

My main function looks something like this:

int main(void) {
 LSM6DSO_set_mode();
 LSM6DSO_Enable_X();
 
 LSM6DSO_Enable_Pedometer();
 LSM6DSO_Step_Counter_Reset();
 
 printf("a_x: %d\n", (int32_t) acc[0]);
 printf("a_y: %d\n", (int32_t) acc[1]);
 printf("a_z: %d\n\n", (int32_t) acc[2]);
 
 uint16_t StepCount = 0;
 
 while(1)
 {
 LSM6DSO_Get_Step_Count((uint16_t *) &StepCount);
 printf("Steps Taken: %d \n", StepCount);
 }
}

I am able to get readings from the acceleration (a_x, a_y, a_z), meaning the sensor is not broken.

Using the libraries given in stm32duino/LSM6DSO github, the functions I use to set up and read from the sensors are as shown:

void LSM6DSO_set_mode()
{
	uint8_t dataToWrite = 0; //Temporary variable
 
	dataToWrite = 0; //Start Fresh!
	dataToWrite |= LSM6DSO_BW_XL_200Hz;
	dataToWrite |= LSM6DSO_FS_XL_2g;
	dataToWrite |= LSM6DSO_ODR_XL_26Hz;
 
 I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_CTRL1_XL, dataToWrite);
 
	//Set the ODR bit
 I2CreadByte(LSM6DSO_ADDRESS, LSM6DSO_CTRL4_C, (uint8_t *) &dataToWrite);
	dataToWrite &= ~((uint8_t)LSM6DSO_BW_SCAL_ODR_ENABLED);
 
 I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_CTRL10_C, 0x3E);
 I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_TAP_CFG1, 0x40);
 I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_INT1_CTRL, 0x10 );
	I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_TAP_THS_6D, 0x03 );
	I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_INT_DUR2, 0x7F );
	I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_WAKE_UP_THS, 0x80 );
	I2CwriteByte(LSM6DSO_ADDRESS, LSM6DSO_MD1_CFG, 0x48 );
	
	delay(200);
}
 
 
bool LSM6DSO_Enable_Pedometer()
{
 lsm6dso_pin_int1_route_t val;
 
 /* Output Data Rate selection */
 if (LSM6DSO_Set_X_ODR(26.0f) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 /* Full scale selection */
 if (LSM6DSO_Set_X_FS(2) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 /* Enable pedometer algorithm. */
 if (lsm6dso_****_sens_set(®_ctx, LSM6DSO_****_BASE_MODE) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 /* Enable step detector on INT1 pin */
 if (lsm6dso_pin_int1_route_get(®_ctx, &val) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 val.emb_func_int1.int1_step_detector = PROPERTY_ENABLE;
 
 if (lsm6dso_pin_int1_route_set(®_ctx, &val) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 return LSM6DSO_OK;
}
 
 
bool LSM6DSO_Get_Step_Count(uint16_t *StepCount)
{
 if (lsm6dso_number_of_steps_get(®_ctx, (uint8_t *)StepCount) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 return LSM6DSO_OK;
}
 
 
bool LSM6DSO_Step_Counter_Reset()
{
 if (lsm6dso_steps_reset(®_ctx) != LSM6DSO_OK)
 {
 return LSM6DSO_ERROR;
 }
 
 return LSM6DSO_OK;
}

I can't get the pedometer to read the steps counts with this. Am I doing something wrong here?

I appreciate any help. Thank you!

    This topic has been closed for replies.

    2 replies

    JKAuthor
    Visitor II
    August 12, 2019

    I still can't get proper step counts from the step count register. It'll be great if someone can point out potential mistakes that I might have made. Thanks!

    Visitor II
    June 3, 2021

    any lock there? I am having the same issue