# Example 41: MPU6050 Gyroscope

## Experiment Overview

MPU6050 is a 6-axis I2C motion sensor. FastBee includes the driver in the release baseline.

> **Note**: This feature requires `FASTBEE_ENABLE_I2C_SENSORS=1` and active `i2cSensors`.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| SDA | GPIO21 | MPU6050 SDA |
| SCL | GPIO22 | MPU6050 SCL |

> MPU6050 I2C address: AD0 to GND = 0x68, AD0 to VCC = 0x69.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "mpu6050_01",
      "name": "MPU6050 Gyroscope",
      "type": 38,
      "enabled": false,
      "pins": [21, 22],
      "params": {
        "category": "mpu6050",
        "i2cAddress": "0x68"
      }
    }
  ]
}
```

### Data Channels

| Channel | Field Name | Unit | Description |
|---------|-----------|------|-------------|
| 0 | accelX | m/s² | X-axis acceleration |
| 1 | accelY | m/s² | Y-axis acceleration |
| 2 | accelZ | m/s² | Z-axis acceleration |
| 3 | temperature | °C | Chip temperature |

## Peripheral Execution Linkage

### Scenario: Tilt Detection (Timer Trigger)

**Feature**: Periodically read MPU6050 acceleration data to detect device tilt

#### Web UI Configuration Steps

**Step 1: Create Rule**

1. Click **Peripheral Execution** in the left menu
2. Click **<i class="fas fa-plus"></i> New Rule** button
3. Fill in basic configuration:
   * **Rule Name**: `Tilt Detection`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

**Step 2: Configure Trigger (Timer Trigger)**

1. Click **Add Trigger** button
2. Fill in trigger configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Timer Trigger** | Periodic collection |
   | **Timer Mode** | Select **Fixed Interval** | Millisecond interval |
   | **Interval** | `500` | 500ms |

**Step 3: Configure Action (Sensor Read)**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Sensor Read**
   * **Target Peripheral**: Select `mpu6050_01`
   * **Data Field**: `accelX` (X-axis acceleration)

3. Click **Save** button

### MPU6050 Data Channels

| Channel | Field Name | Unit | Description |
|---------|-----------|------|-------------|
| 0 | accelX | m/s² | X-axis acceleration |
| 1 | accelY | m/s² | Y-axis acceleration |
| 2 | accelZ | m/s² | Z-axis acceleration |
| 3 | temperature | °C | Chip temperature |

## Notes

1. **Firmware State**: Confirm `i2cSensors` is compiled and active
2. **Calibration**: Place the sensor at rest for zero-point calibration on first use
3. **Mounting Direction**: Sensor mounting orientation determines axis correspondence
4. **Sampling Rate**: 100-500ms sampling interval recommended for motion detection
5. **I2C Sharing**: Can share the same I2C bus with BMP280 and other devices
