Skip to content
MechStuff

MechStuff

''Making stuff… Simpler''

  • Home
  • MechStuff
  • News
  • How Stuff Works
  • Engines
  • Mech-Tech
  • TechStuff
  • Robotics
    • Robot Stuff
    • Tutorials
  • Engineering Marvels
  • For Mechanical Engineers
    • Games for Mechanical Engineers
    • Movies for Mechanical Engineers
    • Projects for Mechanical engineers
  • Contact Us
  • About us

Connection, Interfacing & Programming of Ultrasonic Sensor HC-SR04

16

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 ?

Share this Stuff :

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • More
  • Click to email a link to a friend (Opens in new window)
  • Click to print (Opens in new window)

Related

Tutorials Arduino, coding, connections, mini-project, ping sensor, program, range sensor, ultrasonic sensor, video tutorial

16 comments on “Connection, Interfacing & Programming of Ultrasonic Sensor HC-SR04”

  1. ANIUDH says:
    November 16, 2016 at 2:26 PM

    IT IS NOT WORKING

    Reply
    1. Jay Baviskar says:
      November 16, 2016 at 9:33 PM

      hello, what seems to be problem ?

      Reply
      1. Arpit says:
        March 4, 2019 at 6:27 PM

        Can we increase the range of ultra sonic sensor HC SR04

        please reply ..
        arpitsingh976026@gmail.com

      2. Jay Baviskar says:
        March 23, 2019 at 3:17 PM

        Hi Arpit, Unfortunately…NO! 🙁

  2. Eymen says:
    January 14, 2017 at 12:21 AM

    exit status 1
    ‘trigPin’ was not declared in this scope

    Reply
    1. Jay Baviskar says:
      January 14, 2017 at 4:27 PM

      hello, you must not have declared trigger pin in your code ! assign a digital pin number to your trigger pin in your “void setup” just like I’ve done in my code !

      Reply
  3. john says:
    September 5, 2018 at 4:10 PM

    brilliantly explained and one i will do

    Reply
  4. Dineth says:
    October 7, 2018 at 10:09 PM

    My is working.thanks

    Reply
  5. Devipriya says:
    February 2, 2020 at 12:58 PM

    Hi sir

    Can we use this program even for arduino mega 2560???

    Reply
    1. Jay Baviskar says:
      April 4, 2020 at 12:07 PM

      Hello Devipriya, Yes, I don’t see why not 🙂

      Reply
  6. pradeep says:
    February 9, 2020 at 8:35 PM

    if ((distance<=10))
    please tell meaning of above line. The arduino IDE is showing error while compiling.
    Thank you

    Reply
    1. Jay Baviskar says:
      August 10, 2020 at 5:04 PM

      It means- if the distance is less than OR equal to 10cm

      Reply
  7. Audrey says:
    February 12, 2020 at 10:56 PM

    thanks a lot !

    Reply
  8. james Mitchell says:
    May 19, 2020 at 1:35 AM

    hello great article work great on my robot car now I am trying to get it to work with my Sabertooth 2×32 motor controller for robot lawnmower can you look at my code I am not a coder thanks jim

    Reply
  9. gabriele says:
    September 1, 2020 at 3:26 AM

    i have a problem and i have also tryied to copy and paste to control there is not software digit error. the problem is that the led is alway on no matter what, even is nothing goes in front of the sensor

    Reply
    1. Jay Baviskar says:
      September 16, 2020 at 7:12 PM

      Hello, please check the connections & check pin numbers if they are correct

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Idealist by NewMediaThemes