IR-08H sensor integration with ESPHome

Hello All,

I have gotten my hands on a IR-08H infared object sensor and I was going to use it to detect when my garage door is up/down.

After doing a bit of research on the sensor itself I have found that it needs to be enabled for around 1ms (using enable pin) in which time I need to read its binary output on the out pin. I have removed the jumper on the board.

Currently I have EN, VCC, OUT and GND connected to D4, +5V, D14, and GND respectively on my ESP32.

My issue: I cannot for the life of me get it to provide a consistent reading. It either flickers the binary sensor on and off repeatedly or does nothing at all. Can anyone provide assistance in adding such as sensor? I don’t need high refresh rates (once every 10 seconds is plenty), but I want it to do all the timing/checks on the esp32, not via HA automations.

I have my code pasted below (Yes I know it is a monstrosity):

output:
  - platform: gpio
    pin: GPIO4    #D4
    id: IRSensor_enable

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO14    #D14
      inverted: false
      mode:
        input: true
        pullup: true
    name: IRSensor_DIN
    device_class: motion
    filters:
      - delayed_off: 2s

time:
  - platform: sntp
    on_time:
      - seconds: /1
        then:
        - output.turn_on: IRSensor_enable
        - delay: 1ms
        - output.turn_off: IRSensor_enable

Thanks!

Have you tried this Ultrasonic Distance Sensor — ESPHome

First of all, if you power it from 5V you get 5v output, not good for esp32.
You should operate it like this:
Write enable pin high
wait 1ms
read output pin
write enable low

Don’t use pullups

I did try that earlier this week, but have since realised I was using a strapping pin.
I just tried it again and had a lot more success, but it was still unreliable. This time i got outputs with surprisingly accurate distance ~25% of the time. Every other time it returned nan. Any idea how to possibly fix this?
EDIT
This is a snippet of an output using the UDS code linked. it polls every 10s. I also tested on default 60s, same issues. Was left in a static position around 50cm from a fabric curtain.

[21:00:43][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:00:53][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:00:53][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:01:03][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:01:03][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:01:13][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:01:13][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:01:23][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:01:23][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:01:33][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:01:33][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:01:43][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.099 m
[21:01:43][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.09930 m with 2 decimals of accuracy
[21:01:53][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.097 m
[21:01:53][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.09673 m with 2 decimals of accuracy
[21:02:03][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.092 m
[21:02:03][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.09210 m with 2 decimals of accuracy
[21:02:13][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.333 m
[21:02:13][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.33271 m with 2 decimals of accuracy
[21:02:23][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:02:23][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:02:33][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:02:33][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:02:43][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.018 m
[21:02:43][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.01801 m with 2 decimals of accuracy
[21:02:53][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:02:53][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:03:03][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:03:03][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:03:13][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.079 m
[21:03:13][D][sensor:094]: 'Ultrasonic Sensor': Sending state 0.07906 m with 2 decimals of accuracy
[21:03:23][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:03:23][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy
[21:03:33][D][ultrasonic.sensor:036]: 'Ultrasonic Sensor' - Distance measurement timed out!
[21:03:33][D][sensor:094]: 'Ultrasonic Sensor': Sending state nan m with 2 decimals of accuracy

Apologies, I am a bit new to ESP Home. I am a bit unsure how to do this. is there a native way to do it via sensors/timers as I did in my original post, or would this be something I should do as custom code?
With the 5v output, would you suggest I add a resistor between the out pin to protect the ESP or is there a better alternative?
For reference, I have been primarily following this guide as to how the sensor operates. http://irsensor.wizecode.com/

No, you should power your IR module from +3.3V pin! Resistor doesn’t protect your input pin.
Your refence looks good.
Remove that pullup.
You can read your sensor in your time component between “delay 1ms” and “output.turn_off”

Amazing! Thankyou! That basically did the trick. It is instantly detecting every time I move my had close and is doing so 100% reliably. It then turns off in the device log after 5 seconds of no detected objects.
The only issue left is that it does not remain triggered when an “object” remains within proximity. Even the IR-08H devices onboard LED doesn’t flash for a static object like it would if it was moving. I hope that makes sense!

See my updated code below:

output:
  - platform: gpio
    pin: GPIO4    #D4
    id: IRSensor_enable

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO14    #D14
      inverted: true
      mode:
        input: true
    name: IRSensor_DIN
    device_class: motion
    filters:
      - delayed_off: 5s

time:
  - platform: sntp
    on_time:
      - seconds: /1
        then:
        - output.turn_on: IRSensor_enable
        - delay: 1ms
        - output.turn_off: IRSensor_enable

EDIT:
Just to make it clear, instead of reading while the enable pin is active, I am always reading. By default when enable is LOW, out is LOW, if it goes high it instantly turns on in HA. If it is low for more than 5 seconds, it then updates to HA as off. This is mostly fine for my needs, but it means I cannot currently account for false positives (only false negatives as it currently does)

I am unsure how I would force the binary sensor to update only during this 1ms window (and ignore inputs outside of it). If you are able to provide a snippet of code or a link for this it would be appreciated.

Edit 2: Fixed the static detection issues. Just needed to adjust R6 potentiometer on the IR-08H so it remained active when facing a static object. Technically everything works as needed right now, but would love to know a way to do the update between the enable and disable actions as mentioned in my last edit. Marking as solved. Thanks a heap @Karosm!

Nice to hear.
There are many ways of doing it.
I think easiest would be replacing your sensor with Template binary sensor
and in time component, read gpio after 1ms delay and then update the sensor.