Introduction: Arduino: HC-SR04 Ultrasonic Sensor

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.

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.

Comments

author
Ajnii (author)2016-12-30

I keep getting a zero distance reading.

Any ideas?

author
TalalKhalil (author)Ajnii2017-01-08

You might need to check the GPIO pins you are using.

The provided code in this instructable uses pins number 11 & 12.

Hope this helps.

author
CipectorF (author)2016-09-05

nice and easy ;-) just testing new sensor thx

author
wold630 (author)2016-03-30

FYI...Your video is sideways!

About This Instructable

1,273views

23favorites

License:

Bio: Inventing the future by turning ideas into reality
More by TalalKhalil:Python Programming Series #1Arduino: HC-SR04 Ultrasonic Sensor Arduino: Simple Sequential LED's
Add instructable to: