# Example 13: ADC Voltage Sampling

## Experiment Overview

Read analog voltage values via ADC (Analog-to-Digital Converter), converting 0~3.3V analog signals to digital values. ESP32 has two ADC modules (ADC1: 8 channels, ADC2: 10 channels), with resolution up to 12 bits (0~4095).

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| ADC | GPIO34 | Potentiometer/Analog sensor output |
| ADC | GPIO36 | Analog signal input |

> GPIO34-39 are input-only pins, best suited for ADC acquisition. ADC2 is unavailable when WiFi is active.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "adc_01",
      "name": "ADC Analog Input",
      "type": 15,
      "enabled": false,
      "pins": [34],
      "params": {}
    }
  ]
}
```

You can also use a dedicated ADC type for more precise configuration:

```json
{
  "id": "adc_precise",
  "name": "ADC Precision Acquisition",
  "type": 26,
  "enabled": false,
  "pins": [36],
  "params": {
    "attenuation": 3,
    "resolution": 12,
    "sampleRate": 0
  }
}
```

### ADC Attenuation Parameters

| attenuation | Range | Precision |
|-------------|-------|-----------|
| 0 | 0~1.1V | Highest |
| 1 | 0~1.5V | High |
| 2 | 0~2.2V | Medium |
| 3 | 0~3.3V | Lower |

## Peripheral Execution Linkage

### Scenario: Voltage Monitoring Alarm (Poll Trigger)

**Function**: Detect ADC voltage every second, trigger buzzer alarm when voltage exceeds threshold (3000mV)

#### Web Interface Configuration Steps

**Step 1: Ensure buzzer peripheral is configured**

* Peripheral ID: `buzzer_gpio`
* 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**: `Over-Voltage Alarm`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

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

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Poll Trigger** | Periodically detect condition |
   | **Target Peripheral** | Select `adc_01` | ADC analog input |
   | **Poll Interval** | `1000` | 1000ms = 1 second |
   | **Condition Expression** | `value > 3000` | Voltage greater than 3000mV |

**Step 4: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **HIGH Level**
   * **Target Peripheral**: Select `buzzer_gpio`

3. Click **Save** button

**Execution Flow**:

```
Poll every 1 second
  ↓
Read ADC value (0-4095)
  ↓
Check: value > 3000?
  ↓ Yes
Buzzer alarm
```

### ADC Attenuation Parameter Reference

| attenuation | Range | Precision | Use Case |
|-------------|-------|-----------|----------|
| 0 | 0~1.1V | Highest | Precision small voltage measurement |
| 1 | 0~1.5V | High | Small voltage measurement |
| 2 | 0~2.2V | Medium | Medium voltage measurement |
| 3 | 0~3.3V | Lower | Full range measurement (default) |

## Notes

1. **ADC2 Limitation**: ADC2 (GPIO0,2,4,12-15,25-27) is unavailable when WiFi is enabled
2. **Input Range**: Exceeding 3.3V will damage the pin, voltage divider circuit needed
3. **Precision**: ESP32 ADC has non-linear errors, calibration needed for precise measurements
4. **Input Impedance**: ADC input impedance is high, but source impedance should not exceed 10kΩ
5. **Sample Rate**: Default single sample, sampleRate=0 means read on demand
