Low-cost ultrasonic sensors for in-field experimentation data collection

ABSTRACT Surface runoff monitoring is important for the sustainable management of global water resources. Obtaining a practical and inexpensive method for collecting data in the field can help to better understand surface runoff and its effects, necessary for the management of watersheds. This study sought to elaborate the calibration curves of the ultrasonic sensor due to temperature variability, verifying the inaccuracy of the distance between objects and the sensor, and determining the feasibility of using low-cost sensors in an in-loco experiment installed on Parshall flumes. The experiment was conducted on the Experimental Farm of the Federal University of Grande Dourados, Dourados, MS, Brazil. The data were collected by twelve HC-SR04 ultrasonic distance sensors , which were coupled to a data acquisition system composed of an expansion board connected to a Raspberry minicomputer. Sensor calibration using temperature data resulted in the error correction of ± 8.0 mm of distance reading. On the other hand, the R2 of the comparison curves between sensor and control system (laser distance meter and ruler in the flume) resulted in high values (above 0.95), showing the feasibility of its use and meeting the specifications for use in the field subject to weather conditions. This study demonstrates the performance of ultrasonic sensors as a potential for new application to evaluate surface runoff aiming to propose new runoff coefficients.


INTRODUCTION
The need to control the effects of surface runoff has driven the development of more efficient and cheaper methods for monitoring agricultural production (Meyer et al., 2019;Rodríguez-Robles et al., 2020).The sustainable management of water resources requires the estimation of the rainfall surplus and its actions on the Earth's surface, especially the negative consequences of water erosion (Mohammadifar et al., 2021), which has caused the transport of nutrients from the soils, resulting in changes in production capacity, in addition to river pollution (Martínez-Mena et al., 2020;Wang et al., 2021).
Although most works estimate surface runoff by models (Al-Juboori, 2022), some projects have been designed to determine surface runoff using empirical methods, whose accuracy will depend on the designer's experience.It is due to the need of using reference tables when choosing parameters related to surface characteristics (Erena;Worku, 2019).Nevertheless, the tables present the specificity of the drainage basins for which they were developed.Therefore, reference tables are generalist and may not cover specific cases, requiring further studies and updating of technical references (Lapides;Sytsma;Thompson, 2021;Young;Mcenroe;Rome, 2009).
Collection containers are one of the methods to carry out measurements in in-loco studies, being an expensive work because the precipitation is variable, implying the need for dynamic monitoring to obtain accumulated volumes for each event (Portocarrero;Andrade;Campos, 2017).Still, methods that involve real-time data collection can be advantageous in immediate and accurate decision-making (Sanches et al., 2022a).
The use of flumes, such as the Parshall flume, is also common in the measurement of surface runoff (Tiwari;Sihag, 2020), mainly coupled to level sensors.However, they need equipment for data collection and an energy source for more remote areas.For example, Abualfaraj et al. (2018) used Parshall flumes equipped with pressure transducers to measure the flow rate on green roofs and observed good results.
The emergence of affordable minicomputers and accessories for teaching programming and robotics has allowed carrying out experiments previously limited to a few institutions.Studies with low-cost sensors have already been applied in loco in automatic monitoring of phosphate and nitrite in agricultural aquatic environments (Lin et al., 2018), such as an intelligent soil moisture sensor (González-Teruel et al., 2019).Low-cost sonars have been used in experiments applied to plant growth monitoring (Trevisan et al., 2018) and automated irrigation systems (Rodríguez-Robles et al., 2020) with satisfactory results.
However, tests need to be carried out to determine the limitations of their use or interference caused by bad weather given the fragility of components (Kruger et al., 2016;Papa;Ponte, 2018).In the case of sensors, we can consider that every measurement  is actually a measurement of  =  + , in which  indicates measurement error (Barcelo-Ordinas et al., 2019).However, understanding how sensor error influences performance and its accuracy is essential (Placidi et al., 2020).
Many studies have already been using sonars to measure height directly in water bodies to monitor extreme high runoff events (Meyer et al., 2019;Panagopoulos et al., 2021) but they may present late warning as runoff may have already occurred on the surface.Knowing the runoff before reaching the rivers is essential to predict the results of major rainfall events in advance.Payero et al. (2021) used the Internet of Things (IoT) to develop a surface runoff measurement system utilizing H flumes and eTape liquid level sensor, including a water collection system for analyzing the impacts of runoff in soybean and cotton fields with and without coverages and obtained an R 2 = 0.99 with the calibration.Schallner et al. (2021) monitored runoff using low-cost UBeTube technology and concluded that the methodology, with proper calibration, could be used in pastures.
Therefore, the purpose of this study was to elaborate the calibration curves of the HC-SR04 ultrasonic sensor due to temperature variability, to confirm the inaccuracy of the distance between objects and the sensor, and to determine the feasibility of using low-cost sensors in an in-loco experiment installed on Parshall flumes.

