Ultrasonic UART sensor

Hi im using SR04

Yeah he has the SR404 working it’s the other water resistant one that’s supposed to be compatible that is giving the issue.

So pin 3 should be the trigger and pin 4 is the echo pin.

Yes, that’s the way I configure it. What I was wondering is if the echo time could be too long, or something similar? Maybe I need a longer trigger or something similar.

No, the trigger times for both are specd at 10us minimum and the conversion factor for the returned pulse length is identical.

I have also sent the seller on Ebay a question about this item to see if he can assist. Could the sensor be damaged you think? Is there an easy way to examine what is receive in on the echo GPIO?

Do you have access to an oscilloscope?

I think my friend might have one. Let me ask him.

He says he has a USB scop. Should work he says. :slight_smile:

Two channels would be good.

Ch 1: connected to the trigger pin.
Ch 2: connected to the echo pin.
Scope trigger on rising edge of Ch1 anywhere between 1 and 2 volts.

You should see the same sort of thing as in the datasheet pulse timing diagram.

Yes good. And of course power it with 5V + ground. I will give it to my friend for diagnostics.

Tried something different. Installed the code from the manufacturer on Arduino IDE, and that works just fine. Same PINs. GPIO4 and GPIO5. Still the same ESP32.

Exactly the same setup as I had with ESPHome. Something crazy is going on.

Do you know if the ultrasonic sensor use the same (-ish) code to measure in the ESPHome sensor?

void loop()
{
  // The sensor is triggered by a falling edge of a HIGH pulse that 
  // is more than 60 microseconds in duration.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(5);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(100);
  digitalWrite(TRIGGER_PIN, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  // If no object detected, fixed pulse width of 35ms is sent
  // by the sensor.
  pinMode(PWM_OUTPUT_PIN, INPUT);
  duration = pulseIn(PWM_OUTPUT_PIN, HIGH);
 
  // Convert the pulse width duration into a distance
  distance = duration;
  distance = distance / 58;
  
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(250);
}

Thanks. I will play with this a bit and see.

Just got a feedback from the ebay seller.

You cannot use HCSR04 code directly for this sensor.

There are subtle differences.

Please check the github link in the eBay description of this item.

I have provided full PWM code along with connection diagram.

So I guess that answers it. :slight_smile: I have to use my own code. Will see if I can make that somehow.

After heaps of debugging we found it. The default timeout is very wrong. I ended up with this YAML

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

With no timeout setting this is the default. (extra debugging added)

This is with timeout set to 20m.

The timeout setting and default has to be very wrong, or at lest for this sensor. Default of 5830us has to be low also for the SR04?

3 Likes

Hello corvy,
I’m trying to do the same thing.
Did you finally make it work with the resistors that tom_I suggested you to add or just by connecting the sensor to the ESP32 pins ?
Thanks :slight_smile:
Sébastien

Hello, I changed the timeout and added the resistors. Hope you make it work!

My build is finally completed. The sensor is fitted to a black water tank in my boat and it works just perfectly. :slight_smile: Thanks for the help and thanks to #third-party:esphome for a great product!

Screenshot 2020-07-28 10.10.35

1 Like

I ran into the same issue with the US-100 sensor (https://www.banggood.com/3pcs-US-100-Ultrasonic-Ranging-Module-with-Temperature-Compensated-Sensor-Dual-Mode-Serial-Port-p-1589413.html) - the fix was the same - setting timeout: 20m sorted it out for me. Putting this here so that others might find it quicker if they run into this.

thanks.