Arduino: HC-SR04 Ultrasonic Sensor

by EngineeringLead in Circuits > Arduino

2844 Views, 25 Favorites, 0 Comments

Arduino: HC-SR04 Ultrasonic Sensor

FullSizeRender (1).jpg

Objective:

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.

Schematics

ultra schematics.png

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.

Code

1.PNG

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)

Results

Arduino: Ultrasonic Sensor

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