Experimental site
The experiment was carried out in the unit located in the experimental area of FAECA (Experimental Farm) on the campus of the Federal University of Grande Dourados (UFGD), in Dourados, Mato Grosso do Sul, Brazil (22°24′18″ S, 54°99′47″ W, 437 m altitude).The materials were prepared in the Laboratory of Hydraulics, in the building of the School of Agricultural Sciences, Unit 2 of UFGD, also in Dourados, MS, Brazil.
According to the Köppen climate classification, the climate of the study area is Am, that is, a tropical monsoon climate (Alvares et al., 2013), with a dry winter, a mean annual temperature of 22.8 °C, and a mean annual precipitation of 1389 mm for the last 30  years , as shown in Figure 1, according to (Empresa Brasileira de Pesquisa Agropecuária -2022).
The experimental area consisted of twelve experimental plots of 4 m × 10 m, with a 4-m² downstream triangle, totaling 44 m².Six hillslopes have a gradient of 8.2%, whereas the remaining six have a gradient of 18.6%.
The plots of the higher slope were used from the natural land profile.The soil was cut to obtain the lowest slope aiming at minimal changes in the natural soil conditions.The plots were divided using guides made with the soil and the Parshall flumes with sensors were placed at the plot outlet (Figure 2 and 3).

Electronic system
The system used in the experiment was designed for using HC-SR04 ultrasonic sensor manufactured by OSEPP Electronics, installed in each Parshall flume, and an HC-SR04 ultrasonic sensor installed at 40 cm from a concrete slab that did not undergo reading variation due to rain, identified as a standard sensor for comparison purposes.A Texas Electronics Inc. TR-525M tipping bucket rain gauge was also installed.In addition, a HiLetgo HTU21D air temperature and humidity sensor was used.The data were stored on an SD card attached to a Raspberry Pi 3 minicomputer.The language used was Python, which is open-source and easy to program.
An expansion board for up to 20 devices was required to connect all the sensors to the Raspberry.The entire system was connected to a 12V battery powered by a photovoltaic board, as there is no power supply at the experimental site.A charge controller was installed not to damage the battery, limiting the voltage to 13.5V (Figure 4).
According to the manufacturers, the HC-SR04 sensors have a working range between 2.0 and 400.0 cm, with an accuracy of 0.3 cm and a field of vision of 15°.The HTU21D sensor has an operating range for temperature and humidity from −40 to 125 °C, with an accuracy of ±0.2 °C, being factory calibrated.The rain gauge has a resolution of 0.2 mm, an accuracy of 1.0% up to 50 mm per hour, a range of 700 mm per hour, and operating temperature limits from 0 to 70 °C.
Parshall flumes (Figure 5) have a rectangular cross section and feature a converging inlet section, a throat and a diverging outlet section, following the current NBR/ISO 9826:2008 standard.They were duly calibrated through the correlation of the water depth and their output flow rate, with a calibration curve according to Equation 1 from (Sanches et al., 2022b): where Q is the flow rate in m 3 h −1 and H is the water depth in cm.
The costs for implementing the experiment are detailed in Table 1, with a value of US$ 98.02 per monitored plot.

