I have been using a sonic sensor (HC-SR04) to measure/alert how full a water softener salt tank is but being inside the salt tank they were corroding and stopped working every few months and needed to be replaced. I naively bought this sensor (the PWM version) thinking is would be a drop in replacement for the HC-SR04, alas it is not.
Could it be as simple as an āintervalā sensor and a binary_sensor?
The interval would toggle RX every couple seconds, and also store millis() in a global as it does so.
The binary sensor would be wired to TX and has an on_release automation that subtracts the stored millis in the global from millis(), thus telling you the echo-time, from which you can compute the distance.
Agree. I looked over the code in Arduinoās āNewPingā library which seems to work with this device.
Could be implemented in ESPhome using a couple lambdaās, Iāll wager.
Are you using the Arduino āpingā sketch with that device? Any thoughts on if esphome has a similar feature? I was not able to find anything to accomplish the same thing. Sorry, long time Home Assistant user but pretty much a beginner at esphome.
Iām sure I am not close yet but here is what I have been able to come up with so far. Largest problem I have found so far is I cannot find a way to get the time with millisecond resolution. Any one have any ideas on how to get that?
Code so far (wont work as is but posing anyways to see if anyone thinks I am already heading down the wrong path):
Slight update. I was able to get the following code compiled, had to comment out the important parts until I can figure how to get milliseconds.
I do get āpwm output is runningā and āpwm output is stoppedā in the logs but I never get āD2 Received State Changeā so Im not sure the D3 pin ever actually sends a signal through the sensor device.
I am curious why you commented-out the part where it sets send_millisecond, since it needs that value in order to do the math when it receives an echo.
Also, you might want to store that send_millisecond value after you toggle the TX line and its included delay, since the signal may not actually get sent until the end of that cycle, and you donāt want to include your own delay in the turnaround time. (if the signal is sent at the beginning of the toggle, then itās correct as you first had it, storing at the beginning).
Shortcut: instead of int(time({get millisecond})) you can just say āmillis()ā which is ānowā in system-clock millisecond-time.
Ok, this is some great advice! Getting closer with the following code however I never get āD2 Received State Changeā in the log so echo_receive_pin is never receiving a state change. Does it need to turn on a listening mode or something like that? Also likely I will need to change echo_receive_pin from on_state trigger to something that only triggers on one state otherwise it likely will reset diff_millisecond again after the cycle.
You should be fine with it as it is, no need to change the lambdas just yet.
And you may be onto something about pin D2 and what it expects and how itās configured.
Take a look at the various options under Pin Schema (link below), and see if perhaps it needs, or doesnāt need, a pullup/pulldown set, inverted, etc.
Because itās connected to an active-logic device, you probably donāt want any pullup or pulldown.
And one might expect that setting a sensor on GPIO would automatically put the pin in receive mode, it might not hurt to specify that explicitly.
I set D2 to āinput: trueā and D3 to āoutput:trueā and tried every combination of inverted for both pins and got the same results, also tried commenting āoutput.turn_off: pwm_outputā just to see if I could ever get a state change on echo_receive_pin and I never do. I also tried setting the output pin to āopen_drain: trueā and still nothing.
Current code is below but I still get no indication that there is ever a state change on the input pin.
I think someone above said there was but in effort to not add an additional layer (already using several other esphome devices), wanting to learn esphome better, and in the spirit of just trying, I wanted to see if it is possible in esphome before giving up.
I think with glyndonās help we have gotten pretty close. Iām sure the answer is somewhere in the spec sheet posted above but it is either worded oddly or it is over my head and I am missing something.