The ultrasonic sensor is a device that can measure distance by sending out sound waves and calculating how long it takes for them to bounce back. It can be used to measure the distance between two objects or to detect whether an object is in the way.
In this tutorial we will learn how to use the ultrasonic sensor to measure distance and print the values on your computer.
Connect the wires between the ultrasonic sensor and the Arduino, as shown in the image below.
Connections from the Arduino to the ultrasonic sensor:
• Arduino VCC pin → ultrasonic sensor VCC pin (+ pin)
• Arduino pin 11 → ultrasonic sensor Trig pin
• Arduino pin 11 → ultrasonic sensor Echo pin
• Arduino GND pin → ultrasonic sensor GND pin (- pin)
Now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.
As we see in the following image, the serial monitor displays the distance between the sensor and the object. The value is updated and printed every one second because of the delay we determined in our code.
You can ensure that the sensor is calibrated and that the value it provides is completely correct by comparing the output with one of the measuring tools, such as a ruler. For example, the speed of sound varies slightly depending on temperature and pressure, so the accuracy of the sensor may differ from the value in the equation that we used.