Specifications
- Model: MPU-6050.
- Supply voltage: 3.3–5.0 V.
- Operating current: less than 5 mA.
- Accelerometer ranges: ±2g, ±4g, ±8g, ±16g.
- Gyroscope angular-velocity ranges: ±250, 500, 1000, 2000°/s.
- Number of axes: 3.
- Connection interface: 4-wire I2C.
Overview
The combined gyroscope and accelerometer module measures a robot’s orientation in space as rotation around three axes: longitudinal X (roll), transverse Y (pitch), and vertical Z (yaw).
The sensor board marks the orientation of axes X and Y relative to its body. The rotation arrow points toward increasing angles. Axis Z points vertically upward from the component side of the board. Rotation angles increase clockwise around an axis.
Using the sensor
Connecting
The position-sensor module uses I2C and connects to either of the two I2C ports on the Trackduino controller.
| Sensor | Controller (I2C) |
|---|---|
| VCC | 5V |
| GND | GND |
| SCL | SCL |
| SDA | SDA |
Programming
The Position block is in the Sensors block group.
In the text environment, include the accel_gyro.h library and call the calibration function setupAccel() in setup():
#include <accel_gyro.h> // include the library
void setup()
{
setupAccel(); // calibrate; keep the sensor stationary on a level surface
}
void loop()
{
// your code
}
The visual block generates these functions automatically.
| Block and generated function | Description |
|---|---|
![]() float readAccelAngle(char axis, int mode) | Returns a floating-point value for the rotation angle on the selected axis.char axis: 'x', 'y', or 'z'.int mode: calculation mode: 0 or RAW_GYRO (gyroscope only), 1 or RAW_ACCEL (accelerometer only), 2 or FILTERED (averaged). |
Operating modes
The sensor combines a gyroscope and accelerometer. It can report readings from either sensor alone or combine them to average and stabilize the output.
Accelerometer-only mode: Angles are calculated from the gravity vector.
- Advantage: error does not accumulate over time.
- Limitation: readings are unstable and sensitive to vibration and lateral acceleration.
The gravity vector cannot determine rotation around vertical axis Z. Therefore, Z readings in accelerometer-only mode are always 0. Rotation around Z can be measured only in gyroscope-only or averaged mode.
Gyroscope-only mode: Angles are calculated by measuring angular velocity and integrating it over time.
- Advantage: readings are stable and less affected by random acceleration and vibration.
- Limitation: error accumulates over time. Readings depend on the interval between measurements; more frequent measurements improve accuracy.
Averaged mode (recommended for most cases): Software combines the two sensors to compensate for their individual limitations. The accelerometer readings, which do not accumulate error over time, are filtered with gyroscope readings to ignore jumps caused by random acceleration and vibration.
- Advantage: readings are stable and less affected by random acceleration and vibration.
- Limitation: error still accumulates on Z because that axis uses only the gyroscope.
Example project
Print readings for all three axes to the serial port
Generated code:
#include <accel_gyro.h>
void setup()
{
Serial.begin(115200);
setupAccel();
}
void loop()
{
Serial.print("X:");
Serial.print(readAccelAngle('x',FILTERED));
Serial.println();
Serial.print("Y:");
Serial.print(readAccelAngle('y',FILTERED));
Serial.println();
Serial.print("Z:");
Serial.print(readAccelAngle('z',FILTERED));
Serial.println();
delay( 200 );
}

