Vibration Monitoring using LIS3DH sensor on nRF52840

Hi Nordic team,

I hope this message finds you well. I am currently working on developing applications for custom boards utilizing the nRF Connect SDK v2.4.0, with a particular focus on the nRF52840 microcontroller.

In our ongoing project, we have a requirement to implement a feature involving Vibration Monitoring, which triggers whenever a threshold level is crossed. I have successfully interfaced the sensor and am able to obtain X, Y, and Z values using the LIS2DH drivers. However, I am encountering difficulties in implementing the vibration detection using the interrupt pin. Although I have specified the interrupt pin in the .overlay file, I am not receiving the expected output.

&i2c0 {
    lis2dh@19 {
        compatible = "st,lis2dh";
        reg = <0x19>;
		status = "okay";		
        label = "LIS3DH";
		irq-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>, <&gpio0 1 GPIO_ACTIVE_HIGH>;
		disconnect-sdo-sa0-pull-up;
    };
};

Could you please provide some sample code for reference regarding Vibration Monitoring?

Additionally, I would like to inquire if LIS3DH drivers are available, as it would be beneficial for our project.

Best regards,


Anmol

  • Hi,

    Due to the Easter holidays in Norway, we are less staffed than usual. You can expect an answer sometime next week,

    Sorry for the inconvenience and thanks for your patience. 

    Regards, 
    Amanda

  • Hello,

    Although I have specified the interrupt pin in the .overlay file, I am not receiving the expected output.

    can you please provide more information about the problem? Maybe some logs and such.

  • Hello Hakon,

    I'm using LIS3DH sensor and having trouble understanding how to utilize interrupts that trigger whenever a vibration threshold level, for example, 5m/s^2, is exceeded. Could you please provide some sample code for reference regarding Vibration Monitoring?

    Regards,

    Anmol

  • Anmol Maske said:
    Could you please provide some sample code for reference regarding Vibration Monitoring?

    I think the best example would be the asset tracker v2, but it's not quite for vibration monitoring. You can still look at the accelerometer configuration to check how it's done.

  • Hi,

    I have solved the problem. Directly used the interrupt trigger function from lis2dh sample and threshold attribute in sensor_attr_set() function with some manual computations. This is giving me readings whenever threshold level is crossed.

    /*Enable trigger handler for LIS*/
    static void lis_trigger_handler(const struct device *dev, const struct sensor_trigger *trigger)
    {
    	int64_t uptime = k_uptime_get();
        
        if(uptime >= 750){
    
    		LOG_INF("Vibration is beyond the 750ms\n");
    		vibration_detected = 1;
    		read_sound();
    
    	}
    	else
    	{
    		vibration_detected = 0;
    	}
    
    }
    
    /*Threshold for Vibration Monitering*/
    void configure_lis_vibration_threshold(const struct device *lis_dev) {
    
        int ret;
        struct sensor_value vib_val;
    	
        vib_val.val1 = lowG_vib_threshold; // Adjust this threshold value as needed
        vib_val.val2 = 0; //(int32_t) SENSOR_G *1.5
       
           struct sensor_trigger lis_trig = {
            .type = SENSOR_TRIG_DATA_READY,
            .chan = SENSOR_CHAN_ACCEL_XYZ,
        };
        
        // Set sensor attribute for upper threshold
        ret=	sensor_attr_set(lis_dev, SENSOR_CHAN_ACCEL_X, SENSOR_ATTR_UPPER_THRESH,&vib_val);
        if (ret) {
            LOG_ERR("Failed to set sensor attribute: %d", ret);
            return;
        }
    	
        // Register trigger handler
        ret = sensor_trigger_set(lis_dev, &lis_trig, lis_trigger_handler);
        if (ret) {
            LOG_ERR("Failed to set sensor trigger: %d", ret);
            return;
        }
    }
    
    /*Read Sound Values with/without Vibration*/
    void read_sound(){
        
    	configure_lis_vibration_threshold(lis2dux12);
        wakeup_now();
    
    	LOG_INF("Sound reading is started");
    	LOG_INF("Vibration detected flag = %d", vibration_detected);
    	if(vibration_detected) 
    	{
    		Motion_Started = 1;
    		// Add sleep mode
    		k_sleep(K_MSEC((1000 * motion_sound_rate) - 1000));  
    		vibration_detected = 0;
    	}
    	else
    	{
    		Motion_Started = 0;
    	}
    
    	sleep_now();
    }
    
    
    

Related