Picture of Simple Arduino and HC-SR04 Example
After buying a HC-SR04 from Amazon, I could not get it to work out of the box. Not wanting to concede I had a DOA sensor on my hands, I searched for a simple example setup. After spending far too long on this than I felt I needed to, I decided to make this instructable to help other emerging tinkerers get their project off the ground.

I admit this example is more than bare-bones in that it has LEDs, but this lets me test it without needing a PC to show distance and check the accuracy of the sensor.
Remove these adsRemove these ads by Signing Up

Step 1: Parts List

Arduino UNO R3 (I use the Adafruit mount)
One (1) HC-SR04 Ultrasonic Sensor
One (1) Red LED
One (1) Green LED
Two (2) 560 ohm (Green, Blue, Brown, Gold) Resistors
Half Breadboard
Eight (8) Male/Male hookup wires
A ruler that measures centimeters (or use the serial monitor)

Step 2: Connect the components

Picture of Connect the components
Connect the components and wires as shown in the two pictures.

Step 3: Upload the sketch

Copy the sketch to your Arduino and watch the blinky lights.

HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at:
Original code improvements to the Ping sketch sourced from
Some code and wiring inspired by

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  else {
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  else {
    Serial.println(" cm");
1-40 of 107Next »
kumarsrt8 days ago

hello sir can im doing obstacle avoidance robot with vacuum cleaning need to use 3 ultrasonic HC-SR04 sensors in front of the robot.can i connect VCC and Gnd pin directly to the regulated 5V from IC7805 will affect the working of sensors.if not is there any way to connect 3 sensors

KenL410 days ago

Thanks, I brought 10 sensors for a few project and this was the fastest way to test it. They all work, and thanks again for such a easy to follow example.

Blazingnite15 days ago

what does the || mean?

|| means "OR". As in this OR that.


if (distance >= 200 || distance <= 0){

Serial.println("Out of range");

is saying if the distance is greater than or equal to 200 OR the distance is less than or equal to 0, then print the line "Out of range"

on that same note, if you see &&, that means AND

Trippme15 days ago

Very simple, thank you!

baudeagle21 days ago

Thank you very much, helps me to test my new ultrasonic sensors from Ebay.

RobL123 days ago

followed the instructions and had it working in minutes - great directions, thanks very to try with a Picaxe!



menandroi23 days ago

my another question is what is this value ? ---> distance = (duration/2) / 29.1;

menandroi23 days ago

what if there are 3 different alarm? for example 25cm to 50cm will trigger alarm1, 51 to 75cm alarm2 and 76 to 100cm above for alarm3? what is the command to be used?

menandroi23 days ago

what if there are 3 different alarm? for example 25cm to 50cm will trigger alarm1, 51 to 75cm alarm2 and 76 to 100cm above for alarm3? what is the command to be used?

rituchoudhary made it!24 days ago

Thanks for sharing the tutorial. :-)

eranox made it!25 days ago

This is just what I was looking for! Thanks for such a detailed, straighforward instructable. This will come in handy in dozens of projects!

ushabala031 month ago

Can anybody share the code in c++?

Thanks for this tutorial man, but if I'll use a parallac PIR motion sensor, what code should I use ?

gheciobanu1 month ago

Thanks for the tutorial, i got the same sensor, and this is exactly what i need. Works perfectly! Thank you!

MichaelL162 months ago

I got this working. However, every few seconds i see 3cm in the console a flash of red with nothing in the way of the sensor. is this some byproduct of the code or maybe I have a bunged up sensor.

Hi Michaeil - do you have a shield attached to your arduino? The reason I ask is that I had a very similar problem when I had the WIFI shield attached. Once I removed that the sensor worked fine.

joik_17092 months ago

Is there the same tutorial for node js using johnny five?

bochokoy3 months ago

I tweaked this.. I added a DC Motor in the circuit. Everytime the sensor detects an object the DC Motor wont start though, it rotates when spinned manually. Could somebody help me with this? It's like the voltage is too low. Im having Arduino Nano. Help? anyone

abirm1 bochokoy2 months ago

have u used a motor driver? if not then use a L298 Motor driver IC to run the motor.

thebluesman007 made it!2 months ago

Fantastic! :D

rkasemi6 months ago

i need to make a obstacle avoiding robot and i have a HC-SR04 sensor, can anyone help me to modify this code because i need to put servo motors instead of lights, plzzz i need your help!!!

HI, i developed this with sensor and hub-ee wheels. Here is a video and all my code. Have fun :) 24 KB
tagr made it!2 months ago

I just try this tutorial , it's working . it's really useful for me. thanks for sharing.

schulbusl2 months ago

thanks for the tutorial,

my version of it :D


I can't seem to green LED to light up and I'm pretty sure that I've wired it up correctly.
If I keep an object at a distance less that 4cm, the red LED lights up just fine though.

mcansozeri made it!3 months ago

Thanks for the tutorial. It was really helpful for me. I'll improve this and share it with you :D

image1 (1).JPG
Boba_Steph made it!5 months ago

Made it!

Very helpful for a beginner like me. Now I'm currently working on adding a speaker that will change in tone as distance increases.


hey, I'm trying to do a similar thing with changing volumes, could i ask how you're going about it?

You can use piezo beep to make frequency-varying speakers :)

thanks, I'm trying to change an mp3 though

CarlosV23 months ago

very good, Thanks

salman476 months ago

first i made it.. but when i tried it to
show some friends.. the hc sr04 sensor stopped working its consistantly
giving "out of rang" in serial monitor :( .. in addition my Tx LED of arduino Mega keep blinking when i upload the code in it now ..

ErwinC salman474 months ago

Me too, and after double-checking, the blue and green wires were swapped and after fixing that it all worked great.

Maybe cause this line is swapped.

"Echo to Arduino pin 13 Trig to Arduino pin 12"

Should say, Echo to 12 and Trig to 13.

agrove35 months ago

Nice article! I found a nice tutorial on HC-SR04 and Arduino here as well:

jaimeiniesta5 months ago

Made it!

Thanks, this was easy to setup. I've seen there are libraries to make this easier, though:

MattS35 months ago

Hi wonderful model, but I'm having trouble getting the LEDs to blink even though the sensor unit is picking up and the breadboard receiving data. Might you have the answer?

Great and simple

RittaChickka5 months ago
1-40 of 107Next »