element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Measuring Distance using HC-SR-04 sensor with Arduino
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ami301618
  • Date Created: 5 Mar 2021 12:27 PM Date Created
  • Views 380 views
  • Likes 6 likes
  • Comments 2 comments
Related
Recommended

Measuring Distance using HC-SR-04 sensor with Arduino

ami301618
ami301618
5 Mar 2021

For distance measuring applications HC-SR04 is the most common sensor used. This sensor uses sound waves to measure the distance between the sensor and the obstacle. It works on the principle of Doppler effect.

 

HC-SR04 also known as ultrasonic sensor have four pins:

 

Vcc ------ +5v

Trig------- Output

Echo ------Input

Gnd-------Gnd

 

Trig will send or generate the sound wave which is received at Echo pin and time of travel is calculated by the microcontroller.

 

So, Distance is calculated using the formula:

Distance = Speed * Time

 

Speed = 340m/s

Time Calculated by PulseIn function of Arduino

 

Hence, using the formula stated above distance can be easily calculated.

 

 

Source Code:

 

const int trig = 6;

const int echo =7;

float duration, distance;

 

 

void setup()

{

  pinMode(trig, OUTPUT);

  pinMode(echo, INPUT);

  Serial.begin(9600);

}

 

 

void loop()

{

  digitalWrite(trig, LOW);

  delayMicroseconds(5);

  digitalWrite(trig, HIGH);

  delayMicroseconds(5);

  digitalWrite(trig, LOW);

 

  duration = pulseIn(echo, HIGH);

 

  distance = (0.034*duration)/2;

 

  Serial.println("Distance is: ");

  Serial.println(distance);

 

  delay(500);

 

 

}

Attachments:
image
  • Sign in to reply

Top Comments

  • dubbie
    dubbie over 2 years ago +1
    Amit, Do you have any calibration curves/graphs for this. Have you checked the beam width or the minimum/maximum distances it works at? Did you use an Arduino? Dubbie
  • ami301618
    ami301618 over 2 years ago in reply to dubbie +1
    Hi Dubbie Yes, I used Arduino Uno for this. No, I don't have any calibration curves/graphs for this, but the minimum distance was 2 cm and the maximum distance was 300cm. The beam width is yet to be calculated…
  • ami301618
    ami301618 over 2 years ago in reply to dubbie

    Hi Dubbie

     

    Yes, I used Arduino Uno for this. No, I don't have any calibration curves/graphs for this, but the minimum distance was 2 cm and the maximum distance was 300cm. The beam width is yet to be calculated as the effectual angle for trigger output is 15 degrees.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dubbie
    dubbie over 2 years ago

    Amit,

     

    Do you have any calibration curves/graphs for this. Have you checked the beam width or the minimum/maximum distances it works at? Did you use an Arduino?

     

    Dubbie

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube