Accelerometer / Gyro Tutorial


The access of the MPU6050 accelerometer and gyro can be done using the iio dev nodes that are created by the driver.

Here we will show you a simple way to extract data from the device node.

To discover the iio device you need to first list the directory contents of all iio nodes:

root@beaglebone:~# ls -l /sys/bus/iio/devices/
 total 0
 lrwxrwxrwx 1 root root 0 Dec 8 18:23 iio:device0 -> ../../../devices/platform/ocp/44e0b000.i2c/i2c-0/0-0068/iio:device0
 lrwxrwxrwx 1 root root 0 Dec 8 18:23 iio:device1 -> ../../../devices/platform/ocp/44e0b000.i2c/i2c-0/0-005c/iio:device1
 lrwxrwxrwx 1 root root 0 Dec 8 18:23 trigger0 -> ../../../devices/platform/ocp/44e0b000.i2c/i2c-0/0-0068/trigger0
 lrwxrwxrwx 1 root root 0 Dec 8 18:23 trigger1 -> ../../../devices/platform/ocp/44e0b000.i2c/i2c-0/0-005c/trigger1

As you can see we have 2 iio devices 0 and 1.  Inspect the contents of the folders to see what the devices are.  e.g.:

root@beaglebone:~# ls -l /sys/bus/iio/devices/iio\:device0/

For the MPU6050 accelerometer and gyro we need the following contents :

drwxr-xr-x 2 root root 0 Dec 8 18:24 buffer
-r--r--r-- 1 root root 4096 Dec 8 18:24 dev
-r--r--r-- 1 root root 4096 Dec 8 18:24 in_accel_matrix
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_scale
-r--r--r-- 1 root root 4096 Dec 8 18:24 in_accel_scale_available
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_x_calibbias
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_x_raw
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_y_calibbias
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_y_raw
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_z_calibbias
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_accel_z_raw
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_scale
-r--r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_scale_available
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_x_calibbias
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_x_raw
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_y_calibbias
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_y_raw
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_z_calibbias
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_anglvel_z_raw
-r--r--r-- 1 root root 4096 Dec 8 18:24 in_gyro_matrix
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_temp_offset
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_temp_raw
-rw-r--r-- 1 root root 4096 Dec 8 18:24 in_temp_scale
-r--r--r-- 1 root root 4096 Dec 8 18:24 name
lrwxrwxrwx 1 root root 0 Dec 8 18:24 of_node -> ../../../../../../../firmware/devicetree/base/ocp/i2c@44e0b000/mpu6050@68
drwxr-xr-x 2 root root 0 Dec 8 18:24 power
-rw-r--r-- 1 root root 4096 Dec 8 18:24 sampling_frequency
-r--r--r-- 1 root root 4096 Dec 8 18:24 sampling_frequency_available
drwxr-xr-x 2 root root 0 Dec 8 18:24 scan_elements
lrwxrwxrwx 1 root root 0 Dec 8 18:24 subsystem -> ../../../../../../../bus/iio
drwxr-xr-x 2 root root 0 Dec 8 18:24 trigger
-rw-r--r-- 1 root root 4096 Dec 8 18:23 uevent

Check that we have the correct device by querying the name:

root@beaglebone:~# cat /sys/bus/iio/devices/iio\:device0/name
mpu6050

Acceleration (g)

If we need to know the current axis acceleration we can simply cat the dev node to extract the reading:

root@beaglebone:~# cat /sys/bus/iio/devices/iio\:device0/in_accel_x_raw
1940
root@beaglebone:~# cat /sys/bus/iio/devices/iio\:device0/in_accel_scale
0.000598

Reading the datasheet for the scaling it is the following calculation:

in_accel_x_raw * in_accel_scale = g

1940 * 0.000598 = 1.16012g

You can use the same process for x, y & z

 

Gyro (radians or degrees)

If we need to know the current axis angle we can simply cat the dev node to extract the reading:

root@beaglebone:~# cat /sys/bus/iio/devices/iio\:device0/in_anglvel_x_raw
-18
root@beaglebone:~# cat /sys/bus/iio/devices/iio\:device0/in_anglvel_scale
0.001064724

Reading the datasheet for the scaling it is the following calculation:

in_anglvel_x_raw * in_anglvel_scale = rad

-18 * 0.001064724 = -0.019165032 rad

In order to convert to degrees you need to multiply the radians by 57.29577951307855 :

-0.019165032 * 57.29577951307855 = -1.098075447833095 deg

You can use the same process for x, y & z

Remember that the reading you get from the gyro is the rate of change not absolute, so you need to accumulate fast readings if you want to get the absolute value.