Skip to main content
Visitor II
January 2, 2018
Solved

Movement detection with LIS2DH

  • January 2, 2018
  • 14 replies
  • 5302 views
Posted on January 02, 2018 at 15:59

HI everybody,

I'm working with the LIS2DH for a motion sensor, and I'm using the embbeded functions of motion detection for drive the state machine of my MCU.

The operation is simple, but I have some trouble with the sensor.

I want to detect a movement if the duration is min 5sec, and I want to detect End of movement if the duration is 5sec.  

So I use the IA1 funtion with INT1 output for detection movement.

And IA2 function with INT2 output for End of movement detection.

Both INT output are OK with the detection, but I have non stable operation. Sometimes the INT1 toggles witout reason, sometime it's INT2.....

So if you run with the sensor in your hand, the INT1 output is high after 5sec. And if you put the sensor on the floor, the INT2 output is high after 5sec.

Sample is 1Hz ,Range is 2G.

High filter is set.

INT1_CFG is set to High level detection on X, Y , Z.  OR condition

INT2_CFG is set to Low level

detection on X, Y , Z.  OR condition

INT1_duration & INT2_duration are set to 0x05 in order to have 5sec of time detection.

INT1_threshold & INT2_threshold are set to 0x02, in order to have high sensivity.

Below the setting of the LIS2DH:

status_t LIS2DH_Init(void)

{

SPI1_Initialize();

CS_LIS1_SetHigh();

CS_LIS2_SetHigh();

LIS2DH_WriteReg(LIS2DH1,LIS3DH_CTRL_REG1,0x1F);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_CTRL_REG2,0x03);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_CTRL_REG3,0x40);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_CTRL_REG4,0x80);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_CTRL_REG5,0x00);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_CTRL_REG6,0x20);

//START detection

LIS2DH_WriteReg(LIS2DH1,LIS3DH_INT1_CFG,0x02);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_INT1_THS,0x02);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_INT1_DURATION,0x05);

//STOP detection

LIS2DH_WriteReg(LIS2DH1,LIS3DH_INT2_CFG,0x15);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_INT2_THS,0x02);

LIS2DH_WriteReg(LIS2DH1,LIS3DH_INT2_DURATION,0x05);

return MEMS_SUCCESS;

}

Could you please help to understant and solve this problème?

Thanks a lot for your support.

