Introduction: Arduino: HC-SR04 Ultrasonic Sensor

About: Moving fast and breaking things!


Utilize an HC-SR04 ultrasonic sensor with an Arduino to measure the distance of any object.

The basic idea behind an ultrasonic sensor:

An ultrasonic sensor triggers a signal that travels in a straight path, and then if the triggered signal encounters an object, it will be reflected and the echo pin will detect it.

Parts Needed:

1. Arduino Uno microcontroller.

2. Arduino software IDE (I used the 1.6.8 version)

Note: Arduino IDE can be downloaded for free from Arduino software.

3. HC-SR04 ultrasonic sensor.

4. Breadboard.

5. Wires as needed.

Step 1: Schematics

The HC-SR04 has 4 pins: Vcc, trig, echo, GND.

Vcc: input voltage, connect it to +5V.

trig: trigger pin, connect it to any digital pin.

echo: echo pin, connect it to any digital pin.

GND: ground, connect it to GND on the Arduino.

Note: the attached figure shows the schematics for this project.

Step 2: Code

The attached figure shows the code needed. Also, the attached file contains the code used.

After writing the code, you have to:

1. Verify it.

2. Upload it to the board.

3. Open the serial monitor. (Cltr + Shift + M)

Step 3: Results

The embedded YouTube video shows the functionality of the ultrasonic sensor, where it was used to measure the distance of an object.