Data collection
The data were collected during an approximate 1-year experimental cycle (April 17, 2020, to June 23,  2021), used in the system adjustments and calibration.The area maintenance was carried out weekly to verify the functioning and cleaning of the flumes when required.
The installation height of each sensor was established by the mean of five readings using a Bosch GLM40 laser distance meter, with a 1.5-mm precision, according to the manufacturer.Forty readings were performed for eight well-defined distance variations between sensors and Parshall flume in four randomly chosen sensors (two from each slope group) to verify the sensor reading precision compared to height.
The distances were defined by obstacles with height variations of sensors relative to the flumes (Figure 6) to save water, as it depended on the supply of a water truck on site to carry out the verification readings during the minimum 80 minutes of collection.Overlapping 3-mm glass slides, with thickness calculated by the mean thickness at 3 points of the slide using a Mitutoyo ® 530-104b-10 8-inch universal Analog Caliper, were used.
The HC-SR04 sensor sends an ultrasonic wave when triggered, being able to signal when the sent pulse is received.The Raspberry records the time between  sending and receiving and the system calculates how far the sensor is from the object.The distance can be determined by Equation 2 and time by Equation 3, as the speed of sound propagating through the air is known (343.40 m s −1 at 20 °C), and the sound traveled twice the distance between the sensor and the object. (2) (3) where d is the distance traveled by sound in m, Δt is the pulse travel time in s, and c is the speed of sound in m s −1 , primarily adopted as 343.40 m s −1 .
Ciência e Agrotecnologia, 47:e013422, 2023 The system was programmed to measure five pulses per reading, recording their simple mean.The system performs a test limited to 40 µs of the response time before performing the reading, thus verifying the functioning between the transmitter and the transducer, as 50 µs is the limit of repetition of sensor readings, according to Zhmud et al. (2018).Faulty is logged after 1 second if this condition is not met, warning of sensor problems.The sensors were installed in two groups (according to slopes) on the expansion board to save system energy.Therefore, whenever one sensor presents faulty, there is an indication of instability in the group to which the respective sensor belongs.
The experiment was programmed to take one reading per hour.However, the readings would be taken every 15 seconds if there was a record on the rain gauge.The date, time, and temperature and relative humidity sensor readings were recorded along with the sensor readings.Readings ceased 10 minutes after the last rain record, totaling at least 40 readings per event and collections are returned every hour.

Calibration
Initially, the sound travel time (∆t) is calculated using Equation 3. Subsequently, Equation 4, used to correct the speed of sound relative to air temperature, was applied (Bartoszek et al., 2022).Then, Equation 5 was applied, thus obtaining the distance corrected by temperature.
to the air temperature in cm, correlation is the correlation between d sound and T, and T is the temperature in °C.

Data analysis
The data were separated by sensors to analyze the collected heights relative to the temperature.The distances read in Parshall flumes can vary on rainy days and, for this reason, only dry days were analyzed, not considering rainy days.The values were subjected to the distribution test, after which the correlation was calculated, and a linear regression curve was modeled for distance x temperature.
After calibration, the d T points were obtained (measured after all calibrations), the correlation was calculated, and a new linear regression curve was modeled for d T x temperature.Subsequently, distance (d) and calibrated distance (dT) values underwent descriptive analysis (mean, quartiles, standard deviation, and coefficient of variation), comparing them and verifying a decrease in error  by comparing the mean errors Equation 7, absolute mean errors Equation 8, and root mean square error Equation 9, as proposed by Legates and Mccabe (1999): where c is the speed of sound in m s −1 and T is the temperature in °C.
(5) in which d sound is the distance traveled by sound corrected in cm, Δt is the pulse travel time in s, and c is the corrected speed of sound in m s −1 .
Subsequently, the correlation of the corrected values between d sound and temperature was calculated, obtaining how much the temperature impacts the other system components.Thus, the following correction is applied to obtain the calibrated distance: (6) where d T is the calibrated height of the sensor relative to the temperature in cm, d sound is the corrected height relative (7) (8) (9) in which H control is the height obtained by the control system (ruler in the flume or laser distance) in cm, H sensor is the distance read by the sensor in cm, and n is the amount of data.
Finally, the height values obtained in the laboratory test and the field with the glass slides of the four flumes were analyzed to verify the reliability of the elaborated curves, according to the variation of the distance from obstacles, modeling a linear regression curve, calculating correlation, and obtaining R 2 , ME, MAE, and RSME.

Temperature
Figure 7 shows the air temperature variation collected by the HTU21D sensor after 1 year of the experiment, demonstrating a large temperature variation even in predominantly hot regions.Several temperature values close to 0 and 45 °C were observed in August, which has the lowest temperatures.The mean temperature obtained in the experimental period was 23.75 °C, very close to that observed by Embrapa (2022) in 2020 and 2021 for Dourados (23.61 and 23.40 °C, respectively), as shown in Figure 1.

