Categories: Tutorials

Connection, Interfacing & Programming of Ultrasonic Sensor HC-SR04

Today I will give you a tutorial on the Ultrasonic sensor HC-SR04 ! Also called as PING sensor or RANGE sensor; it is one of the most basic & easiest sensors of the beginners kit !

About Ultrasonic sensor HC-SR04 :-

The transmitter transmits ultrasonic pulses & they are reflected back & gets sensed by the receiver if any obstacle lies between the path.
The range of this ultrasonic is about 4 metres. There is a chance of error of +-3cm in measuring the distance.
It measures the time interval between sending & receiving the pulse & then by a formula – gives us the distance.

It has got 4 pins :-
VCC –                             connect it to 5V supply.
GND –                            connect it to ground.
echopin, trigpin –  connect it to any digital pin (as of here, we’ve connected them to 9, 10).

& the positive terminal of the LED is connected to pin number 7 on Arduino !

Code for the Ultrasonic sensor :-

int trigPin = 9;
int echoPin = 10;
int led = 7;


void setup() {
  Serial.begin(9600); 
   pinMode(led, OUTPUT);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // put your setup code here, to run once:

}

void loop() {
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(10);
 
 if((distance<=10)) 
  {
    digitalWrite(led, HIGH);
}
   else if(distance>10)
 {
     digitalWrite(led, LOW);
   }
}

You might like –

Make an Obstacle avoiding robot with this Ultrasonic sensor in 10 mins

Here is a video tutorial on the ultrasonic sensor….. 😉

You can buy it from Amazon… here it is

Please do not hesitate to ask questions. Please comment your problems(any kind of) faced by you in the comment section below so that I could answer them as early as possible 🙂 !

You might also like –

Tutorial on Infrared sensor TSOP 17xx | How to control LED with help of TV remote ?

Jay Baviskar

View Comments

Recent Posts

Metal Fabrication – A complete guide for Engineering students

Metal fabrication plays a vital role in engineering, transforming metal into functional components of everyday…

4 months ago

CNC Machine Inspection Techniques Based on Blockchain Technology

The pieces being produced in complex machining services frequently have difficult patterns. They may need…

9 months ago

How does a Digital Height Gauge work?

Digital height gauges are precision measuring instruments used to determine the height of an object…

11 months ago

The Advantages of Multi-Cavity Injection Molds

Injection molding is a rapid prototyping and manufacturing process applicable in the mass production of…

1 year ago

What are Industrial Pins? | Types and Applications

Fastening hardware, such as industrial pins, serve various functions in all industries. Butprimarily, you can…

2 years ago

What to Expect from Your First Car Service

Owning and operating a car is one thing, but keeping it running smoothly is another…

2 years ago