wilfried

    This topic has been closed for replies.
    Best answer by wilfried walezack
    Posted on February 27, 2018 at 11:20

    Hi Miroslav,

    I'm validated your first proposal. So I toggle the CS of the opposite sensor after a writing or a reading on the first sensor. Just a last question, I put a delay à 50us for the toggle of the CS.:

    CS_LIS2_SetLow();__delay_us(50);CS_LIS2_SetHigh();

    This delay is working on my board. Do you think it's a good delay for get a stable working ?

    Thanks a lot for your support.

    wilfried

    14 replies

    ST Employee
    January 4, 2018
    Posted on January 04, 2018 at 09:50

    First of all the value of INT1_CFG register is not correct, it should be 0x2A (not 0x02).

    I think using 1Hz is difficult because the high-pass filter has a decay and it will influence the timing.

    I would recommend you to try ODR 10Hz, adjust the Duration register accordingly and also increase the threshold little bit to increase noise immunity.

    Please see below my configuration, it seems to me it works well.

    0690X00000609CrQAI.png
    Visitor II
    April 12, 2019

    Hello,

    can I have the name of the software for the observation that you use please?

    Visitor II
    January 9, 2018
    Posted on January 09, 2018 at 18:52

    Dear Miroslave,

    Thanks for your feedback.

    For the first point, you're right, it's a mistake from my side, the real value is 0x2A.

    I have increased the detection threshold for interrupts 1 & 2, from 0x02 to 0x05, and the  operation is stable now.

    I need also support for the high pass filter, it's not clear for me. The goal of the high pass filter is to delete the continus value on the Z axe (-1g).

    If I walk with the sensor on my hand with the last setting, the values of the X,Y and Z axes will be not constant, the values will move from -500mg to 500mg, and the mean value is around 0g. So according to the sample frequency ODR, it's possible to have a low value, under the threshold INT1_THS. so if I put 0x32 for INT1_DURATION and only one point is under the threshold, what's about the INT1_output ? is it set or not set?

    Does  the high pass filter have action for the case of detection? What's will be the best filter mode for this situation?.

    Another question: I plan to buy the dev board 

    STEVAL-MKI109V2 with LIS2DH sensor

    in order to fully understand the features.

    Is it possible to record and export  curve and data point during long timing ( 15min)?

    Best regards

    and thanks for your help.

    wilfried

    ST Employee
    January 10, 2018
    Posted on January 10, 2018 at 10:51

    If even only one sample will be under the threshold the interrupt won't be triggered, basically in your case each sample bellow the threshold will reset the duration counter. I don't know what is your exact target, but usually these detections are not 100% accurate, the threshold is different for each activity like for example working on PC, walking, sitting in car, ... and it can vary person by person, also it depends on the position of the sensor (on wrist, in pocket, ...)

    STEVAL-MKI109V2 or  STEVAL-MKI109V3 cooperate with Unico application, which is able to record data and store them into text file. 15 minutes is not a problem.

    Visitor II
    January 10, 2018
    Posted on January 10, 2018 at 12:57

    Hi,

    My final application is a sensor for asset tracking ( truck and railway wagon), we plan to start the mass prod this year.

    The sensor have to detect beginning of movement with INT1 , and end movement with INT2.

    Beginning movement  must be detected from 10sec of vibration.

    End of movement must be detected from 120sec of no-vibration.

    The 2 INT functions are runnng in same time, so I have to put ODR to 1HZ in order to have a long time of stop detection (120sec).

    According to your last response, I have a doubt concerning the faisability of the movement detection.

    What do think about this kind of detection?

    Do I have to use a post treatment with my MCU for manage the detection.

    Regards.

    ST Employee
    January 10, 2018
    Posted on January 10, 2018 at 13:29

    Yes, I think using MCU for data processing is better approach which will be needed.

    With LIS2DE12 accelerometer the only parameter which you can change is threshold as the duration is given, which is not flexible.

    Good approach is to collect data in real situation (on track or wagon) and then evaluate them offline, you can the simulate your detection and see if the simple solution would work on not.

    Visitor II
    January 10, 2018
    Posted on January 10, 2018 at 13:33

    I'm using LIS2DH , not DE.

    ST Employee
    January 10, 2018
    Posted on January 10, 2018 at 16:09

    Sure, but the interrupt features are the same.

    Visitor II
    February 21, 2018
    Posted on February 21, 2018 at 19:53

    Hi Miroslav,

    My sensor is working fine now. Detection of activity/ no activity is ok with embedded functions.

    But I have a strange running on the SPI bus when I use 2 LIS2DH,

    When I connect and use only one LIS2DH, all are OK, but on my final board I have 2 LIS2DH connected to the same SPI bus. I drive the CS pin on each sensor for set parameters on the selected sensor.

    I can't write the value 0x32 into the register LIS3DH_INT1_DURATION(0x33), when I read after the write operation the value is 0xFF. I have no problem with the value 0x31 and 0x33, but only with the value 0x32.

    If I disconnect the 2nd LIS2DH, the writing operation is OK with the value 0x32.

    I checked my schematic (see below). All it's OK.

    Is very crazy, I havn't explanation. Do you I have an idea?

    thanks a lot for your support!

    wilfried

    0690X00000609lyQAA.png
    ST Employee
    February 21, 2018
    Posted on February 21, 2018 at 21:35

    I think, I have an explantion, the second sensor which has CS=1 is actually expecting I2C command and the 0x32 is write address of the sensor on I2C bus. So it interract with your SPI comunication. Can you please capture the comunication on the SPI bus by scope or logic analyzer? You should not create I2C start condition with the SPI communication.

    Visitor II
    February 23, 2018
    Posted on February 23, 2018 at 14:37

    HI,

    Please find bellow scope capture of the SPI communication

    Yellow curve -> clock

    blue curve -> MOSI

    1) Write 0x31 into 0x33 register (LIS3DH_INT1_DURATION) - > Write OK

    and Read 0x33 register -> Read OK

    2) Zoom on the Reading communication of the 0x33 register-> Read OK

    3) Write 0x32 into 0x33 register (LIS3DH_INT1_DURATION) - > Write OK

    and Read 0x33 register -> Read KO (you can see distorsion of the data ) 

    4) Zoom on the Reading communication of the 0x33 register-> Read KO

    On the picture 4, we can see that the LIS2DH pull down the bit7 of the data.

    SPI frequency is 2Mhz.

    I don't send START condition, just SPI communication to the LIS2DH sensor.

    Could you pls help me to solve this issue?

    Best regards

    wilfried

    0690X00000609kmQAA.png0690X00000609lBQAQ.png0690X00000609nQQAQ.png0690X00000609jFQAQ.png
    ST Employee
    February 23, 2018
    Posted on February 23, 2018 at 15:48

    When the second LIS2DH pull down the bit7 of the data, it actually does acknowledge of his address 0x31.

    Unfortunately I don't see the CS signal, but I suppose you don't reset and set CS signal between write and read operation and this cause the problem. Can you please check it?

    The SPI mode should be mode 3, but I suppose you are using this mode.

    Visitor II
    February 23, 2018
    Posted on February 23, 2018 at 16:18

    Yes, the SPI mode is set to Mode3.

    I tried with reset CS after writing, and also to held high the CS signal between writng and reading, but the result is the same. bit 7 are already pull down.

    see below

    if I put 100ms between the writing and readin, same results...

    0690X00000609jKQAQ.png0690X00000609lLQAQ.png
    ST Employee
    February 23, 2018
    Posted on February 23, 2018 at 16:34

    I though the CS driven from the same pin with an invertor.

    You should also toggle the CS signal on the second sensor, it should be break the unwanted I2C communication.

    Anyway it is strange the second sensor accept the communication as I2C, it meas there must be a start bit (SDA high to low with clock high), but it should not be there.

    Visitor II
    February 23, 2018
    Posted on February 23, 2018 at 17:38

    OK, 

    if I toogle the CS of the second LIS2DH,  the SPI communication is OK, I can now write the value 0x32 into the register 0x33, and also I can read it.

    Your 2nd proposal seems to be working. but I'm confuse with this solution. Normally with the SPI communication, we  do not have to toogle the CS of the other device.

    Could you give me more information about your first solution (inverter). It's not clear for me.

    Regards

    ST Employee
    February 24, 2018
    Posted on February 24, 2018 at 11:35

    'Your 2nd proposal seems to be working. but I'm confuse with this solution. Normally with the SPI communication, we  do not have to toogle the CS of the other device.' You are right, this is not common, but please be aware the the CS of the sneor is not only chip sellect but also switch between I2C and SPI bus. So the inactive sesnor with CS=1 is listening the communication and expect I2C data. When you send the data 0x32 it is actually I2C address of the sensor and the sensor make ACK which disturb your SPI coomunication. When you togggle the CS pint, you interrupt the I2C communication and the sensor cannot do the ACK.

    The solution with the invertor would be usable if you want ot distinguish between two sensor with only one MCU pin, so one CS pin would connected directly to MCU and second through invertor.

    wilfried walezackAuthorAnswer
    Visitor II
    February 27, 2018
    Posted on February 27, 2018 at 11:20

    Hi Miroslav,

    I'm validated your first proposal. So I toggle the CS of the opposite sensor after a writing or a reading on the first sensor. Just a last question, I put a delay à 50us for the toggle of the CS.:

    CS_LIS2_SetLow();__delay_us(50);CS_LIS2_SetHigh();

    This delay is working on my board. Do you think it's a good delay for get a stable working ?

    Thanks a lot for your support.

    wilfried