Is it ok to use this sensor with esphome in 3-wire mode?
I’m thinking it might be ok, having read this paragraph in an online article:
In 3-Wire mode the single I/O pin is used as both an input and as an output. This is possible because there is never a time when both the input and output are being used. By eliminating one I/O pin requirement we can save a connection to our Arduino and use it for something else. It also is useful when using a chip like the ATtiny85 which has a limited number of I/O pins.
My intended example config for using it this way:
sensor:
- platform: ultrasonic
name: "Ultrasonic Sensor"
# use the same pin for the trigger and echo
trigger_pin: 26
echo_pin: 26
I’m not sure what the esphome code looks like and haven’t experimented with this.
Then, if I may also ask a second question: What is the unit provided by the sensor? It’s not quite clear to me from the docs whether it’s m or cm.
Hi all,
Since with ESPHome 2025.02 update removed the custom platform that would support the 3-pin ultrasonic configured discussed in this topic, have any of you found a solution to change the code to support new ESPHome requirements?
Just use the normal 4-wire solution, if you can. I never in the end tried the 3-wire solution. My question was really a matter of curiosity and not necessity.
I tried to trace the setup() to see whether the pin directions are configured beforehand, but I just end up at this virtual function (doesn’t look like it’s overridden by the ultrasonic component).
Note the comment:
/** Where the component's initialization should happen.
*
* Analogous to Arduino's setup(). This method is guaranteed to only be called once.
* Defaults to doing nothing.
*/
In Arduino world, pins default to input pins. It’s not clear to me what ESPHome does.
In the product page you linked, one can see how they change the mode of the pin – ESPHome would need to do the same and I don’t see it in the ultrasonic component’s code.
So, all I can really say is: Try it. If it works, great. If it doesn’t, it’s probably because the pin directions are fixed, meaning that the config for a single pin will be conflicting.
UPDATE:
The trick might be to get the pin setup right. In the ESPHome config, you can specify more pin options according to the pin schema. Setting it to INPUT_OUTPUT_OPEN_DRAIN might be the right option here. Also see this post. If all else fails, you might be able to implement this using custom lambdas. Also note some caveats here and an example here, as well as this one.
[19:48:02][D][ultrasonic.sensor:036]: 'DistanceFuel' - Distance measurement timed out!
[19:48:02][D][sensor:094]: 'DistanceFuel': Sending state nan m with 2 decimals of accuracy
I’ll try other combinations of options, but it’s a bit random.
I don’t know. It might depend on how you use ESPHome: In a Python env separate to HA, or on your HA installation (via an add-on, I think). It will be tricky though, because I don’t think that code gets recompiled when you build a config for a device. It gets done when you install ESPHome.
Creating an external component would be a more natural and maintainable way. In that case, copy the Ultrasonic code and modify it as needed. (I haven’t used this myself; I only know about this.)