How To Use An HC-SR04 Ultrasonic Sensor With Arduino

How To Use An HC-SR04 Ultrasonic Sensor With Arduino © CC BY-NC-SA

This tutorial will show you how to use an HC-SR04 chip with Arduino.

  • 5,041 views
  • 1 comment
  • 4 respects

Components and supplies

Apps and online services

About this project

This tutorial 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:

  • Arduino Uno
  • HC-SR04
  • Wires

Let's Begin!

Step 1: 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

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

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!

Code

Code snippet #1Plain text
#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");

Comments

Similar projects you might like

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 6,213 views
  • 4 comments
  • 12 respects

Door Alarm With Ultrasonic Sensor

Project showcase by MattG

  • 4,896 views
  • 1 comment
  • 8 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 4,663 views
  • 7 comments
  • 19 respects

Ultrasonic Sensor with LCD Display Using 433MHz

Project tutorial by mcharrison500

  • 3,525 views
  • 0 comments
  • 17 respects
Add projectSign up / Login