As you all might know, the whole world suffers under this tiny little thing called Corona Virus. The Government of my home country decided to restrict public life. Because suddenly I had gotten a lot of free time, I decided to invest this time into Arduino. What you see here is the result of my first few days in quarantine.
The project itself (the code and structure) is very simple and I hope easy to understand and copy.
The ultrasonic sensor is able to send and receive ultrasonic waves. This gives us the opportunity to use it as a distance sensor. Objects in front of the sensor reflect those waves back to the sensor. Due to the time, the wave needs to return to the sensor and the knowledge of how fast a sonic wave moves, we are able to calculate the distance between the sensor and the objects which reflects the wave.
We need a formula which contains speed, time and distance calculate the distance.
v = speed in meters per second (m/s)
d= distance in meter (m)
t= time seconds (s)
Now we need to change the formula to be able to calculate the time. To do this, we multiply both sides by t:
Now we know to calculate the distance.
It is important to adjust the data to the formula and the units which are used by our microcontroller.
Time (t): The sensor measures the time from the moment the wave being sent to moment the wave returns to the sensor. Since we are only interested in the distance between and the object we need to halve our time by divide by 2.
Speed (v): Sonic moves by 343, 2 meters per second (m/s). We have to adjust the unit of Speed. From meters per second to centimetre per microsecond, because the microcontroller measures time in microseconds.
343, 2 meter per second (m/s) = 0, 03432 centimeter per microsecond (cm/μs)
Time = pulseIn(echo, HIGH); // Reads a pulse on a pin in μs
distance = 0.03432 * (Time/2); // our formula to calculate the distance :) d=v*t