How to Interface Ultrasonic Sensor (HCSR04) to Arduino Uno

Introduction: How to Interface Ultrasonic Sensor (HCSR04) to Arduino Uno

Ultrasonic sensors are much more efficient than other obstacle detection sensors. Ultrasonic Sensors relatively inexpensive and also the ultrasound emitters and detectors are portable without the need for complex circuitry.Real time assistance and artificial vision along with dedicated obstacle detection system is provided.

Here TechPonder helps you in interfacing Ultrasonic sensor to Arduino.

Hardware requirements;

1) Arduino UNO

2)Ultrasonic Sensor


4)Bread Board

Step 1: Hardware Connections

Hardware connections:-

Arduino Ultrasonic sensor

5v - > Vcc

gnd -> Gnd

8th Pin -> Trig pin

7th pin -> Echo Pin

Arduino Buzzer

9th pin -> +ve pin

Gnd -> -ve Pin

Step 2: Program and Results

// UltraSonic Sensor interfacing to Arduino .

int buzzer = 9;

int triggerPin = 7; //triggering on pin 7

int echoPin = 8; //echo on pin 8

void setup()


Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor Serial.println("Tech Ponder's UltraSonic Sensor Tutorial");

pinMode(triggerPin, OUTPUT); //defining pins

pinMode(echoPin, INPUT);

pinMode(buzzer, OUTPUT);

digitalWrite(buzzer,LOW); }

void loop()

{ int duration, distance; //Adding duration and distance

digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)


digitalWrite(triggerPin, LOW);

duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave

distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number


Serial.print(distance); //printing the numbers

Serial.print("cm"); //and the unit

Serial.println(" "); //just printing to a new line

if (distance < 35)



Serial.println("Buzzer On");




Results are shown on Serial Monitor.

For every second Arduino calculates the distance using Ultrasonic sensor. When the distance is less than 35cm arduino detects the threshold value and the buzzer is on. it can be used as an obstacle detector. When the obstacle approaches distance less the 35cm it alerts the user.



Be the First to Share


    • Big and Small Contest

      Big and Small Contest
    • Make It Bridge

      Make It Bridge
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge