Blog of Andrés Aravena
Methodology of Scientific Research:

Experiment: measuring the speed of sound

08 April 2020

One of the reasons why it is easier to do physics experiments nowadays is because there are many inexpensive sensors that are easy to find in the market. Many of them are used for drones or similar applications, so they are produced in large volumes at good prices.

One of such sensors is the Ultrasonic Ranging Module HC–SR04. According to the specification sheet,

“Ultrasonic ranging module HC–SR04 provides 2cm–400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit.”

The sensor emits a 40kHz tone and then it listens to the echo bouncing at the nearest solid object in the frontal direction. At that frequency, sound waves are very directional, so the microphone does not capture the speaker output directly. Instead, the microphone only detects the echo.

These devices are used as distance sensors, for example in a car rear bumper. When parking, it can indicate us when the vehicle is too close to an obstacle. The distance is calculated with the formula \[\text{distance}=\frac{\text{echo delay}\cdot\text{sound speed}}{2}.\]

The sensor specification assumes that the sound speed is 340m/s. Here we will not take the sound speed for granted. Instead, we will use the sensor to measure it. We will measure the distance and the echo delay, and use the formula \[\text{sound speed}=\frac{2\cdot\text{distance}}{\text{echo delay}}.\]

We can prepare a microcontroller to connect the ultrasonic sensor to the computer. This device looks like in the picture.

The device used for this experiment. A NodeMCU development board controls the HC-SR04 sensor to the computer using a USB interface.

The experiment was performed as follows:

The plan was to measure echo delay time at 10mm, 20mm, up to 200mm. In practice the smallest distance was about 12mm and the largest one was 180mm.

All the experiment was recorded in a video, that you can see in YouTube. The device took a sample every 0.5 seconds (nominally), and printed it in the computer screen

Device output, as seen in the computer screen.
raw_echo_delay <- read.delim("raw_echo_delay.txt")

Once all samples were taken, the values were saved in the file raw_echo_delay.txt. This file contains 643 rows. Graphically, the values look like in the figure.

plot(raw_echo_delay, cex=0.5,
    main="Raw data: echo delay v/s elapsed time")
Raw data from experiment

There are some obvious outliers, due to device malfunction. They are easy to spot, since they are much smaller than the neighborhood values.

Labeling and filtering

To calculate the speed of sound we also need another variable with the measured distance between the device and the wall.

In general it is better to use a program to process the raw data in a reproducible way. In this case I did it manually, according to the following protocol.

The result of this protocol is in the file echo_delay.txt.

echo_delay <- read.delim("echo_delay.txt")
boxplot(delay~distance, data=echo_delay)
The filtered data looks like this

Now there are 527 rows.

Measurement at 12 millimeters.

Measurement at 20 millimeters.

Measurement at 30 millimeters.

Measurement at 40 millimeters.

Measurement at 50 millimeters.

Measurement at 60 millimeters.

Measurement at 70 millimeters.

Measurement at 80 millimeters.

Measurement at 90 millimeters.

Measurement at 100 millimeters.

Measurement at 110 millimeters.

Measurement at 120 millimeters.

Measurement at 130 millimeters.

Measurement at 140 millimeters.

Measurement at 150 millimeters.

Measurement at 160 millimeters.

Measurement at 170 millimeters.

Measurement at 180 millimeters.