Reference sensor (Sensor 13) x temperature
First, the IQR (interquartile range) statistical method is used to analyze outliers, so that outliers = value < (Q1−1.5IQR)or value > (Q3+1.5IQR),as well as Li et al. (2023).Of the 13232 readings, 287 data points were removed (2.16% outliers) and the remaining data are presented in Figure 8.The sensor was positioned to avoid abrupt changes in the distances read and failures may have been caused by interference from some animal, vegetation (Figure 9), or even read failure due to power supply below 3.6V, as observed by Komarizadehasl et al. (2022) that showed high sensibility this type sensors.
In a solar panel and battery system, voltage supply below 3.6V may occur when the battery is discharged or when there is an issue in the electrical system that hinders proper battery recharging (Chen et al., 2020).Furthermore, battery quality and age can also affect the system's ability to supply power.It is important to ensure that the electrical system is functioning correctly and that the battery is charged enough to supply the necessary energy to the system.Regularly monitoring the battery charge level and the efficiency of the electrical system can help prevent power supply below 3.6V and keep the system operating properly (Liu;Gao;Liu, 2022).
The values of uncorrected distances did not show a normal distribution but the Spearman correlation coefficient between distance and temperature resulted in r = −0.933(significant at α=0.05), demonstrating a hightemperature interference.After calibration, the values were compared with the known references values (installed heights) and no statistically significant differences were found (r=0.004 at α=0.05) (Figure 10).
The correlation coefficient −0.067 demonstrates that the calculated distances without the proper use of the speed of sound will be smaller as the temperature increases, as the sound moves faster (Kumar et al., 2020), that is, at 0.622 m s −1 at every 1 °C (Bartoszek et al., 2022), thus reducing the response time of the distance sensor.
The dT points were obtained after correcting the distance relative to the temperature, showing that the sensor-maintained precision despite the low cost, reducing the minimum and maximum values.The new equation (d T = 0.0001T+41.479)demonstrates that the new slope of the trend line is very close to zero with little temperature interference, resulting in values close to that of the installed height of 41.45 cm.
A descriptive statistical analysis was applied using electronic spreadsheets, with the data shown in Table 2.   Table 2 shows the fundamental importance of considering the variation of the speed of sound relative to the air temperature, which reached minimum and maximum values of 0 and 44.60 °C, respectively.Correction led to a reduction of up to 229.90% in the range of the obtained values, which represents 0.33 m 3 h −1 for every 1 cm in the Parshall flume flow, according to Equation 1. Sensor 13 showed constant values after correcting the total errors, keeping the values between 41.01 and 41.98 cm, with a mean of 41.48 cm.It represented a maximum error of ±5 mm, equal to those found by Kumar et al. (2020) with laboratory measurements.
The same procedure was applied to the other 12 sensors and the obtained values demonstrated similar behavior to the reference sensor (S13) (Table 3).However, Sensors 03 and 06 presented a maximum error of 8 mm, Sensors 02, 08, 10, and 11 showed a maximum error of 7 mm, and Sensors 01, 04, 05, 07, 09, and 12 presented a maximum error of 6 mm when comparing minimum and maximum values with the mean, as observed by Pereira et al. (2022) in an experiment carried out to analyze the water level in flow channels in the laboratory.
The experiment was carried out without any weather control, with the sensor installed only avoiding direct contact with rain, aiming to undergo other interferences present in agricultural activities, resulting in higher imprecision compared to laboratory experiments (Al-Agele; Jashami; Higgins, 2022).Table 4 shows the sensor installation height, means,  and standard deviations of the distances obtained after calibration compared to that expected, as well as ME, MAE, and RMSE, for each data set.RMSE values are close to that found by Kholopov (2015), with 3.2 mm for an inertial navigation system using HC-SR04 sensor, very close to the value established by the manufacturer (3 mm).

