Need help adjusting Ultrasonic Sensor based on air temperature

Hi There!
I recently built a snow depth sensor using an esp8266 and an ultrasonic sensor HC-SR04
I tested it out in the house and it works very well with reasonable accuracy. However, I know that sound moves at different speeds at different temperatures. So of course as soon as I put it outside and tested in a montana winter, the output was much different than the actual measurement.

Currently I have no temperature sensor connected to the esp8266, but I do know the temperature in the yard from a sensor in HA.

My plan was to make the conversion with a template sensor in HA, but not sure of the math.
I found this from someone doing this with arduino, but it seems they are using the temperature adjustment to find the adjusted speed of sound whereas I was trying to adjust the distance measured after the fact.
I assume the ultrasonic sensor is using the 0.034 speed of sound in centimeters per microsecond based at ~14 C.

So here’s the question:
If I have a measurement in cm coming in to HA from and esphome device, is there a template that could be used to adjust it based on a temperature sensor?

OR

If I have to add a temperature sensor to the esphome device (which I’m happy to do), how would I make this adjustment in esphome?

OR

is there a different way to do this with an ultrasonic sensor that I’m not seeing?

For what it’s worth, I already had the code working to convert to inches and subtract from the known height to get “snow depth.” Was working great in the house when testing with various objects.

Thanks in advance!

1 Like

Either suggestion will work, and you don’t need to add a temp sensor to the esp in the second scenario. If you already have the outside temp in HA you can get it in esphome via this component: Home Assistant Sensor — ESPHome

Do you know how to adjust the distance based on temperature? If you do, give us a clue!

Thank you for the info. I didn’t realize you could pull an ha sensor into esphome. So that solves that part.

I don’t. That’s actually the main question although that must not have been clear.
I’ll continue to fiddle but hopefully someone can help out with that.

How much of an error are you seeing?

Sensor is 18” over the base and overnight saw it at 1/2” off.

If you know what the temperature is and you know what the error is you can set up a graph of error vs temperature.

then you can calculate a line of best fit equation for that graph and then you should be able to use that equation in the correction for the actual reading vs temperature.

it will be way easier if you can get the equation for the line of best fit as close to a straight line as possible. Then you can just use an “Actual +/- (error/degree)” correction.

Yea I think now that I know I can bring in the outdoor temperature from ha without adding a temperature sensor to this esphome device, I’ll probably spend some time doing exactly this.
I was hoping there would just be some equation or lambda that was already known to just kind of take care of it for me. haha! No such luck!

Thank you for the suggestions.
I’ll keep this updated as I continue…

1 Like

I have found this HC-SR04 and the temperature - Let's Control It and Ultrasonic Sensors Knowledge Part 4. (different sensor).

Oh awesome. I’ll check that out. Gives me another place to start

Welp… the whole thing is pretty much moot at this point.

Got some snow this evening and i decided to see what my mustangs looked like. Once there was about 1/2” accumulation, something i didn’t take into account is that the snow seems to reflect the signal oddly enough that the echo is never received.

So i may try this out over a shorter distances: 4”, 6” and 12” instead of 18” to see if that helps. If it doesn’t work from at least a height of 8”, it kind of defeats the purpose for me as we often get over 6” at a time.

I’ll report back

Maybe another solution is called for? Maybe optical like this VL53L0X Time Of Flight Distance Sensor — ESPHome

That’s a good idea. I’ll have to try that one out.
Tried a height of 9" and with 1/2" of snow and no luck.

Get a stick. Place QR codes on it, every inch or two. Stick it in the ground and scan the QR codes with a camera. Whichever ones can’t be seen tells you how far up the snow has come.

Haha. Currently i just have a stick with the inches on it. Was just excited about this as a project for my son and i.

Building the apparatus was a fun project for the two of us. Might have to repurpose this whole project as a vehicle presence sensor for the garage or something.

I use one as a garage door position sensor since I don’t have a smart garage door opener…well I technically do but MyQ sucks for reliability…so I went the DIY route instead.

This works great for that use even without any temperature compensation (unheated garage) since it just needs a range instead of a precise value.

Nice!

I have a diy opener situation with two reed sensors for open and closed states.

I was actually thinking about using this for presence of my garage and recycling cans.

I have some WLED notifier lights for garbage and recycling days and this would work well to tell me if the can(s) had already been taken out.

So for your door, do you just have a range of distances that mean the garage is closed and the open if it times out?

The implementation I have seen took into account 3 distances measured from the ceiling.

longest - the door is closed and there is no car.
Medium - the door is closed and there is a Car.
Shortest - the door is open.

This is for a tilt door.

The sensor is above the door just before its furthest point of open travel and I have the sensor set so that if it reads less than 38 cm (~15in) the door is open.

This makes a lot of sense.
I might use this instead of my two sensors situation.
I like having the partial state on the door because we use keep our door cracked occasionally and it’s nice to see the state between open and closed.

I may see if there’s a spot i can Install this sensor to get all of those states but also a partially open state. Obviously if it’s partially open I’ll have to use an assumed state of the vehicle (if vehicle was there, and door is now partial, then vehicle is probably still there….)

I like this. Plus it will give me a new door sensor that can be used for my refrigerator now…,

There is a recent thread about someone who put a rotation counter on their garage door motor, the number of rotations is directly related to the door position. In that way he knows exactly where it is in the open/close cycle.

EDIT: here you are State Aware Garage Door