Arduino: HC-SR04 Ultrasonic Sensor

1,445

23

4

Posted in TechnologyArduino

Introduction: Arduino: HC-SR04 Ultrasonic Sensor

About: The DIY Pioneer

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.

Share

    Recommendations

    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Spotless Contest

      Spotless Contest
    • Trash to Treasure

      Trash to Treasure
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    4 Comments

    I keep getting a zero distance reading.

    Any ideas?

    1 reply

    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.

    nice and easy ;-) just testing new sensor thx

    FYI...Your video is sideways!