Sensors in Parshall flumes x height variation
The reliability of the heights read after the calibrations related to the ambient temperature were verified by laboratory and field tests carried out using the HC-SR04 sensor compared to readings performed by the ruler installed in the Parshall flume, with the linear regression shown in Figure 11.
The results of calibration presented high correlations, the R² = 0.99 showed good precision the values, corresponding already observed to Abreu et al. (2021), Therefore, the results calibrations showed values R² near the one with anothers sensors, whose calibrations were greater than 0.9 of the coefficients of determination (Sanches et al., 2020;2022b).Subsequently, a statistical analysis was performed using the obtained results, as shown in Table 5.
Figure 12 shows the relationship between calibrated distances and distances with laser tape.Can be observed that the HC-SR04 sensor has good precision with compared laser tape, as it presented a linear fit with a high coefficient of determination.It should be noted that laser measuring tapes have an accuracy of +/-1.5 mm.
Comparative sensors calibration is very common, in work conducted by Sanches et al. (2022a) a mechanical vacuum sensor was compared with an automated pressure transducer, showing that both showed good correlation as  with the present work, in which the results were for r² = 0.99 (Figure 12).Table 6 shows the values of mean error (ME), mean absolute error (MAE), root mean square error (RSME), Spearman's correlation coefficient (r), and coefficient of determination (R 2 ) for the tests performed in the laboratory and field.
The results were satisfactory, with the lowest r of 0.97 and R 2 =0.99.Similarly, MAE and RMSE values remained within the 3-mm range of factory error.The entire experiment was carried out in loco, and the deviations in the distances of Sensor 05 may be the result of interference of some insect or grass.Amorim et al. (2021) conducted a calibration test using the same sensor and observed good accuracy and low data dispersion, suggesting that the sensor may have high applicability in automation and robotics projects.Likewise, Pereira et al. (2022) had excellent results using the sensors in level flumes, with high accuracy up to readings of 20 cm.However, the present study showed good results when using distances up to 30 cm, as observed by Dusarlapudi et al. (2020).
Al-Agele, Jashami and Higgins (2022) used the same sensor model with fixed measurements of 0.5 m in the laboratory and found a maximum error of 3.2% in the mean, with a standard deviation of 3.3 cm, well above that found in the study.On the other hand, the HRXL-MaxSonar-WR ultrasonic sensor used in a river level measurement experiment showed a standard deviation of 0.13 cm (Panagopoulos et al., 2021), very close to that of 0.14 cm of Sensor 09, but at acquisition cost 10 times higher.The error should be considered relative to time, as fatigue can be representative in long-term experiments (Gandha;Santoso, 2020).Implementing a correction during data collection, such as the Kalman filter and linear prediction, may further reduce total errors (Liu, Kouguchi;Li, 2021).Similarly, Abreu et al. (2021) proposed changes in software and hardware, which produced an error reduction from 7% to 1% without affecting the low cost of the sensor.
Figure 13 shows the behavior of the S05 sensor with the variation of the water flow for 30 minutes, a field test carried out on 04/14/2021.2000 liters were released in plot T5, increasing and decreasing the flow, and the data collected showed a drained volume of 437.8 liters, resulting in a surface runoff coefficient of 0.219.With the end of the flow, from reading 127, it was found a 1.2 cm layer of eroded soil present in the Parshall flume.Of the 120 readings collected during the 30 minutes of testing, 4 showed negative values and were discarded, resulting in 3.33% of outliers.

Figure 1 :
Figure 1: Annual values of temperature and precipitation in the experimental area.Source: (Embrapa, 2022).

Figure 2 :
Figure 2: Sketch of the experiment.

Figure 3 :
Figure 3: Picture of the implanted system. 1

Figure 5 :
Figure 5: Sketch of the design of the Parshall flume.Measurements in cm.

Figure 6 :
Figure 6: Methods used for data acquisition to compare with the values obtained by the sensors.

Figure 7 :
Figure 7: Temperature variation during 12 months of data collection.

Figure 9 :
Figure 9: Examples of vegetation interference in data collection.

Figure 10 :
Figure 10: Relationship between distances obtained directly by the sensor (d), distances calibrated using the relationship between sound speed and air temperature (dT) and temperature of the ultrasonic sensor 13 (reference).

Figure 11 :
Figure 11: Distance sensor x ruler in the flume.

Table 2 :
Descriptive statistics calculated for distance measurement and temperature-corrected distance (cm) for the reference sensor (S13).

Table 3 :
Descriptive statistics calculated for distance measurement and temperature-corrected distance (cm) for 12 sensors in Parshall flumes.

Table 4 :
Height values of installed sensors, means of calibrated distances, and errors of 13 sensors (cm).

Table 5 :
Statistical values for measurements obtained with a laser distance meter and sensor in the laboratory.ValueLaser distance meter (LM) Calibrated sensor (dT) Error (%) -100(dT−LM)/dT

Table 6 :
Values obtained in the laboratory test with HC-SR04 compared to the ruler and in the field compared to the laser distance meter (cm).