# Examples

A comprehensive collection of FastBee-Arduino configuration examples, covering basic GPIO operations, sensor integration, display control, communication protocols and Modbus sub-device access.

> Recommended reading: [Supported Sensors and Modules](https://www.fastbee.cn/doc/en/device/v2/getting-started/supported-sensors-and-modules.html) - Confirm version support first; [Complete Sensor and Module Guide](https://www.fastbee.cn/doc/en/device/v2/peripherals/sensor-guide-complete.html) - View detailed configuration reference

## Hardware Cases

The examples below correspond to different hardware and configurations; their JSON must not be copied across devices:

| Environment | Example | Main content |
| --- | --- | --- |
| `esp32c3-F4R0` | [C3 Temperature/Humidity](https://www.fastbee.cn/doc/en/device/v2/examples/esp32c3-temperature-humidity.html) | OLED, temperature/humidity, and low-memory usage |
| `esp32c3-F4R0` (verify Flash first) | [C3 Smart Switch Panel](https://www.fastbee.cn/doc/en/device/v2/examples/esp32c3-smart-switch-panel.html) | Neutral-live 1/2/3-gang, button single click, relay and web sync; materials verified, physical device pending |
| `esp32c6-F4R0` | [C6 RGB Quick Start](https://www.fastbee.cn/doc/en/device/v2/examples/esp32c6-blink-quickstart.html) | GPIO8 WS2812 with timer/platform linkage |
| `esp32-F4R0` | [Puzhong ESP32 Test](https://www.fastbee.cn/doc/en/device/v2/examples/puzhong-esp32-test.html) | Classic ESP32 peripherals and rules |
| `esp32s3-F16R8` | [ECM50-A Networking and Industrial Collection](https://www.fastbee.cn/doc/en/device/v2/examples/ecm50a-networking.html) | AI/DI, SHT30, Modbus RTU, Ethernet, and 4G |
| `esp32s3-F8R0` | [Waveshare 6-channel Relay](https://www.fastbee.cn/doc/en/device/v2/examples/waveshare-esp32s3.html) | 6-channel relay, WS2812, and RS485 |
| `esp32s3-F16R8` | [Waveshare 4-inch Touchscreen](https://www.fastbee.cn/doc/en/device/v2/examples/waveshare-esp32s3-touch-lcd4.html) | RGB display, GT911 touch, web drag-and-drop editing, and generic device control |

The JSON for existing cases is kept under `docs/` in the FastBee-Arduino source repository; the smart switch panel case provides field notes and the original Arduino code, and a dedicated config must be established after confirming the physical device identity — the COM4 temperature/humidity config cannot be reused. Verify the chip, pins, and local network parameters before importing; public examples contain no production WiFi or MQTT credentials.

## Example List

### Basic Experiments (01-24)

| No. | Name | Core Peripheral Type |
|-----|------|---------------------|
| [01](https://www.fastbee.cn/doc/en/device/v2/examples/01-led-first.html) | LED On | GPIO\_DIGITAL\_OUTPUT |
| [02](https://www.fastbee.cn/doc/en/device/v2/examples/02-led-blink.html) | LED Blink | GPIO\_DIGITAL\_OUTPUT + periph-exec |
| [03](https://www.fastbee.cn/doc/en/device/v2/examples/03-led-flowing.html) | LED Running Light | Multi GPIO + JavaScript Script |
| [03b](https://www.fastbee.cn/doc/en/device/v2/examples/03b-led-flowing-command-script.html) | LED Running Light (Command Script) | Multi GPIO + PERIPH/DELAY Command Sequence |
| [04](https://www.fastbee.cn/doc/en/device/v2/examples/04-buzzer.html) | Buzzer | GPIO\_DIGITAL\_OUTPUT / PWM |
| [05](https://www.fastbee.cn/doc/en/device/v2/examples/05-relay.html) | Relay | GPIO\_DIGITAL\_OUTPUT |
| [06](https://www.fastbee.cn/doc/en/device/v2/examples/06-button-control.html) | Button Control | GPIO\_DIGITAL\_INPUT\_PULLUP |
| [07](https://www.fastbee.cn/doc/en/device/v2/examples/07-dc-motor.html) | DC Motor | GPIO\_DIGITAL\_OUTPUT |
| [08](https://www.fastbee.cn/doc/en/device/v2/examples/08-stepper-motor.html) | Stepper Motor | STEPPER\_MOTOR |
| [09](https://www.fastbee.cn/doc/en/device/v2/examples/09-external-interrupt.html) | External Interrupt | GPIO\_INTERRUPT |
| [10](https://www.fastbee.cn/doc/en/device/v2/examples/10-timer-interrupt.html) | Timer Trigger | TIMER\_TRIGGER |
| [11](https://www.fastbee.cn/doc/en/device/v2/examples/11-pwm-breathing.html) | PWM Breathing LED | GPIO\_PWM\_OUTPUT |
| [12](https://www.fastbee.cn/doc/en/device/v2/examples/12-uart-communication.html) | UART Communication | UART |
| [13](https://www.fastbee.cn/doc/en/device/v2/examples/13-adc-voltage.html) | ADC Voltage Sampling | GPIO\_ANALOG\_INPUT |
| [14](https://www.fastbee.cn/doc/en/device/v2/examples/14-rgb-neopixel.html) | RGB LED Strip | NEO\_PIXEL |
| [15](https://www.fastbee.cn/doc/en/device/v2/examples/15-seven-segment.html) | Seven-Segment Display | SEVEN\_SEGMENT\_TM1637 |
| [16](https://www.fastbee.cn/doc/en/device/v2/examples/16-rtc-clock.html) | NTP Clock | System NTP + Timer |
| [17](https://www.fastbee.cn/doc/en/device/v2/examples/17-ds1302-rtc.html) | DS1302 RTC | External RTC |
| [18](https://www.fastbee.cn/doc/en/device/v2/examples/18-ds18b20-temperature.html) | DS18B20 Temperature | SENSOR(ONE\_WIRE) |
| [19](https://www.fastbee.cn/doc/en/device/v2/examples/19-dht11-humidity.html) | DHT11 Temp & Humidity | SENSOR(DHT) |
| [20](https://www.fastbee.cn/doc/en/device/v2/examples/20-ultrasonic-distance.html) | Ultrasonic Distance | SENSOR(HC-SR04) |
| [21](https://www.fastbee.cn/doc/en/device/v2/examples/21-ir-remote.html) | IR Remote Control | IR\_REMOTE (check environment feature state) |
| [22](https://www.fastbee.cn/doc/en/device/v2/examples/22-servo-sg90.html) | Servo Control | PWM\_SERVO |
| [23](https://www.fastbee.cn/doc/en/device/v2/examples/23-oled-display.html) | OLED Display | LCD(SSD1306) |
| [24](https://www.fastbee.cn/doc/en/device/v2/examples/24-sd-card.html) | SD Card Read/Write | SDIO config placeholder (SPI partial, file actions TBD) |

### Extended Experiments (25-44)

| No. | Name | Core Peripheral Type |
|-----|------|---------------------|
| [25](https://www.fastbee.cn/doc/en/device/v2/examples/25-laser-sensor.html) | Laser Sensor | GPIO\_DIGITAL\_OUTPUT |
| [26](https://www.fastbee.cn/doc/en/device/v2/examples/26-tilt-sensor.html) | Tilt Sensor | GPIO\_DIGITAL\_INPUT\_PULLUP |
| [27](https://www.fastbee.cn/doc/en/device/v2/examples/27-vibration-sensor.html) | Vibration Sensor | GPIO\_DIGITAL\_INPUT |
| [28](https://www.fastbee.cn/doc/en/device/v2/examples/28-reed-switch.html) | Reed Switch | GPIO\_DIGITAL\_INPUT |
| [29](https://www.fastbee.cn/doc/en/device/v2/examples/29-photoelectric-sensor.html) | Photoelectric Sensor | GPIO\_DIGITAL\_INPUT |
| [30](https://www.fastbee.cn/doc/en/device/v2/examples/30-rain-sensor.html) | Rain Drop Sensor | ADC + GPIO |
| [31](https://www.fastbee.cn/doc/en/device/v2/examples/31-ps2-joystick.html) | PS2 Joystick | Dual ADC + Button |
| [32](https://www.fastbee.cn/doc/en/device/v2/examples/32-sound-sensor.html) | Sound Sensor | GPIO\_ANALOG\_INPUT |
| [33](https://www.fastbee.cn/doc/en/device/v2/examples/33-light-sensor.html) | Light Sensor | GPIO\_ANALOG\_INPUT |
| [34](https://www.fastbee.cn/doc/en/device/v2/examples/34-flame-sensor.html) | Flame Sensor | GPIO\_ANALOG\_INPUT |
| [35](https://www.fastbee.cn/doc/en/device/v2/examples/35-smoke-sensor.html) | Smoke Sensor MQ-2 | GPIO\_ANALOG\_INPUT |
| [36](https://www.fastbee.cn/doc/en/device/v2/examples/36-touch-sensor.html) | Touch Sensor | GPIO\_TOUCH |
| [37](https://www.fastbee.cn/doc/en/device/v2/examples/37-rotary-encoder.html) | Rotary Encoder | ENCODER |
| [38](https://www.fastbee.cn/doc/en/device/v2/examples/38-ir-obstacle.html) | IR Obstacle | GPIO\_DIGITAL\_INPUT |
| [39](https://www.fastbee.cn/doc/en/device/v2/examples/39-lcd1602-display.html) | LCD1602 Display | LCD config placeholder (PCF8574 driver TBD) |
| [40](https://www.fastbee.cn/doc/en/device/v2/examples/40-bmp280-pressure.html) | BMP280 Pressure | I2C SENSOR (`i2cSensors` required) |
| [41](https://www.fastbee.cn/doc/en/device/v2/examples/41-mpu6050-gyro.html) | MPU6050 Gyroscope | I2C SENSOR (`i2cSensors` required) |
| [42](https://www.fastbee.cn/doc/en/device/v2/examples/42-ir-tracking.html) | IR Line Tracking | GPIO\_DIGITAL\_INPUT |
| [43](https://www.fastbee.cn/doc/en/device/v2/examples/43-rfid-mfrc522.html) | RFID MFRC522 | SPI SENSOR (`rfid` required) |
| [44](https://www.fastbee.cn/doc/en/device/v2/examples/44-pir-motion.html) | PIR Motion | GPIO\_DIGITAL\_INPUT |

### Modbus Sub-Devices (45-48)

| No. | Name | Core Peripheral Type |
|-----|------|---------------------|
| [45](https://www.fastbee.cn/doc/en/device/v2/examples/45-modbus-temp-humidity.html) | Modbus Temp & Humidity Sensor | UART(Modbus RTU) |
| [46](https://www.fastbee.cn/doc/en/device/v2/examples/46-modbus-stepper-motor.html) | Modbus Stepper Motor Control | UART(Modbus RTU) |
| [47](https://www.fastbee.cn/doc/en/device/v2/examples/47-modbus-relay.html) | Modbus Relay Module | UART(Modbus RTU) |
| [48](https://www.fastbee.cn/doc/en/device/v2/examples/48-modbus-soil-sensor.html) | Modbus Soil Sensor | UART(Modbus RTU) |

### RF & Radar (49-50)

| No. | Name | Core Peripheral Type |
|-----|------|---------------------|
| [49](https://www.fastbee.cn/doc/en/device/v2/examples/49-433mhz-rf-module.html) | 433MHz RF Module | RF\_MODULE |
| [50](https://www.fastbee.cn/doc/en/device/v2/examples/50-radar-sensor.html) | Radar Sensor Module | RADAR\_SENSOR |

## Best Practices

### Pin Assignment Principles

* **GPIO34-39**: Input only, suitable for ADC/digital input
* **GPIO6-11**: Connected to Flash, not recommended for use
* **GPIO32-39**: ADC1, available after WiFi is enabled (preferred)
* **GPIO4,12-15,25-27**: ADC2, unavailable after WiFi is enabled
* **GPIO0**: BOOT button, use with caution
* **GPIO5**: STATE LED, occupied by default

### Sensor Configuration Tips

* **DHT11/DHT22**: Sampling interval >= 2 seconds, avoid reading too fast to prevent data anomalies
* **DS18B20**: Multiple sensors can share the one-wire bus, distinguished by address
* **I2C Devices**: Can share SDA/SCL, but I2C addresses must not conflict
* **Ultrasonic HC-SR04**: Trigger and echo pins must differ, sampling interval >= 100ms
* **BMP280/MPU6050**: Require `FASTBEE_ENABLE_I2C_SENSORS` and the active `i2cSensors` runtime feature

### Peripheral Execution Rule Design

* **Periodic Acquisition**: intervalSec >= 5 seconds, avoid frequent sensor reads
* **Event Trigger**: First confirm the acquisition rule has generated the `ds:<id>_<field>` data source
* **Linked Control**: Pay attention to relay active level (high-active / low-active)
* **Script Execution**: Requires active `ruleScript` and `commandScript`; low-memory targets may reject or disable script work
* **Sync Delay**: Add syncDelayMs (100-500ms) between actions to avoid concurrent conflicts

## Common Scenarios

### Scenario 1: Temperature/Humidity Monitoring Alarm

**Requirement**: Turn on the fan (relay) when temperature exceeds 30°C

**Steps**:

1. Configure DHT11 sensor (type: 38, pin: 13)
2. Configure relay (type: 12, pin: 25)
3. Create periodic acquisition rule (read DHT11 every 10 seconds)
   * Trigger: timerTrigger, intervalSec: 10
   * Action 1: sensorRead (dht\_01, temperature)
4. Create event trigger rule (temperature > 30°C)
   * Trigger: eventTrigger, eventId: `ds:dht_01_temperature`, operator: >, value: 30
   * Action: GPIO HIGH (relay\_01)
5. Report to MQTT cloud platform (reportAfterExec: true)

**Reference**: [19-dht11-humidity.md](https://www.fastbee.cn/doc/en/device/v2/examples/19-dht11-humidity.html), [05-relay.md](https://www.fastbee.cn/doc/en/device/v2/examples/05-relay.html)

### Scenario 2: Auto Light on Low Illuminance

**Requirement**: Automatically turn on LED when illuminance drops below 50 lux

**Steps**:

1. Configure BH1750 light sensor (type: 38, I2C address: 0x23)
   * Or use photoresistor ADC (type: 15, pin: 34)
2. Configure LED (type: 12, pin: 26)
3. Periodically read illuminance (every 5 seconds)
   * Action: sensorRead (bh1750\_01, illuminance)
4. Event trigger: illuminance < 50 lux
   * Trigger: eventTrigger, eventId: `ds:bh1750_01_illuminance`, operator: <, value: 50
   * Action: GPIO HIGH (led\_01)
5. Delayed off or re-detect (avoid frequent switching)

**Reference**: [33-light-sensor.md](https://www.fastbee.cn/doc/en/device/v2/examples/33-light-sensor.html), [01-led-first.md](https://www.fastbee.cn/doc/en/device/v2/examples/01-led-first.html)

### Scenario 3: Modbus Device Control

**Requirement**: Control temperature/humidity sensor and relay via Modbus RTU

**Steps**:

1. Configure UART as Modbus RTU (type: 1, pins: TX-17, RX-16)
2. Scan slave devices (address 1-247)
3. Map registers to data points
   * Holding registers 40001-40002: temperature
   * Holding registers 40003-40004: humidity
4. Create polling rule (every 10 seconds)
   * Trigger: pollTrigger, read holding registers
   * Action: parse data, write to sensor\_cache
5. Conditional trigger control command
   * Event trigger: temperature > 35°C
   * Action: write Modbus coil (turn on relay)

**Reference**: [45-modbus-temp-humidity.md](https://www.fastbee.cn/doc/en/device/v2/examples/45-modbus-temp-humidity.html), [47-modbus-relay.md](https://www.fastbee.cn/doc/en/device/v2/examples/47-modbus-relay.html)

### Scenario 4: Local Display Monitoring

**Requirement**: OLED screen displays temperature and humidity, refreshed every second

**Steps**:

1. Configure DHT11 sensor (type: 38)
2. Configure OLED display (type: 36, I2C address: 0x3C)
3. Periodically read sensor data (every 2 seconds)
   * Action 1: sensorRead (dht\_01, temperature)
   * Action 2: sensorRead (dht\_01, humidity)
4. Display action output
   * Action 3: displayOLED, show "Temp: 28.5°C"
   * Action 4: displayOLED, show "Humidity: 65%"
5. Or use TM1637 seven-segment display for alternating display
   * Action 3: displayTM1637, show temperature for 2 seconds
   * Action 4: displayTM1637, show humidity for 2 seconds

**Reference**: [23-oled-display.md](https://www.fastbee.cn/doc/en/device/v2/examples/23-oled-display.html), [15-seven-segment.md](https://www.fastbee.cn/doc/en/device/v2/examples/15-seven-segment.html)

### Scenario 5: Multi-Function Button Control

**Requirement**: Single button for short-press LED toggle and long-press mode switch

**Steps**:

1. Configure button (type: 13, GPIO\_DIGITAL\_INPUT\_PULLUP, pin: 0)
2. Configure LED (type: 12, pin: 26)
3. Create event trigger rule (button press)
   * Trigger: eventTrigger, eventId: `button:btn_01:click`
   * Action: toggle LED state (HIGH/LOW)
4. Create long-press rule
   * Trigger: eventTrigger, eventId: `button:btn_01:long_press`
   * Action: toggle mode (auto/manual)

**Reference**: [06-button-control.md](https://www.fastbee.cn/doc/en/device/v2/examples/06-button-control.html)

### Scenario 6: Overcurrent Protection

**Requirement**: Auto power-off and alarm when current exceeds 15A

**Steps**:

1. Configure ACS712 current sensor ADC (type: 15, pin: 34)
2. Configure relay (type: 12, pin: 25)
3. Periodically read current value (every 1 second)
   * Action: sensorRead (current\_01, current)
   * Advanced params: sensitivity, zeroOffset, vRef, adcMax
4. Event trigger: current > 15
   * Action 1: GPIO LOW (turn off relay)
   * Action 2: triggerEvent, report `over_current` event
   * Action 3: MQTT report alarm message

**Reference**: [13-adc-voltage.md](https://www.fastbee.cn/doc/en/device/v2/examples/13-adc-voltage.html)

## Peripheral Type Quick Reference

| Type Value | Type Name | Description |
|------------|-----------|-------------|
| 1 | UART | Serial / Modbus RTU |
| 11 | GPIO\_DIGITAL\_INPUT | Digital input |
| 12 | GPIO\_DIGITAL\_OUTPUT | Digital output |
| 13 | GPIO\_DIGITAL\_INPUT\_PULLUP | Pull-up input (button) |
| 15 | GPIO\_ANALOG\_INPUT | ADC analog input |
| 17 | GPIO\_PWM\_OUTPUT | PWM output |
| 18 | GPIO\_INTERRUPT\_RISING | Rising edge interrupt |
| 19 | GPIO\_INTERRUPT\_FALLING | Falling edge interrupt |
| 20 | GPIO\_INTERRUPT\_CHANGE | Dual-edge interrupt |
| 21 | GPIO\_TOUCH | Touch input |
| 36 | LCD | Display (currently supports SSD1306/SH1106, LCD1602 TBD) |
| 37 | SDIO | SD card interface config placeholder |
| 38 | SENSOR | Sensor (DHT/DS18B20/BMP280 etc.) |
| 41 | PWM\_SERVO | Servo |
| 42 | STEPPER\_MOTOR | Stepper motor |
| 43 | ENCODER | Rotary encoder |
| 44 | ONE\_WIRE | One-wire bus (DS18B20) |
| 45 | NEO\_PIXEL | WS2812B RGB LED strip |
| 47 | SEVEN\_SEGMENT\_TM1637 | TM1637 seven-segment display |
| 48 | RF\_MODULE | 433MHz RF transmit/receive level |
| 49 | RADAR\_SENSOR | RCWL-0516 / 5.8GHz radar sensor |

## Firmware Compatibility

| Mark | Meaning |
|------|---------|
| Release environments | Supported according to compile state, runtime feature switch, memory budget and chip pin constraints |
| Feature-dependent | Check compile support, runtime switch, memory budget and chip-specific overrides |

## Related Documentation

* [Peripheral Management](https://www.fastbee.cn/doc/en/device/v2/system/peripheral-management.html) — Add and configure peripherals
* [Peripheral Execution Management](https://www.fastbee.cn/doc/en/device/v2/system/periph-exec-management.html) — Rule linkage configuration
* [Peripheral Documentation](https://www.fastbee.cn/doc/en/device/v2/peripherals.md) — Detailed peripheral type descriptions
* [Modbus RTU Protocol](https://www.fastbee.cn/doc/en/device/v2/protocols/modbus-rtu.html) — Modbus communication configuration
