# Example 8: Stepper Motor Control

## Experiment Overview

Control a 28BYJ-48 stepper motor via ULN2003 driver board for precise angle and speed control. FastBee has a built-in `STEPPER_MOTOR` peripheral type (type: 42), supporting half-step drive mode.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| IN1 | GPIO15 | ULN2003 Driver Board IN1 |
| IN2 | GPIO2 | ULN2003 Driver Board IN2 |
| IN3 | GPIO0 | ULN2003 Driver Board IN3 |
| IN4 | GPIO4 | ULN2003 Driver Board IN4 |

> ULN2003 module requires external 5V power, 28BYJ-48 stepper motor has 2048 steps per revolution (half-step mode).

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "stepper_01",
      "name": "ULN2003 Stepper Motor",
      "type": 42,
      "enabled": false,
      "pins": [15, 2, 0, 4],
      "params": {
        "stepsPerRevolution": 2048,
        "speed": 8
      }
    }
  ]
}
```

### Parameter Description

| Parameter | Description | Default |
|-----------|-------------|---------|
| stepsPerRevolution | Steps per revolution (28BYJ-48 half-step=2048) | 2048 |
| speed | Speed (RPM) | 8 |

## Peripheral Execution Linkage

### Scenario 1: Forward One Revolution (Platform Trigger)

**Function**: Control motor forward one revolution (2048 steps) via cloud platform command

#### Web Interface 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**: `Stepper Forward`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Platform Trigger** | Receive cloud platform commands |
   | **Operator** | Select **Match Pattern** | Match command |
   | **Comparison Value** | `{"cmd":"forward"}` | Forward command |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in action configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Action Type** | Select **Call Peripheral** | Call peripheral method |
   | **Target Peripheral** | Select `stepper_01` | Stepper motor peripheral |
   | **Action Params** | `2048` | Forward 2048 steps (one revolution) |

3. Click **Save** button

**Execution Flow**:

```
Receive forward command
  ↓
Call stepper_01 move method
  ↓
Forward 2048 steps (one revolution)
  ↓
Action complete (blocking execution)
```

> ⚠️ **Note**: stepper\_move action blocks the current rule until motor movement completes

***

### Scenario 2: Reverse Half Revolution (Platform Trigger)

**Function**: Control motor reverse half revolution (-1024 steps) via cloud platform command

#### Web Interface Configuration Steps

**Step 1: Create Rule**

1. Click **<i class="fas fa-plus"></i> New Rule**
2. Fill in basic configuration:
   * **Rule Name**: `Stepper Reverse`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

1. Click **Add Trigger** button
2. Fill in:
   * **Trigger Type**: Select **Platform Trigger**
   * **Operator**: Select **Match Pattern**
   * **Comparison Value**: `{"cmd":"reverse"}`

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Call Peripheral**
   * **Target Peripheral**: Select `stepper_01`
   * **Action Params**: `-1024` (negative = reverse, 1024 steps = half revolution)

3. Click **Save** button

### Steps to Angle Conversion

| Steps | Angle | Description |
|-------|-------|-------------|
| 2048 | 360° | One full revolution |
| 1024 | 180° | Half revolution |
| 512 | 90° | Quarter revolution |
| 256 | 45° | Eighth revolution |
| -2048 | -360° | Reverse one full revolution |

## Notes

1. **Power**: Stepper motor needs 5V independent power, ULN2003 cannot be powered from ESP32 3.3V
2. **Heat**: Stepper motor still has holding current when stationary, disconnect power when not in use for long periods
3. **Speed Limit**: 28BYJ-48 max ~15RPM, exceeding will cause missed steps
4. **Pin Order**: IN1-IN4 order must be correct, otherwise motor will vibrate but not rotate
5. **Blocking Execution**: stepper\_move action blocks the current rule until complete
