Ultrasonic sensor precision

I own an automatic cat feeder similar to this one:

I wanted to be able to monitor the level of cat food remaining and receive a notification when it’s running low. So, I installed a D1 Mini and an HC-SR04 ultrasonic sensor, which I attached to the lid of the feeder following this guide.

This is what the sensor displays in Home Assistant:

2 questions:

  1. Why is the line a bit squiggly? I believe this is due to the irregular shape of the cat food pieces, which makes it difficult for the sensor to measure accurately.

  2. Why does the sensor struggle with objects that are too close? When the feeder is completely full, the sensor tends to report distances much greater than expected, and sometimes it doesn’t report anything at all. Although the sensor’s range should be 2cm to 450cm, in practice, anything closer than 5cm seems to be hit or miss.

Try adding a moving median (suggest setting all values to 5) and seeing how much it cleans up after that.

1 Like

Thanks, going to try that.

  filters:
    - median:
        window_size: 5
        send_every: 5
        send_first_at: 5
1 Like

The issue was using the wrong pins on the board. Pins D3 and D4 require a resistor to function properly. Changed to pin D1 and D2, no weird spikes anymore.

1 Like