Ultrasonic UART sensor

I cannot get my ultrasonic sensor to work, and I cannot seem to understand what I am doing wrong.

This is my code:

esphome:
  name: waste_tank
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: "xxx"
  password: "xxx"

# Enable logging
logger:
 
sensor:
  - platform: ultrasonic
    trigger_pin: GPIO5
    echo_pin: GPIO4
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    update_interval: 5000ms
    name: "Waste tank"

My log looks like this:

[18:22:05][C][ultrasonic.sensor:035]: Ultrasonic Sensor 'Waste tank'
[18:22:05][C][ultrasonic.sensor:035]:   Unit of Measurement: '%'
[18:22:05][C][ultrasonic.sensor:035]:   Accuracy Decimals: 0
[18:22:05][C][ultrasonic.sensor:035]:   Icon: 'mdi:water-percent'
[18:22:05][C][ultrasonic.sensor:036]:   Echo Pin: GPIO4 (Mode: INPUT)
[18:22:05][C][ultrasonic.sensor:037]:   Trigger Pin: GPIO5 (Mode: OUTPUT)
[18:22:05][C][ultrasonic.sensor:038]:   Pulse time: 10 µs
[18:22:05][C][ultrasonic.sensor:039]:   Timeout: 11661 µs
[18:22:05][C][ultrasonic.sensor:040]:   Update Interval: 5.0s
[18:22:05][C][ota:029]: Over-The-Air Updates:
[18:22:05][C][ota:030]:   Address: waste_tank.local:3232
[18:22:05][C][ota:032]:   Using Password.
[18:22:05][C][api:103]: API Server:
[18:22:05][C][api:104]:   Address: waste_tank.local:6053
[18:22:06][D][ultrasonic.sensor:025]: 'Waste tank' - Distance measurement timed out!
[18:22:06][D][sensor:092]: 'Waste tank': Sending state nan % with 0 decimals of accuracy
[18:22:11][D][ultrasonic.sensor:025]: 'Waste tank' - Distance measurement timed out!
[18:22:11][D][sensor:092]: 'Waste tank': Sending state nan % with 0 decimals of accuracy
[18:22:16][D][ultrasonic.sensor:025]: 'Waste tank' - Distance measurement timed out!
[18:22:16][D][sensor:092]: 'Waste tank': Sending state nan % with 0 decimals of accuracy
[18:22:21][D][ultrasonic.sensor:025]: 'Waste tank' - Distance measurement timed out!

I have tried to swap the echo and trigger, no change. Never once got an echo. Is there some way to measure if I have cabled it correctly with a multimeter or similar?

Short and sweet I have connected the UART Ultrasonic sensor to 5V+ and GND. Trigger to GPIO5 and Echo to GPIO4. Supersimple, or so I thought… My sensor is a waterpoof one documented here: https://github.com/ritesht93/waterproof_small_blind_ultrasonic_sensor_DYP-A02YYxx_v1.0

It is supposed to be 100% compatible with code and cabling for HC-SR04. I have been looking at different guides but mostly used this: https://www.hackster.io/AskSensors/ultrasonic-ranging-with-esp32-and-asksensors-iot-2fafeb. I have also tried the GPIO16 and GPIO17 (RX/TX), but just the same result.

All tips or pointers welcome!

You need the PWM version to work with esphome. Not the UART version.

1 Like

Ok, I was worried that was the case. I will order another :ok_hand:

Hello again @tom_l

I have ordered the PWM one and I still cannot get it to work.

This is the Arduino code: https://github.com/riteshRcH/waterproof_small_blind_ultrasonic_sensor_DYP-A02YYxx_v1.0/blob/d8dcda206587feae70b3fca179b05b50d18549f7/code/arduino/DYPA02YYWM_v1_arduino_uno_code/DYPA02YYWM_v1_arduino_uno_code.ino

Is there some magic I do not understand? I have tried my code with a HC-SR04 and that works fine. When I put my ear to the sensor (the A02YYWM one) I can hear the “tick” of the trigger. But it does not read any data back. Tried also to increase the pulse time but still no cigar.

esphome:
  name: waste_tank_esp32
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "xxx"
  password: "xxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Waste Tank Esp32"
    password: "xxx"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "xxx"

ota:
  password: "xxx"

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO2
    echo_pin: GPIO4
    name: "depth"
    update_interval: 2s
    pulse_time: 20us

Anything else I should try? I feed it with 5V like it says on the spec. Does the trigger have to ve 5V as-well? Thanks for any help!

Fixed, the code was not quoted correctly.

Please format your code correctly.

1 Like

Not sure why it ended up like that…

esphome:
  name: waste_tank_esp32
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "xxx"
  password: "xxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Waste Tank Esp32"
    password: "xxx"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "xxx"

ota:
  password: "xxx"

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO2
    echo_pin: GPIO4
    name: "depth"
    update_interval: 2s
    pulse_time: 20us

Are you doing any level shifting on the echo PIN?

The SR04 requires a 5V supply.

3.3V is enough to trigger the measurement to start.

But, the echo pin will pulse 5V into GPi04. It is only rated for 3.3V.

Connect a 4K7 resistor to the SR04 echo pin. Connect a 9K1 resistor from the other end of the 4K7 to ground. The junction of the two resistors goes to GPIO4.

SR04 Echo out> ---------[ 4K7 ]---------â—Ź--------[ 9K1 ]------------ GND
                                        |
                                        |
                                      GPIO4

Fingers crossed you have not burnt out the GPIO.

Ah thanks, good tip. I have more ESPs in case. Will do another test. :+1:

I have tried with the resistance to shift the level. I measured with a multimeter and it was down to 3,1V so that should be working well, but still it does not work. Tried to configure the HC-SR04 with the same setup and that works just perfect. So the GPIOs seems to be working fine. Also, just to be 100% sure I replaced the ESP with a new one and I get the same results.

But looking in the data of the sensor I find this.

Does this make a difference? For my eyes it seems that it has a delay on T2 that might be troublesome? Am I wrong?

I don’t understand what you are saying here. Is it working or not?

Does what make a difference?

Yes the HC-SR04 is working, but I am trying the waterprotected version, which is supposed to be 100% compatible. The HC-SR04 is not water protected at all. But I suspect that the sensor I use is not 100% compatible … therefor I link the picture above.

Here is the IP65 sensor:

And this is the Arduino code for the PWM version (which is supposed to be 100% compatible… again, I doubt it).

So sadly it does not work for my application (measurement of waste water inside a tank).

Just to be 100% clear. I have tried the HC-SR04 and the DYPA02YY sensor on the same ESP (one at a time) with the same code. Both with 3,3V and with 5V power supply. The HC-SR04 works fine, but not the DYPA02YY. So what I am looking for is to try and debug the difference of these sensors…

Right! Now I’m with you. I’ll have a look at the two data sheets after dinner.

1 Like

Looks to be compatible. Which pin are you using as Trigger, Tx or Rx?

I use D2 as trigger. Connected to the third pin on the ultrasonic sensor. The last wire connected via the resistor to give 3V back to D4.

Yeah, that’s not what I meant. The sealed transducer has two data pins, Tx and Rx. Which one are you using as the trigger?

not sure if it may help you, but I’ll share.
I’ve been using this sensor with a wemosd1 without any resistors for about 2 years with tasmota.
The tasmota sensors.bin already include this sensor.
just pick your pins, connect and thats it.

menu

1 Like

In the code above they list all the pins in the same order as they are on the connector. I use the third pin as trigger and the fourth as echo.

Is this the same IP65 one that I am using? Or are you using the SR04?