kable(echo_delay)
id elapsed delay distance
3 1 105 12
4 2 104 12
5 2 106 12
6 3 106 12
7 3 105 12
8 4 105 12
9 4 105 12
10 5 105 12
11 6 105 12
12 6 105 12
13 7 105 12
14 7 105 12
15 8 105 12
16 9 105 12
17 9 104 12
18 10 106 12
19 10 105 12
20 11 105 12
21 12 105 12
22 12 105 12
23 13 105 12
24 13 105 12
25 14 105 12
26 15 104 12
27 15 106 12
28 16 105 12
29 16 105 12
30 17 105 12
31 18 105 12
32 18 105 12
33 19 129 12
34 19 129 12
35 20 102 12
36 20 103 12
37 21 102 12
38 22 102 12
39 22 103 12
40 23 103 12
41 23 102 12
42 24 103 12
43 25 102 12
44 25 102 12
45 26 101 12
46 26 102 12
47 27 101 12
48 28 103 12
49 28 103 12
50 29 102 12
51 29 103 12
52 30 101 12
53 31 103 12
54 31 102 12
55 32 103 12
56 32 102 12
57 33 102 12
58 34 103 12
59 34 101 12
60 35 102 12
61 35 102 12
62 36 102 12
63 36 102 12
64 37 103 12
65 38 101 12
66 38 103 12
67 39 101 12
68 39 103 12
69 40 101 12
72 42 119 20
73 42 117 20
74 43 117 20
75 44 117 20
76 44 114 20
77 45 115 20
78 45 113 20
79 46 113 20
80 47 115 20
81 47 113 20
82 48 113 20
83 48 113 20
84 49 113 20
85 49 113 20
86 50 115 20
87 51 114 20
88 51 115 20
89 52 113 20
90 52 114 20
91 53 115 20
92 54 114 20
93 54 113 20
94 55 113 20
95 55 114 20
96 56 113 20
102 60 170 30
103 60 170 30
104 61 170 30
105 61 170 30
106 62 170 30
107 63 170 30
108 63 170 30
109 64 170 30
110 64 170 30
111 65 171 30
112 65 170 30
113 66 170 30
114 67 170 30
115 67 170 30
116 68 172 30
117 68 170 30
118 69 170 30
119 70 170 30
120 70 170 30
121 71 170 30
122 71 170 30
123 72 170 30
124 73 170 30
125 73 170 30
126 74 170 30
142 83 200 40
143 84 201 40
144 84 200 40
145 85 199 40
146 86 200 40
147 86 200 40
148 87 199 40
149 87 199 40
150 88 200 40
151 89 200 40
152 89 199 40
153 90 199 40
154 90 199 40
155 91 200 40
156 92 200 40
157 92 200 40
158 93 200 40
159 93 199 40
160 94 199 40
161 94 199 40
162 95 199 40
163 96 199 40
164 96 200 40
167 98 286 50
168 99 285 50
169 99 286 50
170 100 282 50
171 100 286 50
172 101 285 50
173 102 284 50
174 102 286 50
175 103 286 50
176 103 285 50
177 104 286 50
178 105 285 50
179 105 285 50
180 106 285 50
181 106 286 50
182 107 284 50
183 108 285 50
184 108 286 50
185 109 285 50
186 109 285 50
187 110 285 50
188 110 285 50
189 111 286 50
190 112 285 50
192 113 324 60
193 113 319 60
194 114 318 60
195 115 319 60
196 115 318 60
197 116 318 60
198 116 319 60
199 117 318 60
200 118 319 60
201 118 318 60
202 119 317 60
203 119 319 60
204 120 319 60
205 121 319 60
206 121 318 60
207 122 317 60
208 122 319 60
209 123 340 60
210 124 338 60
211 124 340 60
212 125 339 60
213 125 317 60
214 126 317 60
215 126 318 60
216 127 318 60
217 128 319 60
218 128 318 60
219 129 317 60
220 129 317 60
221 130 318 60
222 131 319 60
223 131 319 60
224 132 319 60
225 132 318 60
226 133 318 60
227 134 317 60
228 134 318 60
229 135 318 60
230 135 319 60
231 136 319 60
232 137 318 60
233 137 319 60
238 140 375 70
242 142 374 70
243 143 374 70
244 144 373 70
245 144 370 70
246 145 371 70
247 145 372 70
248 146 374 70
249 147 373 70
251 148 370 70
252 148 372 70
253 149 374 70
254 150 372 70
255 150 372 70
256 151 371 70
257 151 374 70
258 152 370 70
259 153 371 70
260 153 370 70
271 160 451 80
272 160 451 80
273 161 451 80
274 161 451 80
275 162 452 80
276 163 452 80
277 163 452 80
278 164 453 80
279 164 452 80
280 165 453 80
281 166 452 80
286 169 535 90
287 169 532 90
288 170 534 90
289 170 534 90
290 171 536 90
291 171 535 90
292 172 535 90
293 173 535 90
294 173 535 90
295 174 534 90
296 174 534 90
297 175 534 90
298 176 534 90
299 176 535 90
300 177 534 90
301 177 535 90
302 178 535 90
303 179 534 90
304 179 535 90
305 180 536 90
306 180 535 90
307 181 535 90
308 182 534 90
309 182 535 90
310 183 534 90
311 183 534 90
312 184 535 90
314 185 569 100
315 186 569 100
316 186 569 100
317 187 569 100
318 187 569 100
320 189 569 100
321 189 568 100
322 190 569 100
323 190 567 100
324 191 571 100
325 192 569 100
326 192 571 100
327 193 569 100
328 193 570 100
329 194 571 100
330 195 571 100
331 195 570 100
332 196 570 100
333 196 570 100
334 197 570 100
335 198 571 100
336 198 571 100
337 199 570 100
338 199 569 100
339 200 570 100
340 201 571 100
341 201 570 100
342 202 570 100
343 202 570 100
344 203 569 100
345 203 570 100
353 208 647 110
354 209 646 110
355 209 662 110
356 210 666 110
357 211 642 110
358 211 644 110
359 212 625 110
360 212 626 110
361 213 647 110
362 214 647 110
363 214 647 110
364 215 647 110
366 216 647 110
367 217 647 110
368 217 623 110
369 218 648 110
370 218 648 110
371 219 647 110
372 219 623 110
373 220 647 110
374 221 623 110
375 221 624 110
376 222 647 110
377 222 647 110
378 223 625 110
379 224 623 110
386 228 730 120
387 228 730 120
388 229 733 120
389 230 732 120
390 230 733 120
391 231 733 120
392 231 732 120
393 232 732 120
394 233 734 120
395 233 732 120
396 234 731 120
397 234 732 120
398 235 733 120
399 235 732 120
400 236 733 120
401 237 734 120
402 237 734 120
403 238 734 120
404 238 733 120
405 239 732 120
412 243 812 130
413 244 811 130
414 244 812 130
415 245 811 130
416 246 812 130
417 246 811 130
418 247 812 130
419 247 813 130
420 248 813 130
421 249 813 130
422 249 813 130
423 250 813 130
424 250 813 130
425 251 811 130
426 251 811 130
427 252 812 130
428 253 812 130
429 253 813 130
430 254 813 130
431 254 812 130
432 255 811 130
433 256 788 130
434 256 787 130
435 257 838 130
436 257 814 130
437 258 837 130
438 259 811 130
439 259 787 130
440 260 811 130
441 260 813 130
442 261 811 130
443 262 812 130
444 262 812 130
445 263 812 130
446 263 813 130
447 264 812 130
448 265 813 130
449 265 813 130
450 266 811 130
451 266 811 130
452 267 813 130
453 267 812 130
454 268 812 130
467 276 849 140
468 276 851 140
469 277 850 140
470 278 850 140
471 278 850 140
472 279 849 140
473 279 850 140
474 280 851 140
476 281 849 140
477 282 851 140
478 282 850 140
479 283 850 140
480 284 851 140
481 284 849 140
482 285 851 140
483 285 852 140
484 286 849 140
485 286 849 140
486 287 851 140
487 288 850 140
488 288 850 140
489 289 850 140
490 289 850 140
491 290 851 140
492 291 850 140
493 291 851 140
494 292 851 140
513 303 905 150
514 304 904 150
515 304 903 150
516 305 904 150
517 305 904 150
518 306 906 150
519 307 903 150
520 307 904 150
521 308 904 150
522 308 904 150
523 309 904 150
524 310 904 150
525 310 906 150
526 311 906 150
527 311 907 150
528 312 907 150
529 313 907 150
530 313 907 150
531 314 907 150
532 314 905 150
533 315 904 150
534 316 903 150
535 316 907 150
536 317 906 150
537 317 904 150
538 318 904 150
539 318 904 150
540 319 904 150
541 320 906 150
544 321 963 160
545 322 963 160
546 323 962 160
547 323 964 160
548 324 963 160
549 324 962 160
550 325 963 160
551 326 962 160
553 327 962 160
554 327 962 160
555 328 962 160
556 329 963 160
557 329 964 160
558 330 963 160
559 330 963 160
560 331 963 160
561 332 963 160
562 332 964 160
563 333 964 160
564 333 963 160
565 334 963 160
566 335 962 160
567 335 964 160
568 336 964 160
569 336 961 160
572 338 1019 170
573 339 1019 170
574 339 1021 170
575 340 1024 170
576 340 1023 170
577 341 1022 170
578 342 1021 170
579 342 1019 170
580 343 1023 170
581 343 1020 170
582 344 1024 170
583 345 1023 170
584 345 1022 170
585 346 1022 170
586 346 1024 170
587 347 1022 170
588 348 1024 170
589 348 1023 170
590 349 1022 170
591 349 1024 170
592 350 1022 170
593 351 1024 170
594 351 1022 170
595 352 1024 170
596 352 1022 170
606 358 1055 180
607 359 1055 180
608 359 1055 180
609 360 1055 180
610 361 1055 180
611 361 1055 180
612 362 1055 180
613 362 1055 180
614 363 1055 180
615 364 1055 180
616 364 1056 180
617 365 1053 180
618 365 1055 180
619 366 1056 180
620 367 1056 180
621 367 1057 180
622 368 1057 180
623 368 1055 180
624 369 1055 180
625 370 1056 180
626 370 1055 180
627 371 1055 180
628 371 1055 180
629 372 1056 180
630 372 1056 180
631 373 1031 180
632 374 1057 180
633 374 1055 180
634 375 1054 180
635 375 1055 180
636 376 1055 180
637 377 1055 180
638 377 1055 180
639 378 1055 180
640 378 1056 180
641 379 1055 180
642 380 1055 180
643 380 1055 180