During last some days, I was working on creating a sensor library for MPU6050. In this blog I want to explain what I have done so far with this sensor.
The MPU6050 comes with onchip Accelerometer and Gyroscope. The sensor can measure acceleration range from +/- 2g, 4g, 8g, 16g.
And it can measure Gyroscope angular velocity of +/- 250, 500, 1000, 2000 LSB/degree/s.
At present, the library can measure x,y,z acceleration. But my plan is to create a fall detector with interrupt capability. Perhaps the Gyro data can also be used for this purpose.
Update
The last time I wrote the blog, I had no idea about interrupts for MPU6050 and how they work. Though, there are some documentation issues, some people have reverse engineered the I2C signals to find out which registers are for Fall detection
and motion detection.
The register 0x38(Interrupt Enable) has two separate bits for enabling/disabling the fall detection and motion detection interrupts on pin INT of the sensor.
//interrupt Registers #define INT_PIN_CONFIG 0x37 #define INT_ENABLE 0x38 #define INT_STATUS 0x3A #define MOT_THR 0x1F #define MOT_DUR 0x20 #define FF_THR 0x1D #define FF_DUR 0x1E
Hence, the following code will enable the motion detection interrupt on pin INT.
imu.writebyte(INT_PIN_CONFIG,0x20); imu.writebyte(INT_ENABLE, 0x40); imu.writebyte(MOT_THR, 0x01); imu.writebyte(MOT_DUR, 0x01);
The motion detection works for now, the next part is to check the fall detection and how accurate it is..
Update
So now the time is for the fall detection and playing with it tuning some values which could rightly detect the fall based on accelerometer values.
The datasheet or the product specification document mentions this in somewhat detail but many thing are there which is yet not clear.
The first thing is what is the fall detection threshold and it's range? The document says that a threshold in a range of 1mg can be set using the register FF_THS.
So I thought that setting it to the value 0xFF would be arround 250mg right?
The document further explains that if all there values of acceleration axes from (X, Y, Z) are less than this value than a fall detection interrupt will be triggered.
Though setting 0xFF was to much sensitive. If I would just touch the sensor then also interrupt will be triggered.
Then I tried the value 0x00 to just see what corresponds to the least value of FF_THS register? The interrupt trigger should be the case only if sensor moves from with high acceleration.
This was the case indeed. The interrupt never triggered with me trying to drop sensor from some height.
Finally, for fall detection it was required that I set a value of this register such that if there is an interrupt if the sensor is dropped from an height from about ~5/6 feets in different orientations.
Of course, the sensor has a filter to nullify the effects of gravity. This would help create better fall detection and identify the right movement.
The value one might want to set for the detection could be around 0xD0 etc.?? For my case it seems working but I still need to do some experiments.
Update
There were a few challenges to create this library but there are obviously many improvements and especially expansion to this library I am looking for.
The next things I am looking to do is following
- Improve the overall design of the library
- Create a fusion algorithm to get right angle and speed/tracking information
- create self-test functions and also add external sensors to fetch I2C data from for examples mangnetometers.
I will keep working on this library and post updates to this blog. If anyone want to join or have some feedback you can contact me!
Update
The final thing for now for this code is to create a library structure as recommended by Arduino hence it can be used by others via .zip download.
For that one needs to put the code in the standard format. The format contains the following structure
- /src
- MPU6050.cpp
- MPU6050.h
- MPU6050reg.h
- /examples
- Example name
- example.ino
- Example name
- /docs
- library.properties
- /extras
- README.md
Out of these files the .properties file is important as it is used by the build systems to generate and compile the example files to create binary.
The things did not work for me in the first go because I was not creating the library inside a library folder of arduino installation.
But anyways the things should work for you if you download the library and would like to get the sensor data!!
Have fun Just like me!!
Update
In the meantime, I created a PCB for MPU6050 and here are the images. There is one 4-pin connector for I2C interface and there are other pin headers for signals such as Auxilary I2C, frame synchronization, Interrupt and external clock input.