Tutorial: Modulo MPU6050 GY-521 Acelerometro y giroscopio con Arduino UNO

En este nuevo tutorial queremos hablar del módulo de 3 ejes, acelerómetro y giroscopio MPU6050 o  GY-521.

Este módulo está basado en el sensor MPU6050 y contiene todo lo necesario para medir movimiento en 6 grados de libertad (6 Dof), combinando un giroscopio de 3 ejes y un acelerómetro de 3 ejes en un mismo chip. Integra un DMP (Procesador digital de movimiento) capaz de realizar complejos algoritmos de captura de movimiento de 9 ejes; este modulo pertenece a los llamados IMUs que son dispositivos capaz de medir la fuerza y la velocidad, pueden llegar a medir ángulos, pero de una manera indirecta aplicando algunos cálculos.

Se comunica a través de una interfaz I2C y posee una librería muy completa para su uso inmediato. Este sensor incorpora un regulador de tensión a 3.3V y resistencias pull-up para su uso directo por I2C. Para una captura precisa de movimiento rápido y lento, posee un rango de escala programable de 250,500,1000,2000 grados/seg para el giroscopio y de 2g/4g/8g/16g para el acelerómetro.

Características del MPU6050

Voltaje de operación: 3V/3.3V~5V DC
Regulador de voltaje en placa
Grados de libertad (DoF): 6
Rango Acelerómetro: 2g/4g/8g/16g
Rango Giroscopio: 250Grad/Seg, 500Grad/Seg, 1000Grad/Seg, 2000Grad/Seg
Sensibilidad Giroscopio: 131 LSBs/dps
Interfaz: I2C
Conversor AD: 16 Bits (salida digital)
Tamaño: 2.0cm x 1.6cm x 0.3cm

↑↑Módulo MPU6050↑↑

Este ejemplo es básico dentro de lo que son los IMUs, a continuación el código, el cual arroja 3 datos de acelerómetro, 3 de giroscopio y 1 de temperatura por el puerto serial.

[code]

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de
#include «Wire.h» // This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, «%6d», i);
return tmp_str;
}
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
}
void loop() {
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers

// «Wire.read()<<8 | Wire.read();» means two registers are read and stored in the same variable
accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

// print out data
Serial.print(«aX = «); Serial.print(convert_int16_to_str(accelerometer_x));
Serial.print(» | aY = «); Serial.print(convert_int16_to_str(accelerometer_y));
Serial.print(» | aZ = «); Serial.print(convert_int16_to_str(accelerometer_z));
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
Serial.print(» | tmp = «); Serial.print(temperature/340.00+36.53);
Serial.print(» | gX = «); Serial.print(convert_int16_to_str(gyro_x));
Serial.print(» | gY = «); Serial.print(convert_int16_to_str(gyro_y));
Serial.print(» | gZ = «); Serial.print(convert_int16_to_str(gyro_z));
Serial.println();

// delay
delay(3000);
}

[/code]

Referencia:

Michael Schoeffler

GY-521 MPU-6050 módulo de 3 ejes acelerómetro Giroscopio Módulo Para Arduino