MPU6050 accelerometer and gyro
The Invensense MPU6050 is a combined digital accelerometer and gyro. This module enables I2C communication with the chip to easily get acceleration and rotation data. Use the MPU6050 (About Modules) module for it.
You can wire this up as follows:
Device Pin | Espruino |
---|---|
1 (GND) | GND |
2 (VCC) | 3.3 |
3 (SDA) | B7 |
4 (SCL) | B6 |
Basic usage:
I2C1.setup({scl:B6,sda:B7});
var mpu = require("MPU6050").connect(I2C1);
mpu.getAcceleration(); // returns an [x,y,z] array with raw accl. data
mpu.getGravity(); // returns acceleration array in G's
mpu.getRotation(); // returns an [x,y,z] array with raw gyro data
mpu.getDegreesPerSecond(); // returns gyro array in degrees/s
Advanced usage:
One of the really nice and powerful features of this module is its integrated Digital Motion Processor, than can do accelerometres and gyros data integration as well as low pass filtering. For that you'll need the MPU6050_DMP module.
I2C1.setup({scl:B6, sda:B7, bitrate:100000});
const MPU = require("MPU6050").connect(I2C1);
// 2nd parameter is the fifoRate. The DMP output frequency = 200Hz / (1 + fifoRate)
const DMP = require("MPU6050_DMP").create(MPU, 3);
function pidLoop(args) {
var data = DMP.getData();
if(data !== undefined) console.log(DMP.getYawPitchRoll(data));
}
setWatch(pidLoop, B5, { repeat:true, edge:'rising' });
It's not mandatory, but the above example makes use of the interrupt feature, where it's the module that tells you when the data is ready and you can go ahead and read it.
1 extra wire is necessary:
Device Pin | Espruino |
---|---|
5 (INT) | B5 |
For use of more than one chip per I2C bus, the address can be selected with an additional parameter to the connect
call:
var mpu = require("MPU6050").connect(I2C1, false); // AD0 pin wired to GND
var mpu = require("MPU6050").connect(I2C1, true); // AD0 pin wired to Vcc
Buying
MPU6050 is a common and inexpensive part, and can be purchased from many places:
This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.