# Example 36: Touch Sensor

## Experiment Overview

ESP32 has built-in capacitive touch sensors supporting 10 touch pins (Touch0~Touch9), enabling touch detection without external components. FastBee provides the `GPIO_TOUCH` peripheral type (type: 21) for direct support.

## Hardware Wiring

| Touch Channel | GPIO Pin | Description |
|--------------|----------|-------------|
| Touch0 | GPIO4 | Touch input 0 |
| Touch2 | GPIO2 | Touch input 2 |
| Touch3 | GPIO15 | Touch input 3 |
| Touch4 | GPIO13 | Touch input 4 |
| Touch5 | GPIO12 | Touch input 5 |
| Touch6 | GPIO14 | Touch input 6 |
| Touch7 | GPIO27 | Touch input 7 |
| Touch8 | GPIO33 | Touch input 8 |
| Touch9 | GPIO32 | Touch input 9 |

> Touch pins can detect touch by connecting directly to a metal pad or conductive material.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "touch_01",
      "name": "Touch Input",
      "type": 21,
      "enabled": false,
      "pins": [4],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Touch Toggle Light (Polling Trigger)

**Feature**: Toggle LED on/off when touch sensor is touched

#### Web UI Configuration Steps

**Step 1: Ensure LED Peripheral is Configured**

* Peripheral ID: `led_d1`
* Type: 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**: `Touch Toggle Light`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

**Step 3: 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 `touch_01` | Touch sensor |
   | **Polling Interval** | `100` | 100ms |
   | **Condition Expression** | `value < 40` | Touch threshold |

**Step 4: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Toggle Level**
   * **Target Peripheral**: Select `led_d1`

3. Click **Save** button

### ESP32 Touch Pin List

| Touch Channel | GPIO Pin | Touch Channel | GPIO Pin |
|--------------|----------|--------------|----------|
| Touch0 | GPIO4 | Touch5 | GPIO12 |
| Touch2 | GPIO2 | Touch6 | GPIO14 |
| Touch3 | GPIO15 | Touch7 | GPIO27 |
| Touch4 | GPIO13 | Touch8 | GPIO33 |
| | | Touch9 | GPIO32 |

> ⚠️ **Note**: Touch1 (GPIO0) is not recommended; it may affect touch readings when WiFi is active

## Notes

1. **Threshold**: Value decreases when touched (typically <40); higher when not touched (>60); determine threshold through actual testing
2. **Wire Length**: Wire from touch pin to sensing pad should not be too long (<30cm)
3. **ESP32-S3**: S3 has improved touch sensors with different threshold ranges
4. **Anti-False-Touch**: Add touch confirmation delay to avoid false triggers
5. **WiFi Impact**: WiFi operation may slightly affect touch readings
