Ultrasonic Sensor HC-SR04 With Arduino

Introduction: Ultrasonic Sensor HC-SR04 With Arduino

See more at:


Facebook link: https://www.facebook.com/pages/Theorycircuit/82511...

The HC-SR 04 is famous ultrasonic range sensor, and its very easy to use with many microcontrollers. This article will give you idea about to interface ultrasonic sensor with arduino board.

Step 1: Make Connections

Here the circuit constructed for to identify the range limit with warning beep sound. This circuit have minimum components such as arduino development board, ultrasonic sensor HC-SR 04 and a buzzer.

Step 2: Arduino Code

/*HC-SR04 Ping distance sensor

VCC to arduino 5v GND to arduino GND

Echo to Arduino pin 9

Trig to Arduino pin 8

Buzzer +ve to Arduino pin 13 and GND to GND

Original code sourced from theorycircuit.com

Some code and wiring inspired by arduino.cc


#define trigPin 8

#define echoPin 9

#define Buzzer 13

void setup()


Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(Buzzer, OUTPUT);


void loop()


digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 30)


digitalWrite(Buzzer,HIGH); //less than 30cm then buzzer will produce beep sound


else {



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


Serial.println("Out of range");


else {


Serial.println(" cm");




/* See more at: http://www.theorycircuit.com/DoItYourself/Electro...


Be the First to Share


    • Origami Speed Challenge

      Origami Speed Challenge
    • Home and Garden Contest

      Home and Garden Contest
    • Science Fair Challenge

      Science Fair Challenge