<jupyter_start><jupyter_text>Ultrasonic distance sensorAn ultrasonic distance sensor can be used to prevent collisions. Cars produced recently are equipped with ultrasonic distance sensors that can detect obstacles, can signal possible collisions and can be used for assisted parking. ![](img/SensoriKadjarFront.jpg)The ultrasonic sensor emits a pulse that propagates in air with constant speed for which we assume for instance the value $v = $ 336 m/s. The pulse travels till the obstacle, it undergoes a reflection, then the reflected pulse travels back until it reaches the sensor again.![](img/smallJeepAndSensor.jpg)**Example** If the distance between the sensor and the obstacle is equal to 3 m, calculate the duration of the time lapse beginning when the pulse is emitted and ending when the pulse, reflected by the obstacle, arrives back to the sensor. Discussion of the exampleIf $d$ is the distance between the sensor and the obstacle, the distance travelled by the ultrasonic pulse is  $\Delta x = 2 d$.We assume that the ultrasonic pulse propagates with constant speed $v$. For a motion with constant speed, the relation between the distance travelled and the duration of the time lapse needed to travel (*time of flight*) is given by the equation$$v = \dfrac{\Delta x}{\Delta t}$$In this case, we have $$\Delta t = \dfrac{\Delta x}{v} = \dfrac{2 d}{v}$$ Calculations with Python<jupyter_code>v = 336   # speed of propagation (m/s)
d = 3     # distance sensor to obstacle (m)
Dt = 2 * d / v     # time of flight (s)

print()
print('Distance sensor-to-obstacle = ', d, 'm', '\n')
print('Time of flight = {0:0.3f} ms'.format(Dt*1000))
print()<jupyter_output>Distance sensor-to-obstacle =  3 m 

Time of flight = 17.857 ms<jupyter_text>> For running the Python scripts contained in this Jupyter **notebook** even if Python and Jupyter are not installed on your device, you can use the **binder** environment online[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/POSS-UniMe/simple-physics-with-Python/HEAD)&nbsp;  Calculations in interactive modeWe consider in particular th ultrasonic distance sensor **HC-SR04**. According to the technical specifications, this sensor operates in the range between 2 cm and 5 m.![](img/ultrasonicSensorSmall.jpg)Using a **slider** we can calculate the *time of flight* of the ultrasonic pulse for whichever chosen distance in the working range of the device.<jupyter_code>from ipywidgets import interact
import ipywidgets as widgets

v = 336     # speed of propagation (m/s)

print('\n','Distance (cm)')

sliderDistance = (widgets.FloatSlider(min = 2, max = 500, step = 0.1, value = 300))     # distance (cm)

def calculate(d):
    Dt = 2 * (d/100.)/ v     # time of flight (s)
    print ('\n','Time of flight {0:0.3f} ms \n'.format(Dt*1000));
    
widgets.interact(calculate, d = sliderDistance)<jupyter_output>Distance (cm)
{'fraction_non_alphanumeric': 0.09599447513812155, 'fraction_numerical': 0.017955801104972375, 'mean_word_length': 4.636186770428016, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '289265', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 896, 'n_tokens_neox': 799, 'n_words': 373, 'path': 'Chapter01/Section01/1-1-2-DistanceSensor.ipynb', 'repo_name': 'POSS-UniMe/simple-physics-with-Python', 'size': '2896'}