Introduction

An ultrasonic or SONAR sensor is an electrical device that determines the distance between an object in front of it and itself by emitting small ultrasonic waves and using the time required for the waves to reflect off of the object and return to the sensor to determine the distance (Burnett, 2020). This method of determining the distance between the sensor and an object has proved to be very useful in regard to range-finding tasks over the years; however, there are a few flaws that are present within the sensor’s design (Gillespie, 2019). Because the speed of the sound waves is dependent on the medium by which they travel, along with the temperature of the environment, the accuracy of the ultrasonic sensor can vary depending on the environment where the sensor is collecting its measurements. Also, the ultrasonic waves that are emitted from the sensor have to be reflected off of a mostly flat, large surface; otherwise, the waves will not reflect back to the sensor correctly (Gillespie, 2019). According to several manufacturer datasheets, at normal room temperature (70°F) the maximum error that will occur in the HC-SR04 ultrasonic sensor’s measurements is approximately 3 mm (How HC-SR04 Ultrasonic Sensor Works & Interface It With Arduino, n.d.; Ultrasonic Distance Sensor (HC-SR04), n.d.). Throughout this paper, this error will be verified to determine if it is an accurate measurement by comparing it to a dataset of distance measurements that were collected from a personal HC-SR04 ultrasonic sensor through various statistical tests. There will also be a test to see if the accuracy of the sensor is affected by the distance between itself and the object that it is detecting.

Figure 1
Figure 1.Image of HC-SR04 Ultrasonic Sensor.

Method

As stated previously, this study was conducted to verify the error measurement in HC-SR04 ultrasonic sensors that have been provided by several datasheets. In order to perform the experiment, a personal ultrasonic sensor was used to collect distance measurements. This sensor is connected to an Elegoo Smart Car V-3.0+ Robot because it was the only ultrasonic sensor that was accessible at the time. The experiment was designed such that the robot was placed at an arbitrary distance away from a wall and this distance was measured by a tape measure.[1] From there, the ultrasonic sensor that is attached to the robot will emit sonic waves to determine how far the wall is away from the robot, and that distance will be displayed through an Arduino serial monitor. An image of this experiment set-up will be displayed below:

Figure 2
Figure 2.Setup of Range-Finding Experiment.

The data collected from the HC-SR04 ultrasonic sensor is sent to the Arduino Uno microcontroller and then transported to the serial monitor through a data cable. The serial monitor that displays the sensor’s distance measurements along with the Arduino sketch written for the ultrasonic sensor will be shown now:

Figure 3
Figure 3.Arduino Sketch for Ultrasonic Sensor.
Figure 4
Figure 4.Serial Monitor Displaying Sensor Data.

For each distance measurement that was taken, 16 different distance readings from the serial monitor were randomly selected and averaged in order to find the optimal range reading that the sensor was outputting. The sampling method of using 16 different sensor readings to find an average sensor reading was employed in order to minimize the noise present in the sensor. While the experiment was being conducted, the temperature of the area was kept at approximately 70°F in order to keep the speed of the sound waves emitted from the sensor at about the same velocity as the sound waves that were being emitted from the HC-SR04 ultrasonic sensor in the datasheets. This was done by setting the thermostat to exactly 70°F; however, while this was the temperature that the thermostat was set to, it is possible that the exact temperature in the room was slightly different due to insulation issues in the area – thus causing a slightly different speed of sound than what the manufacturers calculated. Assuming that the temperature in the area was either approximately 69°F or 71°F, then the speed of sound would be either 343.47ms or 344.12 ms – with the speed of sound at 70°F being 343.8 ms (Speed of Sound Calculator, n.d.). Using a few calculations of true error, a combined margin of error that the speed of sounds at 69°F and 71°F produce when compared to the speed of sound at 70°F is approximately 0.189. With the combined margin of error being less than a percent, we can assume that the data collected using our sensor is nearly accurate to the data that the manufacturers collected.

The breakdown of the results collected from this experimental process will begin with an analysis of the true error collected by the sensor’s distance measurements. This will be followed by an analysis of both the absolute error and absolute relative error of the distance measurements in order to compare the absolute error to the error stated by the HC-SR04 datasheets (~3 mm). The rationale behind also including the true error of the distance readings instead of using only the absolute error of the readings was to determine if the sensor’s true error was significantly different from zero. Analyzing the true error in the distance readings allows one to determine if the distance readings measured by the HC-SR04 sensor is significantly different from the actual distances between the objects and the sensor. The results of the study will also contain a two-sample test to determine if the distance by which the sensor readings are taking place has an effect on the error of the readings. The two samples that will be tested will be the fifteen shortest and fifteen longest distance readings from the sensor data that was collected. These samples will be averaged and then a statistical test will be employed in order to determine if the errors between the two samples are significantly different from each other.

Result

Table 1.Data Spreadsheet of Sensor Readings and True Error Residuals.
Distance (mm) Average Distance Reading (mm) Error (Expected – Observed)
215.9 208.825 7.075
304.8 304.73125 0.06875
457.2 447.96875 9.23125
609.6 610.79375 -1.19375
762 759.25625 2.74375
762 778.48125 -16.48125
914.4 912.8375 1.5625
1066.8 1059.09375 7.70625
1219.2 1211.25625 7.94375
1295.4 1301.15625 -5.75625
1371.6 1372.51875 -0.91875
1524 1518.81875 5.18125
1651 1658.99375 -7.99375
1651 1658.45625 -7.45625
1676.4 1670.5375 5.8625
1828.8 1829.81875 -1.01875
1981.2 1987.15 -5.95
2133.6 2131.64375 1.95625
2286 2291.75625 -5.75625
2438.4 2437.06875 1.33125
2540 2544.075 -4.075
2590.8 2594.28125 -3.48125
2743.2 2747.50625 -4.30625
2895.6 2898.0875 -2.4875
3048 3050.3 -2.3
3175 3180.55625 -5.55625
3200.4 3213.15 -12.75
3352.8 3361.54375 -8.74375
3505.2 3505.79375 -0.59375
3657.6 3659.09375 -1.49375

Note: Descriptive Statistics of True Error: Sample Size, n=30, Average True Error, x ̅=-1.58833 mm, Sample StDev, s=6.12003, Sample Var, s^2=37.45475, Range =25.7125 mm

Figure 5
Figure 5.Analysis of Sensor True Error Residuals.

Note: Five Number Summary: Minimum=-16.48125 mm, Q1=-5.70625 mm, Median=-1.34375 mm, Q3=1.85781 mm, Maximum=9.23125 mm

Figure 6
Figure 6.True Error from Sensor Readings.

As stated previously, the average distance readings in the spreadsheet were calculated by taking sixteen different readings that the sensor was outputting through the serial monitor. Thirty distance measurements were taken at different distances from the wall, and the distance error that was produced from the expected distance versus the observed distance from the sensor was recorded for each trial. Through observation of the datasheet above, one can see that the average true error, ¯x, is equal to 1.58833 mm – which is noticeably smaller than zero. This trend continues when observing the boxplot where the median of the true error is found to be 1.34375 mm. Both of these values can be considered significantly smaller than zero – indicating that the true error present in the sensor may be statistically different from zero. However, in order to find statistical evidence to support the claim, there must be various statistical tests conducted. The distribution of the collected true error data appears to be slightly skewed-left – as can be observed through the boxplot. However, when observing the histogram above, we can see that the distribution looks roughly normal. When performing a Shapiro-Wilk test for normality on the true error data, the data is shown to be approximately normal. Due to the data’s normality, the statistical tests that will be utilized below will be both a bootstrap and tconfidence interval to estimate the population true error, μ. The outcome of this test will be displayed below:

Table 2.Tests for Negligibility of True Error Data from Sensor Readings.
Parameter Estimation Technique Lower Bound – Upper Bound
95% t confidence Interval 3.8736<μ<0.6969
95% Bootstrap Interval 3.7585<μ<0.5448

Note: Let μ= Population True Error Average. Parameters collected from bootstrap interval were collected after 1,000,000 bootstrap iterations.

Because zero is contained within both of these intervals, we can conclude that the population true error is negligible from zero. Consequently, these tests suggest that there is not enough evidence to deem the distance readings from the HC-SR04 sensor to be statistically different from the actual distances measured. While based on the data provided we can determine that the population average of true error is not statistically different from zero, there is still a possibility that true error fluctuates as the distance between the sensor and the wall grows. Due to this, a series of other statistical tests must be conducted to determine the relationship between distance and true error, if present. The results of these tests will be shown below:

Figure 7
Figure 7.Test for Statistical Significance of True Error Regression Slope.

Note: Regression Line Equation: y ̂=-0.002388x+2.937079 95% Confidence Interval for β_1: -0.0042<β_1<-0.0006

As can be observed from the scatter plot and confidence interval above, there appears to be a generally negative trend in the relationship between the two variables, indicating that as the distance between the sensor and the wall increased, the sensor began to undermeasure the distance. While this may be the case, there may be data points that are skewing the relationship seen in the regression line. In order to obtain better insight into the relationship between the two variables, there must be more statistical tests conducted. The first statistical test will be a t-test of the slope of the regression line to determine if it statistically-significant from zero. The results from this test will be displayed below:

Let b1= Sample Slope of True Error Regression and β1= Population Slope of True Error Regression, ^SE= Standard Error of the True Error Regression Slope, and α=.05

Given: H0: β1=0

Test: H1:β1<0

Recall b1=0.002388, β1=0, and ^SE=0.001037

t=0.00238800.001037=2.30280

P=0.01

Because the p value calculated from the true error regression slope is less than the accepted level of significance at α=.05, there is considerable evidence that there may be a negative relationship present between the distance by which the sensor is measuring from the wall and the true error from 21.6 cm to 365.8 cm. However, as stated previously, there is a possibility that the presence of outliers affected the slope of the regression line. In order to further test the effect that distance has on the error, there must be a two-sample test between the fifteen longest and fifteen shortest distances to determine if there is a significant difference between the two true errors. The distribution of the true error is approximately normal; however, when split into two equal groups, the sample size, n, of each group will be equal to 15 individual distance measurements. Because of this small sample size, a bootstrap interval for the difference in two population means will be conducted to confirm whether a relationship is present between measured distance and true error. The results from this can be seen below:

Table 3.Test for Statistical Difference Between Short and Long Error Observations.
Shorter True Error Observations (mm) Longer True Error Observations (mm)
7.075 -1.01875
0.06875 -5.95
9.23125 1.95625
-1.19375 -5.75625
2.74375 1.33125
-16.48125 -4.075
1.5625 -3.48125
7.70625 -4.30625
7.94375 -2.4875
-5.75625 -2.3
-0.91875 -5.55625
5.18125 -12.75
-7.99375 -8.74375
-7.45625 -0.59375
5.8625 -1.49375

Note: Sample Difference of the Two Means, (x ̅_1-x ̅_2)=4.1867 mm

By observing the table above along with the provided descriptive statistics, one can see that there is a noticeable difference of means between the shorter distance measurements and the longer ones (>4 mm). The variation also surprisingly increases with the shorter measurements in comparison to the longer ones. However, that may be due to outliers present within the data. Nonetheless, a bootstrap interval between the difference of the two means must be executed to estimate where the population difference may be. The results of this bootstrap interval will be displayed below:

Table 4.Bootstrap Interval.
95% Bootstrap Interval for Difference of Population True Error Means, (μ1μ2) 0.3688<μ1μ2<7.7721
Point Estimate of Difference of Population True Error Means, ^(μ1μ2) 4.1873

Note: These statistics were collected after 1,000,000 bootstrap iterations.

Because zero cannot be found in the bootstrap interval, there is a significant amount of evidence that a relationship between the variability of the true error measurements and the distance measured from the wall exists. This may indicate that there actually is an underestimation in distance measured from the sensor as the length measured increases within the scope of the measurements used in this study. From here, the claim that the maximum absolute error in the HC-SR04 sensor is equal to 3 mm will now be tested. A data spreadsheet of the sensor readings along with its descriptive statistics will be shown below:

Table 5.Data Spreadsheet of Sensor Readings and Absolute Error Residuals.
Distance (mm) Average Distance Reading (mm) Error (Expected – Observed)
215.9 208.825 7.075
304.8 304.73125 0.06875
457.2 447.96875 9.23125
609.6 610.79375 1.19375
762 759.25625 2.74375
762 778.48125 16.48125
914.4 912.8375 1.5625
1066.8 1059.09375 7.70625
1219.2 1211.25625 7.94375
1295.4 1301.15625 5.75625
1371.6 1372.51875 0.91875
1524 1518.81875 5.18125
1651 1658.99375 7.99375
1651 1658.45625 7.45625
1676.4 1670.5375 5.8625
1828.8 1829.81875 1.01875
1981.2 1987.15 5.95
2133.6 2131.64375 1.95625
2286 2291.75625 5.75625
2438.4 2437.06875 1.33125
2540 2544.075 4.075
2590.8 2594.28125 3.48125
2743.2 2747.50625 4.30625
2895.6 2898.0875 2.4875
3048 3050.3 2.3
3175 3180.55625 5.55625
3200.4 3213.15 12.75
3352.8 3361.54375 8.74375
3505.2 3505.79375 0.59375
3657.6 3659.09375 1.49375

Note: Descriptive Statistics of Absolute Error, Sample Size, n=30, Average Absolute Error, x ̅=4.96583 mm, Sample StDev, s=3.81507, Sample Var, s^2=14.55472, Range =16.4125 mm

At first observations of this datasheet, the average absolute error, ¯x, looks significantly larger than the manufacturer’s claimed error of ~3 mm. In fact, the percent error between the two is approximately over 66%! However, when analyzing the dataset further, we can observe a boxplot and histogram that will describe the distribution of the data further:

Figure 8
Figure 8.Analysis of Sensor Absolute Error Residuals.

Note: Five Number Summary: Minimum=0.06875 mm, Q1=1.66094 mm, Median=4.74375 mm, Q3=7.36094 mm, Maximum=12.750 mm

Figure 9
Figure 9.Absolute Error from Sensor Readings.

By observing this boxplot and histogram, we can see that the distribution of the plot looks approximately skewed-right with a median equaling 4.74375 mm. With closer analysis of this data’s distribution, one can discover that the distribution of this data resembles a folded normal distribution. A folded normal distribution is a generalization of the half-normal distribution where its random variable can be represented as Y=|X| due to its distribution essentially being the absolute value of a normal distribution (Glen, 2015). This distribution shape arises specifically in data that is not concerned with its direction but only its magnitude, and because our distribution contains the absolute value of the error readings, the distribution of the histogram above is more than likely a folded normal distribution (Glen, 2015). With the median being still being significantly far away from the expected error value of ~3 mm, there is a strong possibility that the average error, ¯x, was not affected by the presence of outliers, and that the average error may actually be significantly larger than the claimed error average. Nonetheless, in order to determine if the collected error dataset is statistically different from the manufacturer’s claimed error, both a bootstrap and t confidence interval for the population absolute error average must be utilized. This test’s results will be displayed below:

Table 6.Test for Collected Error Data vs Claimed Error Data.
Parameter Estimation Technique Lower Bound – Upper Bound
95% t confidence Interval 3.5413<μ<6.3904
95% Bootstrap Interval 3.6892<μ<6.3667

Note: Let μ= Population Absolute Error Average. Parameters collected from bootstrap interval were collected after 1,000,000 bootstrap iterations.

Because the claimed population error mean (3 mm) cannot be found within either the bootstrap interval nor the confidence interval, there is not enough evidence that the sample error mean and the manufacturer’s claimed error mean are equivalent. This means that the difference between the two means is statistically large enough to be deemed significant – which may provide evidence that the population absolute error mean claimed by the manufacturers could be incorrect. In fact, according to this data, the absolute error present in distance readings may be greater than 3 mm for all sensors. To further explore this error, a study of the absolute relative error will now be conducted to statistically determine how far away the sensor’s error is from its expected value, 3 mm. The results from this study will now be displayed:

Table 7.Data Spreadsheet of Sensor Readings and Absolute Relative Error.
Distance (mm) Average Distance Reading (mm) Absolute Relative Error, εt|%
215.9 208.825 135.8333333
304.8 304.73125 97.70833333
457.2 447.96875 207.7083333
609.6 610.79375 60.20833333
762 759.25625 8.541666667
762 778.48125 449.375
914.4 912.8375 47.91666667
1066.8 1059.09375 156.875
1219.2 1211.25625 164.7916667
1295.4 1301.15625 91.875
1371.6 1372.51875 69.375
1524 1518.81875 72.70833333
1651 1658.99375 166.4583333
1651 1658.45625 148.5416667
1676.4 1670.5375 95.41666667
1828.8 1829.81875 66.04166667
1981.2 1987.15 98.33333333
2133.6 2131.64375 34.79166667
2286 2291.75625 91.875
2438.4 2437.06875 55.625
2540 2544.075 35.83333333
2590.8 2594.28125 16.04166667
2743.2 2747.50625 43.54166667
2895.6 2898.0875 17.08333333
3048 3050.3 23.33333333
3175 3180.55625 85.20833333
3200.4 3213.15 325
3352.8 3361.54375 191.4583333
3505.2 3505.79375 80.20833333
3657.6 3659.09375 50.20833333

Note: Each Absolute Relative Error reading was calculated through the following formula: |ε_t |%=|(observed-expected)/expected|*100%, where expected = 3 mm.

Through initial observations, one can see that the average relative error is over 106% with a substantial amount of variation present between the distance readings. Based on these numbers, it begins to become clear that the absolute error collected from the study and the error collected by several manufacturing websites are significantly different. By observing both a boxplot and histogram of this data, we can take a deeper look into its distribution and estimate how far away the population absolute relative error is away from zero. The boxplot and histogram derived from this data will be displayed below:

Figure 10
Figure 10.Absolute Relative Error Residuals (%)
Figure 11
Figure 11.Absolute Relative Error from Sensor Readings

By analyzing both the boxplot and histogram, we can see that the absolute relative error has a skewed-right distribution – much like the absolute error distribution from previously. Much like the absolute error that was analyzed previously, the distribution observed from this histogram seems to be a folded normal distribution in shape. We can also easily see that the minimum value in both the boxplot and histogram is 8.5417%. Because of this, the likelihood that our sensor’s absolute relative error average is equivalent to zero percent is highly unlikely. However, there still must be more statistical tests conducted to prove that, without uncertainty, the population absolute relative error average is statistically different from zero. The results from these tests can be seen below:

Table 8.Let |εt|μ= Population Absolute Relative Error Average.
Parameter Estimation Technique Lower Bound – Upper Bound
95% t confidence Interval 70.973%<|εt|μ<141.56%
95% Bootstrap Interval 76.201%<|εt|μ<142.35%

Note: Parameters collected from bootstrap interval were collected after 1,000,000 bootstrap iterations.

Because zero percent clearly cannot be found within either interval, we can conclude that there is significant evidence that the population absolute error average is much different from what is being shown in several datasheets. Much like the previous study on absolute error, these statistics show that the error is significantly larger than what was expected from the manufacturing sheets.

Conclusion

Through this experiment, the HC-SR04 ultrasonic sensor manufacturer’s claimed distance error was proved to possibly be invalid and there may be an additional form of error present as the distance between the sensor and an object become farther from each other. A possible source of error that was present in this experiment included human error when measuring the distance from the wall to the sensor using a tape measure. While the robot and the tape measure were aligned as close as possible on every trial, it is possible that they were slightly-misaligned on certain trials – introducing more error to each trial. Throughout the duration of collecting sensor data, there was only one sensor that was used to collect all of the distance readings. While unlikely, it is possible that the sensor used in this experiment was faulty and produced inaccurate results because of it. Another factor that may have produced inaccurate results from the statistical tests could be looked at from the fact that the sample size of the data used was only 30 samples. For future works, there should be a more accurate way of determining the actual distance between the sensor and an object – instead of a tape measure. There should also be a more-precise way of aligning the robot when taking distance readings so that there would not be any additional error in that regard. Multiple HC-SR04 ultrasonic sensors should be used when taking distance readings so that any additional error present in one sensor can be compensated for by other sensor readings. Most importantly, there should be a larger sample taken in order to obtain more accurate parameters to estimate the population error for the HC-SR04 ultrasonic sensor.


  1. The tape measure’s measuring units are only in inches; however, the error given in manufacture datasheets was given in units of millimeters. To counter this, dimensional analysis was utilized to convert the measurements from the tape measure from inches into millimeters.