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 ItYou 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 OutOpen 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
Please log in or sign up to comment.