Accelerometer/gyroscope – MPU6050

This peripheral is a good example of a chip that gives us good resolution for orientation and acceleration at a fair price. The breakout circuit has many pins in addition to the ones we are used to. It has extra pins for SDA and SCL that can be used to interface other I2C modules with the MPU6050, a chip selection one (AD0) which can be used to modify the I2C address, and an interrupt one which can be used to read from the component on demand, similarly to what we saw with the GPIO expander.

For this example we will only use the basic four: Vcc, Ground, SCL, and SDA.

To get the driver, we need to add it to the module dependencies:

dependencies {
[...]
implementation 'com.plattysoft.things:mpu6050:+'
}

The driver itself is quite straightforward. I modified the original version slightly to adhere to the standard methods for opening the component we have been using. A simple usage is like this:

val gyroscope = Mpu6050.open()
Log.d(TAG, "Accel: x:${gyroscope.accelX} y:${gyroscope.accelY} z:${gyroscope.accelZ}")
Log.d(TAG, "Gyro: x:${gyroscope.gyroX} y:${gyroscope.gyroY} z:${gyroscope.gyroZ}")
gyroscope.close()

The driver has the values for acceleration as accelX, Y, and Z and, similarly, gyroscope orientation is presented as gyroX, Y, and Z.

Kotlin allows us to use getters as just variable reads. Keep in mind that in most drivers those getters are synchronous blocking operations.

Note that, one more time, these are getters that issue a request to the component for reading and then receive a response. They are synchronous and can take a bit of time to execute, but Kotlin allows us to express them just as variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.4.239