ESP32 - DYP A02 Ultrasonic configuration

Hi guys!

I’m super new to home assistant,yaml, esphome etc.

I’m having a real hard time with configuring an ultrasonic sensor. I have scoured the net for example code and tutorials.

I have an esp32 dev ch340 and DYP A02 ultrasonic sensor.

The DYP A02’s pinout is as follows:
PIN 1 - VCC
PIN 2 - GND
PIN3 - RX
PIN4 - TX

I have checked my esp32 pinout diagram and have connected the above sensor as follows:

ESP 32 PIN VIN - to sensor PIN 1 (VCC supplying 5v)
ESP 32 PIN GND - to sensor PIN 2 (supplying GND)
ESP 32 PIN D16 - GPIO16 RX - to sensor PIN 3 (RX)
ESP 32 PIN D17 - GPIO17 TX - to sensor PIN 3 (TX)

My code:

esphome:
  name: esphome-web-09eba8
  friendly_name: Float switch

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

ota:


wifi:
  ssid: "xxxxxxxxxxxxxxxxxxxx"
  password: "xxxxxxxxxxxxxxxxxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "xxxxxxxxxxxxxxxxxxx"
    password: "xxxxxxxxxxxxxxxxxxx"

captive_portal:

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO16
    echo_pin: GPIO17
    name: "Ultrasonic Sensor"
    update_interval: 5s
    pulse_time: 10us
    timeout: 20m

I am getting this error:

[12:44:35][C][logger:301]: Logger:
[12:44:35][C][logger:302]:   Level: DEBUG
[12:44:35][C][logger:303]:   Log Baud Rate: 115200
[12:44:35][C][logger:305]:   Hardware UART: UART0
[12:44:35][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'Ultrasonic Sensor'
[12:44:35][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[12:44:35][C][ultrasonic.sensor:045]:   Unit of Measurement: 'm'
[12:44:35][C][ultrasonic.sensor:045]:   Accuracy Decimals: 2
[12:44:35][C][ultrasonic.sensor:045]:   Icon: 'mdi:arrow-expand-vertical'
[12:44:35][C][ultrasonic.sensor:046]:   Echo Pin: GPIO17
[12:44:38][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.16 m
[12:44:38][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.16035 m with 2 decimals of accuracy
[12:44:38][W][component:204]: Component ultrasonic.sensor took a long time for an operation (0.09 s).
[12:44:38][W][component:205]: Components should block for at most 20-30ms.
[12:44:43][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.16 m
[12:44:43][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.16070 m with 2 decimals of accuracy
[12:44:43][W][component:204]: Component ultrasonic.sensor took a long time for an operation (0.05 s).
[12:44:43][W][component:205]: Components should block for at most 20-30ms.
[12:44:48][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.16 m
[12:44:48][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.16052 m with 2 decimals of accuracy
[12:44:53][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.16 m
[12:44:53][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.16052 m with 2 decimals of accuracy
[12:44:53][W][component:204]: Component ultrasonic.sensor took a long time for an operation (0.08 s).
[12:44:53][W][component:205]: Components should block for at most 20-30ms.

I get a constant reading of 0.16m and timeout error’s.

Any one have some advise?

It is usually TX->RX and RX->TX . It does seem from tasmota forums that it works the same as the standard HC-SR04

·Multiple output interfaces optional: UART automatic ,UART ·controlled, PWM pulse width, Switch , RS485

The component you used expects a trigger and echo pin. Probaby the PWM pulse version is what you would need for it I guess. :man_shrugging:

ESPHome also supports a ultrasonic sensor that works over I2C - if yours working only with UART you mind need to patch things together yourself :hammer_and_wrench:

He doesn’t have UART configured and does not use the pins for that function hence it doesn’t matter at all :bulb:

PS.: @Threephase your post would fit better in the esphome category :point_down:

Hey man thanks so much for responding. I’m not sure how I missed the ESPhome section, how do I go about moving this thread?

Regarding your reply, I am still battling to understand the issue due to my lack of knowledge.
I came across a little bit of code for UART which was setup like this:

uart:
  id: mod_bus
  tx_pin: GPIO17
  rx_pin: GPIO16
  baud_rate: 9600
  stop_bits: 1  

does the above code look correct to start setting up UART?

I thought I would be able to find code online I could copy/paste as that is how I have managed to build all my basic home assistant entities. Don’t get me wrong I have learnt some basics but this setup is a lot more advanced than I thought it would be. I don’t know enough to even start to problem solve this.

Not sure what to do, so I was hoping that some one could assist with some code or advice to get this thing working. If I do get it working I am definitely going to post a tutorial online for the next user as this is such an awesome project to get working and should be freely available to anyone!

You can just edit the first post and change to category :rocket:

That looks OKisk to setup up the UART bus on the esp side.

I think your device (when it only works over UART) is just not supported in esphome at the moment.

Some times ultrasonic sensor actually have a way to change the output interface - maybe you are lucky and can force your device the work the simple way via trigger and echo pin.

Well, not lucky regarding that info :face_with_raised_eyebrow:

Do you have any idea which version you got? :point_down:

No. Main Spec. Output interface Model No.
1 Split waterproof probe, no shell UART auto DYP-A02YYU-V2.0
2 Split waterproof probe, no shell UART controlled DYP-A02YYT-V2.0
3 Split waterproof probe, no shell PWM pulse width, DYP-A02YYM-V2.0
4 Split waterproof probe, no shell Switch DYP-A02YYGD-V2.0
5 Split waterproof probe and housing UART auto DYP-A02YYUW-V2.0
6 Split waterproof probe and housing UART controlled DYP-A02YYTW-V2.0
7 Split waterproof probe and housing PWM pulse width, DYP-A02YYMW-V2.0
8 Split waterproof probe and housing Switch DYP-A02YYGDW-V2.0
9 Split waterproof probe and housing RS485 DYP-A02YY4W-V2.0

Unfortunately my sensor came with no documentation. The local site that I purchased it from can only tell me that it is an A02 but cannot stipulate which model. There is a sticker on the sensor with the following:

A0221AU
2134013

I just want to already thank you for taking the time out to help me.

Edit: it seems to bring up alot of UART controlled sensors when googling

This is the closest thread to my issue and the user ended up getting the PWM version as he could not get the UART version to work. Seems I may have wasted a purchase.

esphome just got support for a A01NYUB ultrasonic sensor which communicates over UART :point_down:

Might be worth a shot for with your device :wink:

The A01NYUB isn’t the greatest for a sump since it has an 11" blind spot.

Has anyone been able to get the a02yyuw workign with Home Assistant ESPHome? I have been futzing with this for a while and can’t get it to work. Mine is no a DFRobot but some other UART version from Amazon that at least appears identical. But I can’t get it to work. Also tried with an arduino uno and DFRobot sample code with no luck. So maybe it’s just not compatible in some way.

For anybody who may come across this forum and needs something to work for them, This is what worked for me to get my Controlled UART Sensor to show distance:
The sample code near the bottom of this page Underwater Ultrasonic Obstacle Avoidance Sensor -6m Arduino WiKi - DFRobot
Worked for my A2211AT Ultrasonic sensor (Not the sensor this code is designed for but i’ll take the win).
I am using an ESP32-WROOM-32U

Hi I have a ultrasonic sensor model A02YYT brought from amazon . Its running on ESP32 and program with esphome devkitc V4 with the following code hope it helps :
esp32: board is ESP32 DevKitC ESP32-WROOM-32U Core Development Board with 3dBi 2.4GHz Antenna, Micro USB, WiFi+BT Development Module, Dual-Core Processing for IoT Projects (DevKitC) : Amazon.ca: Electronics

A022YT sensor: Amazon.ca

#/*********
#https://docs.espressif.com/projects/esp-idf/en/v5.1/esp32/hw-reference/esp32/get-started-devkitc.html#get-started-esp32-devkitc-board-front
#*********/

esphome:
  name: ultrasound
  platform: ESP32
  board: esp32dev
  platformio_options: 
    platform: espressif32
    board: az-delivery-devkit-v4
    board_build.mcu: esp32
    board_build.f_cpu: 240000000L

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: ""

ota:
  - platform: esphome
    password: ""

wifi:
  ssid: "yourwifi"
  password: "yourpassword"
  # since I have a hidden network :
  fast_connect: "true"
  manual_ip:
    static_ip: 192.168.2.222
    gateway: 192.168.2.1
    subnet: 255.255.255.0


  ap:
    ssid: "ultrason"
    password: "password"
    
captive_portal:

uart:
    rx_pin: 19 # ESP32 C3 super mini
    #tx_pin: 18 # ESP32 C3 super mini used by switch to trigger 
    baud_rate: 9600

sensor:

  • platform: a02yyuw
    id: a02yyuw_sensor # UART Controlled
    name: “detected_Distance(m)”
    unit_of_measurement: ‘m’
    accuracy_decimals: 2
    filters:
      - multiply: 0.00125
      - calibrate_linear:
         method: least_squares
         datapoints:
         # Map 0.x (from sensor) to 1.0 (true value)
          - 0.53 -> 0.52
          - 1.01 -> 0.93
          - 1.54 -> 1.34
          - 2.32 -> 1.98
#
#      - throttle: 5s
#      - heartbeat: 5s
#      - quantile:
#         window_size: 7
#         send_every: 4
#         send_first_at: 3
#         quantile: .9
#      - debounce: 0.1s

switch:

  • platform: gpio
    name: “GPIO21 Switch”
    pin: 18
    id: GPIO21_switch
    internal: true

interval:

  • interval: 3s
    then:
    • switch.turn_on: GPIO21_switch
    • delay: 100ms
    • switch.turn_off: GPIO21_switch
    • delay: 50ms