# Example 29: Photoelectric Sensor

## Experiment Overview

A through-beam photoelectric sensor consists of a transmitter and receiver pair. When the light path is obstructed, the output signal changes. Suitable for counting, limit detection, conveyor belt item detection, and other industrial scenarios.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| OUT | GPIO4 | Photoelectric sensor DO output (LOW when obstructed) |

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "photoelectric_01",
      "name": "Photoelectric Sensor",
      "type": 13,
      "enabled": false,
      "pins": [4],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Item Pass Counter (Polling Trigger + Script)

**Feature**: Detect items passing through the light path and automatically count

#### 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**: `Pass Counter`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

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

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Polling Trigger** | Periodic condition check |
   | **Target Peripheral** | Select `photoelectric_01` | Photoelectric sensor |
   | **Polling Interval** | `50` | 50ms (fast detection) |
   | **Condition Expression** | `value == 0` | LOW = light path obstructed |

**Step 3: Configure Action (Script Counting)**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Command Script**
   * **Action Parameter**:
   ```
   var c=getVar('count',0);
   c++;
   setVar('count',c);
   ```

3. Click **Save** button

### Application Scenarios

| Scenario | Description | Detection Range |
|----------|-------------|-----------------|
| Item Counting | Conveyor belt item statistics | 5-30cm |
| Limit Detection | Robotic arm position limiting | 5-30cm |
| Safety Protection | Dangerous area intrusion detection | 5-30cm |

## Notes

1. **Alignment**: Transmitter and receiver must be directly aligned; misalignment causes false readings
2. **Ambient Light**: Strong light environments may interfere with the receiver; add a light shield
3. **Detection Range**: Typical effective range is 5-30cm (module type)
4. **Response Speed**: Photoelectric sensors respond quickly (<1ms), suitable for high-speed counting
