BeagleBone® Enhanced Developer Tutorial

How to set up the BBE Accelerometer and Gyro

Simple Methods of Extracting Data from the Device Node of the MPU-6050 Six-Axis (Gyro + Accelerometer) MEMS MotionTracking™ Device

Accessing the MPU-6050 accelerometer and gyro is performed using the iio dev nodes that are created by the driver. Here we will show you two simple ways to extract data from the device node using either a BASH script or a C-application.

BASH Script Method

How to set up the Accelerometer and Gyro of your BeagleBone Enhanced

  1. First list the directory contents of all iio nodes

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

    [email protected]:~# 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.:

    [email protected]:~# ls -l /sys/bus/iio/devices/iio\:device0/

  2. For the MPU-6050 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/[email protected]/[email protected]
    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

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

    [email protected]:~# cat /sys/bus/iio/devices/iio\:device0/name mpu6050

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

    [email protected]:~# cat /sys/bus/iio/devices/iio\:device0/in_accel_x_raw 1940
    [email protected]:~# 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

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

    [email protected]:~# cat /sys/bus/iio/devices/iio\:device0/in_anglvel_x_raw -18
    [email protected]:~# 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.

C-Application Method

You can also extract the relevant information from the iio nodes via software.

Download the source code located on github to the target BBE (best to place it into a folder where you can build it)
https://github.com/sancloud/BBE_Sensors/tree/master/sc_mpu6050

In the folder where you can see the Makefile type make.  This will generate the mpu_reader test application.

Either copy to the bin folder of your choice or use the make install command to place into /usr/bin.

You can then execute the application to test the output of the accel/gyro.  You should get a similar output to the following:

chip_name=MPU6050 

gz   gx gy fgz fgx fgy az ax ay faz fax fay
00026 -0039 00014 1.59 -2.38 0.85 13052 03602 -32768 7.81 2.15 -19.60
00025 -0039 00013 1.53 -2.38 0.79 13104 03632 -32768 7.84 2.17 -19.60
00025 -0038 00013 1.53 -2.32 0.79 13074 03602 -32768 7.82 2.15 -19.60
00026 -0039 00013 1.59 -2.38 0.79 13096 03618 -32768 7.83 2.16 -19.60
00026 -0039 00014 1.59 -2.38 0.85 13094 03580 -32768 7.83 2.14 -19.60
00025 -0038 00013 1.53 -2.32 0.79 13086 03618 -32768 7.83 2.16 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13058 03612 -32768 7.81 2.16 -19.60
00026 -0037 00013 1.59 -2.26 0.79 13050 03630 -32768 7.80 2.17 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13116 03622 -32768 7.84 2.17 -19.60
00025 -0039 00013 1.53 -2.38 0.79 13060 03590 -32768 7.81 2.15 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13108 03588 -32768 7.84 2.15 -19.60
00027 -0038 00013 1.65 -2.32 0.79 13056 03584 -32768 7.81 2.14 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13034 03592 -32768 7.79 2.15 -19.60
00026 -0038 00014 1.59 -2.32 0.85 13070 03600 -32768 7.82 2.15 -19.60
00026 -0039 00013 1.59 -2.38 0.79 13056 03576 -32768 7.81 2.14 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13094 03622 -32768 7.83 2.17 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13090 03588 -32768 7.83 2.15 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13072 03576 -32768 7.82 2.14 -19.60
00026 -0038 00014 1.59 -2.32 0.85 13090 03578 -32768 7.83 2.14 -19.60
00026 -0038 00013 1.59 -2.32 0.79 13084 03644 -32768 7.82 2.18 -19.60


The application can be used as a base to allow you to expand and grow into your own application.