Example Code for HomeAssistant-Monitor Distance
Last revision 2026/01/21
This project integrates the A02YYUW Ultrasonic Sensor into HomeAssistant to monitor detection distances in real time.
Hardware Preparation
- A02YYUW Waterproof Ultrasonic Distance Sensor (Purchase Link)
- FireBeetle 2 ESP32-C6 IoT Development Board (Purchase Link) (Other FireBeetle boards are also compatible)
- HomeAssistant host device
Software Preparation
- Ensure the ESPHome plugin is installed in HomeAssistantOS
Wiring Diagram
Adding a New Device in ESPHome
1. Click the **NEW DEVICE** button on the ESPHome homepage
2. Click **CONTINUE**
3. Enter your device name and click **NEXT**
4. Select your development board model
5. Save the device's Encryption key and click **SKIP**
6. Click **EDIT** on the device card in ESPHome to view the auto-generated default program
Modifying the Code
Append the following code to the end of the auto-generated program, maintaining proper indentation and line breaks:
uart:
tx_pin: GPIO16
rx_pin: GPIO17
baud_rate: 9600
id: uart_a
sensor:
- platform: "a02yyuw"
name: "A02YYUW Distance"
uart_id: uart_a
Code explanation reference: Link
First Firmware Installation
- After modifying the code, click INSTALL

- For the first installation, select Plug into this computer. Subsequent updates can use Wirelessly (OTA).

- After compilation completes, click Download project

- Select Factory format (Previously Modern) to download the firmware locally

- Connect the development board to your computer and click Open ESPHome Web

- Select your device's serial port:

- Select the firmware and flash:

- Wait for the flashing process to complete
Connecting to HomeAssistant
Go to the HomeAssistant homepage and navigate to Settings > Devices & services to view your device information:
Click Add to complete integration (you may need to enter the Encryption key):
Sensor data will now appear in HomeAssistant's Overview page:

Was this article helpful?
