# Multi Sensor Example This example shows how to use two HC-SR04 sensors in the same ESP-IDF application. Each sensor has its own `hc_sr04_handle_t` instance. The sensors are read sequentially to reduce acoustic cross-talk. --- ## Hardware Default GPIOs used by this example: | Sensor | TRIG | ECHO | |---|---:|---:| | Front | GPIO4 | GPIO5 | | Rear | GPIO18 | GPIO19 | The HC-SR04 ECHO pin usually outputs 5 V. ESP32 GPIOs are not 5 V tolerant, so use a voltage divider or level shifter on each ECHO signal. --- ## Build and Flash From this example directory: ```bash idf.py set-target esp32 idf.py build flash monitor ``` --- ## Expected Output ```text I (1234) multi_sensor: front distance: 18.42 cm I (1334) multi_sensor: rear distance: 42.10 cm I (2434) multi_sensor: front distance: 18.39 cm I (2534) multi_sensor: rear distance: 42.06 cm ``` --- ## Notes Avoid triggering multiple ultrasonic sensors at the same time. This example adds a short delay between readings: ```c vTaskDelay(pdMS_TO_TICKS(100)); ``` This reduces the chance of one sensor receiving the echo generated by another sensor.
To create a project from this example, run:
idf.py create-project-from-example "phlangone/hc_sr04=2.0.0:multi-sensor"