# Example 42: IR Tracking Sensor

## Experiment Overview

An IR tracking sensor identifies black lines (low reflection) and white backgrounds (high reflection) by detecting ground reflectance differences. Suitable for line-following robots, path tracking, and similar applications.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| OUT\_L | GPIO4 | Left tracking module DO |
| OUT\_R | GPIO15 | Right tracking module DO |

> Outputs HIGH over black lines (no reflection); outputs LOW over white ground (reflection detected).

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "track_left",
      "name": "Tracking - Left",
      "type": 11,
      "enabled": false,
      "pins": [4],
      "params": {}
    },
    {
      "id": "track_right",
      "name": "Tracking - Right",
      "type": 11,
      "enabled": false,
      "pins": [15],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Line-Following Control (Timer Trigger + Script)

**Feature**: Control motor direction based on left/right sensor states to follow a black line

#### Web UI Configuration Steps

**Step 1: Ensure Motor Peripherals are Configured**

* Motor IN1: `motor_in1` (GPIO Output)
* Motor IN2: `motor_in2` (GPIO Output)

**Step 2: 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**: `Line-Following Control`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

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

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Timer Trigger** | Periodic detection |
   | **Timer Mode** | Select **Fixed Interval** | Millisecond interval |
   | **Interval** | `50` | 50ms (fast response) |

**Step 4: Configure Action (Script Control)**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Command Script**
   * **Action Parameter**:
   ```
   var l=gpioRead('track_left');
   var r=gpioRead('track_right');
   if(l==1&&r==0){
     gpioWrite('motor_in1',0);
     gpioWrite('motor_in2',1);
   } else if(l==0&&r==1){
     gpioWrite('motor_in1',1);
     gpioWrite('motor_in2',0);
   } else {
     gpioWrite('motor_in1',1);
     gpioWrite('motor_in2',1);
   }
   ```

3. Click **Save** button

### Line-Following Control Logic

| Left Sensor | Right Sensor | Status | Action |
|------------|-------------|--------|--------|
| 1 (Black Line) | 0 (White Ground) | Drift Left | Turn Right |
| 0 (White Ground) | 1 (Black Line) | Drift Right | Turn Left |
| 1 (Black Line) | 1 (Black Line) | Center | Go Straight |
| 0 (White Ground) | 0 (White Ground) | Off Line | Straight / Stop |

## Notes

1. **Mounting Height**: 1-2cm from ground for best results
2. **Black Line Width**: Recommended 2-3cm width, matching sensor spacing
3. **Ambient Light**: Strong light may affect detection; add a light shield
4. **Multi-Sensor Expansion**: Use 3-5 sensors for improved tracking accuracy
