How to Use an HC-SR04 Ultrasonic Sensor With Arduino

About: I like to create new things! I'm very interested in IOT and just Internet Stuff. I am not responsible for any injury or deaths caused by my projects.

This instructable will show you how to use an HC-SR04 chip with Arduino. I got a kit from GearBest that has everything in it that you will need for this project, also this kit is very good for beginners and i would recommend you check it out here

You will need the fallowing parts to make this work:

1) Arduino Uno

2) HC-SR04

3) Wires

Let's Begin!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Connect Everything Up


Vcc | 5v

Gnd | Gnd

Echo | 11

Trig | 12


That's it super easy, let's continue

Step 2: Program It

You will need the NewPing Library, you can Download it Here

I used the sample library sketch (i also included it below) and uploaded it to my Arduino UNO

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

Step 3: Test It Out

Open the Serial Port and test it out.

You now should be done! If you have any questions leave them in the comments and i will try to help you out.

Thanks for reading!

Maker Olympics Contest 2016

Participated in the
Maker Olympics Contest 2016

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest