Skip to content

MPU6050 Six-Axis Gyroscope ​

Overview ​

MPU6050 is a six-axis motion sensor (3-axis accelerometer + 3-axis gyroscope) that communicates via I2C bus. It can measure acceleration, angular velocity, and calculate tilt angle. Suitable for attitude detection, motion tracking, tilt alarm, and other scenarios.

Firmware Requirement: Requires FASTBEE_ENABLE_I2C_SENSORS=1 and active i2cSensors.

Supported Peripheral Types ​

Typetype ValueDescription
SENSOR38I2C sensor (MPU6050)

Hardware Wiring ​

MPU6050 PinESP32 GPIODescription
VCC3.3VPower supply
GNDGNDGround
SDAGPIO 21I2C data line
SCLGPIO 22I2C clock line
INTGPIO (optional)Interrupt output (data ready)

MPU6050 supports 3.3V and 5V power supply; ESP32 recommends using 3.3V.

Configuration ​

Before saving MPU6050 configuration, verify I2C address, collection fields, and installation orientation.

Step 1: Navigate to Peripheral Management Page ​

  1. Open a browser and navigate to the ESP32 IP address
  2. After logging in, click Peripheral Configuration in the left menu

Step 2: Add MPU6050 Peripheral ​

  1. Click the Add Peripheral button

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDmpu6050_1Unique identifier
    NameMPU6050 GyroscopeDisplay name
    Peripheral TypeGeneric Sensor (type: 38)I2C sensor
    SDA Pin21I2C data pin
    SCL Pin22I2C clock pin
    Driver NameMPU6050Fixed value
    I2C Address0x680x68 or 0x69
    Sampling Interval10001 second (≥500ms)
    Accel Range2±2g
    Gyro Range250±250°/s
  3. Click Save

Step 3: Verify Configuration ​

  1. Find the newly added peripheral in the peripheral list
  2. Click the Enable toggle
  3. Wait 1 second and check acceleration, angular velocity, and tilt angle data

Important: Confirm i2cSensors is active before adding the peripheral.


Method 2: JSON Configuration File Import ​

Add the following configuration to the peripherals array in data/config/peripherals.json:

json
{
  "id": "mpu6050_1",
  "name": "MPU6050 Gyroscope",
  "type": 38,
  "enabled": false,
  "pins": [21, 22],
  "params": {
    "driver": "MPU6050",
    "i2cAddress": "0x68",
    "interval": 1000,
    "accelRange": 2,
    "gyroRange": 250
  }
}

Parameter Description ​

ParameterDescription
driverDriver name, fixed to "MPU6050"
i2cAddressI2C address, default 0x68 (AD0 to GND); 0x69 when AD0 to VCC
intervalSampling interval (ms), recommend ≥ 500
accelRangeAccelerometer range: 2/4/8/16 (unit: g)
gyroRangeGyroscope range: 250/500/1000/2000 (unit: °/s)

pins[0] = SDA pin, pins[1] = SCL pin

Data Report Format ​

Sensor data is reported via MQTT:

json
[
  {"id": "mpu6050_1", "value": "ax:0.12,ay:-0.03,az:9.78,gx:1.2,gy:-0.5,gz:0.1,tilt:5.3"}
]
FieldDescriptionUnit
ax/ay/azThree-axis accelerationm/s²
gx/gy/gzThree-axis angular velocity°/s
tiltCombined tilt angle°

Driver Registration Mechanism ​

MPU6050 uses the ISensorDriver + DriverRegistry mechanism for registration:

  • Enabled at compile time via FASTBEE_ENABLE_I2C_SENSORS macro
  • Driver automatically registers to the global registry
  • The driver field in configuration is used to match the specific driver implementation

Peripheral Execution Integration ​

Web Interface Configuration Steps ​

Create Tilt Detection Alarm Rule

  1. Click Peripheral Execution in the left menu
  2. Click the Add Rule button
  3. Configure poll trigger:
    • Trigger Type: Poll Trigger
    • Data Source: mpu6050_1
    • Data Field: tilt (tilt angle)
    • Comparison: Greater Than
    • Comparison Value: 30 (degrees)
  4. Add actions:
    • Action 1: Buzzer alarm
      • Action Type: High Level
      • Target Peripheral: buzzer1
    • Action 2: Trigger device event
      • Action Type: Trigger Event
      • Event ID: tilt_alarm
  5. Click Save

💡 Tip: Can share I2C bus with BMP280 (different addresses)


JSON Configuration Example ​

Tilt Detection Alarm ​

json
{
  "name": "Tilt Alarm",
  "enabled": true,
  "triggers": [
    {
      "triggerType": 5,
      "triggerPeriphId": "mpu6050_1",
      "intervalSec": 10,
      "operatorType": 2,
      "compareValue": "30"
    }
  ],
  "actions": [
    { "actionType": 0, "targetPeriphId": "buzzer1" },
    { "actionType": 21, "targetPeriphId": "tilt_alarm" }
  ]
}

operatorType: 2 means “greater than”; this rule raises an alarm when the tilt exceeds 30°.

Attitude Data Display on OLED ​

json
{
  "name": "Display Attitude",
  "enabled": true,
  "triggers": [
    { "triggerType": 1, "timerMode": 0, "intervalSec": 5 }
  ],
  "actions": [
    { "actionType": 19, "targetPeriphId": "mpu6050_1" },
    { "actionType": 27, "targetPeriphId": "oled1", "actionValue": "Tilt:${mpu6050_1.tilt}deg\nAx:${mpu6050_1.ax}" }
  ]
}

Notes ​

  1. Firmware State: Confirm i2cSensors is compiled and active
  2. Installation Orientation: Sensor installation orientation affects axis definition; calibrate based on actual installation
  3. Offset Calibration: Recommend performing offset calibration on first use; place device horizontally at rest and read offset values
  4. Vibration Interference: Environmental vibration affects reading accuracy; recommend adding vibration dampening pad
  5. I2C Sharing: Can share the same I2C bus with BMP280 (different addresses), saving pin resources
  6. Sampling Rate: High-speed sampling (< 100ms) increases CPU load; recommend 500-2000ms for normal applications

FastBee | fastbee.cn