Introduction: How to Use an HC-SR04 Ultrasonic Sensor With Arduino

Picture of How to Use an HC-SR04 Ultrasonic Sensor With Arduino

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!

Step 1: Connect Everything Up

Picture of Connect Everything Up

HC-SR04 | UNO

Vcc | 5v

Gnd | Gnd

Echo | 11

Trig | 12

--------------------------------------------------

That's it super easy, let's continue

Step 2: Program It

Picture of 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)
  Serial.println("cm");

Step 3: Test It Out

Picture of 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!

Comments

About This Instructable

785views

17favorites

License:

Bio: 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 ... More »
More by Funguypro:3D Print Your House!!!!!DIY Raspberry Pi Indoor Outdoor WebcamSee Through Buildings With a Drone
Add instructable to: