Skip to content
This repository was archived by the owner on Jan 24, 2026. It is now read-only.

Commit 3d21a12

Browse files
committed
fix: ultrasonic temperature compensated measuring #699
1 parent a02cd6b commit 3d21a12

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

components/ultrasonic/ultrasonic.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define PING_TIMEOUT 6000
4949
#define ROUNDTRIP_M 5800.0f
5050
#define ROUNDTRIP_CM 58
51-
#define SPEED_OF_SOUND_AT_0C_M_S 331.4 // Speed of sound in m/s at 0 degrees Celsius
51+
#define HALF_SPEED_OF_SOUND_AT_0C_M_S 165.7 // Half speed of sound in m/s at 0 degrees Celsius
5252

5353
#if HELPER_TARGET_IS_ESP32
5454
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
@@ -147,8 +147,8 @@ esp_err_t ultrasonic_measure_temp_compensated(const ultrasonic_sensor_t *dev, fl
147147
{
148148
CHECK_ARG(dev && distance);
149149

150-
// Calculate the speed of sound in m/us based on temperature
151-
float speed_of_sound = (SPEED_OF_SOUND_AT_0C_M_S + 0.6 * temperature_c) / 1000000; // Convert m/s to m/us
150+
// Calculate half (because of roundtrip) speed of sound in m/us based on temperature
151+
float speed_of_sound = (HALF_SPEED_OF_SOUND_AT_0C_M_S + 0.6 * temperature_c) / 1000000; // Convert m/s to m/us
152152

153153
uint32_t time_us;
154154
// Adjust max_time_us based on the recalculated speed of sound
@@ -163,8 +163,8 @@ esp_err_t ultrasonic_measure_cm_temp_compensated(const ultrasonic_sensor_t *dev,
163163
{
164164
CHECK_ARG(dev && distance);
165165

166-
// Calculate the speed of sound in cm/us based on temperature
167-
float speed_of_sound_cm_us = ((SPEED_OF_SOUND_AT_0C_M_S + 0.6 * temperature_c) * 100) / 1000000; // Convert m/s to cm/us
166+
// Calculate half (because of roundtrip) speed of sound in cm/us based on temperature
167+
float speed_of_sound_cm_us = ((HALF_SPEED_OF_SOUND_AT_0C_M_S + 0.6 * temperature_c) * 100) / 1000000; // Convert m/s to cm/us
168168

169169
uint32_t time_us;
170170
// Adjust max_time_us based on the recalculated speed of sound in cm

0 commit comments

Comments
 (0)