Using the HC-SR04 Ultrasonic Range Sensor with an Arduino
Ultrasonic sensor are great for all kind of projects that need distance measurements, avoiding obstacles as examples.
The HC-SR04 are inexpensive and easy to use since we will be using a Library specifically designed for these sensor.
As you can see, we only need 4 connections : Voltage, Ground, Trigger and Echo.
Using a Library designed for these sensors will make our code short and simple.
We include the library at the beginning of our code, and then by using simple commands we can control the behaviour of the sensor.
Once you have the library, just go ahead and extract it to the Library folder inside your Arduino IDE software folder. Make sure to restart the IDE software so the library is loaded at startup.
Copy and Paste the above Sketch/Code in your Arduino IDE.
You can download the NewPing Library we used here: NewPing-Library