Arduino library · Sensors

GY521: an example that runs

Yes. You can use the GY521 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Accelerometer and gyroscope, MPU6050). The example below, "Tilt angles from a GY-521", runs on the Arduino Uno R3.

Built in on Mokxi: #include <GY521.h> and it builds, with nothing to install. Free, and no account needed.

What GY521 does

The MPU-6050 on a GY-521 board: acceleration in g, rotation in degrees a second, temperature, the tilt angles from the accelerometer, and pitch, roll and yaw from the integrated gyroscope. That is what GY521, by Rob Tillaart, covers. The sketch below uses it on the Arduino Uno: “Tilt angles from a GY-521”.

Rob Tillaart's GY521 library (0.6.x), for the MPU-6050 on a GY-521 board: acceleration, rotation, temperature and the angles worked out from them.

Use the mpu6050 part on the board's SDA and SCL. The constructor's address defaults to 0x69 as upstream's does; a GY-521 with AD0 low (the part's default) is at 0x68.

The calls follow release 0.6.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameGY521
AuthorRob Tillaart
Include#include <GY521.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAccelerometer and gyroscope, MPU6050

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <GY521.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "GY521" by Rob Tillaart from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add GY521 to lib_deps in PlatformIO. Mokxi follows the calls of release 0.6.2, so a sketch written here builds there too.

What to know in the simulator: The constructor defaults to address 0x69, as upstream; the part answers at 0x68 with AD0 low.

The calls you will use

In the order a sketch meets them.

GY521 sensor(0x68)

Makes sensor, the GY521 object the calls below are made on.

sensor.setAccelSensitivity(2)

Sets the accel sensitivity to the value you pass in.

sensor.setGyroSensitivity(1)

Sets the gyro sensitivity to the value you pass in.

sensor.setThrottle()

Sets the throttle to the value you pass in. Arguments: throttle.

sensor.read()

Returns the value as a whole number.

sensor.getAngleX()

Returns AngleX as a decimal number.

sensor.getAngleY()

Returns AngleY as a decimal number.

sensor.getAngleZ()

Returns AngleZ as a decimal number.

sensor.getTemperature()

Returns the temperature as a decimal number.

Example: Tilt angles from a GY-521

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "GY521.h"

GY521 sensor(0x68);

void setup() {
  Serial.begin(115200);
  Serial.print("GY521_LIB_VERSION: ");
  Serial.println(GY521_LIB_VERSION);
  Wire.begin();
  delay(100);
  while (sensor.wakeup() == false) {
    Serial.print(millis());
    Serial.println("	Could not connect to GY521: please check the GY521 address (0x68/0x69)");
    delay(1000);
  }
  sensor.setAccelSensitivity(2);  // 8g
  sensor.setGyroSensitivity(1);   // 500 degrees/s
  sensor.setThrottle();
  Serial.println("start...");
}

void loop() {
  sensor.read();
  Serial.print(sensor.getAngleX(), 1);
  Serial.print('	');
  Serial.print(sensor.getAngleY(), 1);
  Serial.print('	');
  Serial.print(sensor.getAngleZ(), 1);
  Serial.print('	');
  Serial.println(sensor.getTemperature(), 1);
  delay(200);
}

Wiring

  • Arduino Uno R3 A4 to Accelerometer and gyroscope, MPU6050 SDA
  • Arduino Uno R3 A5 to Accelerometer and gyroscope, MPU6050 SCL
  • Arduino Uno R3 5V to Accelerometer and gyroscope, MPU6050 VCC
  • Arduino Uno R3 GND to Accelerometer and gyroscope, MPU6050 GND

Open it in the editor, wired and ready to run

Try GY521 without installing anything

Open the example, change the sketch, and keep your version in a free account.