{"id":1492,"date":"2021-07-21T13:12:00","date_gmt":"2021-07-21T13:12:00","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=1492"},"modified":"2021-10-07T13:18:45","modified_gmt":"2021-10-07T13:18:45","slug":"complete-guide-for-ultrasonic-sensor-hc-sr04","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/complete-guide-for-ultrasonic-sensor-hc-sr04\/","title":{"rendered":"Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino"},"content":{"rendered":"\n<p>This article is a guide about the Ultrasonic Sensor HC-SR04. We&#8217;ll explain how it works, show you some of its features and share an Arduino project example you can follow to integrate into your projects. We provide a schematic diagram on how to wire the ultrasonic sensor and an example sketch with the Arduino.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-HC-SR04-Guide-Tutorial.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino\" class=\"wp-image-105367\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-HC-SR04-Guide-Tutorial.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-HC-SR04-Guide-Tutorial.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-HC-SR04-Guide-Tutorial.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-HC-SR04-Guide-Tutorial.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Description<\/h3>\n\n\n\n<p>The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. This sensor reads from 2cm to 400cm (0.8inch to 157inch) with an accuracy of 0.3cm (0.1inches), which is good for most hobbyist projects. In addition, this particular module comes with ultrasonic transmitter and receiver modules.<\/p>\n\n\n\n<p>The following picture shows the HC-SR04 ultrasonic sensor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"HC-SR04 Ultrasonic Sensor Module Distance Measurement Component Part Front\" class=\"wp-image-104465\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The next picture shows the other side of the sensor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Back.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"HC-SR04 Ultrasonic Sensor Module Distance Measurement Component Part Back\" class=\"wp-image-104464\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Back.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Back.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Features<\/h3>\n\n\n\n<p>Here&#8217;s a list of some of the HC-SR04 ultrasonic sensor features and specs\u2014for more information, you should consult the sensor&#8217;s datasheet:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Power Supply :+5V DC<\/li><li>Quiescent Current : &lt;2mA<\/li><li>Working Current: 15mA<\/li><li>Effectual Angle: &lt;15\u00b0<\/li><li>Ranging Distance : 2cm \u2013 400 cm\/1&#8243; &#8211; 13ft<\/li><li>Resolution : 0.3 cm<\/li><li>Measuring Angle: 30 degree<\/li><li>Trigger Input Pulse width: 10uS TTL pulse<\/li><li>Echo Output Signal: TTL pulse proportional to the distance range<\/li><li>Dimension: 45mm x 20mm x 15mm<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How Does it Work?<\/h3>\n\n\n\n<p>The ultrasonic sensor uses sonar to determine the distance to an object. Here\u2019s what happens:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).<\/li><li>The sound travels through the air. If it finds an object, it bounces back to the module.<\/li><li>The ultrasound receiver (echo pin) receives the reflected sound (echo).<\/li><\/ol>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/how-ultrasonic-sensor-works-01.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"453\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/how-ultrasonic-sensor-works-01.png?resize=750%2C453&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"How Ultrasonic Sensor Works\" class=\"wp-image-104535\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/how-ultrasonic-sensor-works-01.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/how-ultrasonic-sensor-works-01.png?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The time between the transmission and reception of the signal allows us to calculate the distance to an object. This is possible because we know the sound&#8217;s velocity in the air. Here\u2019s the formula:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>distance to an object = ((speed of sound in the air)*time)\/2<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>speed of sound in the air at 20\u00baC (68\u00baF) =&nbsp;<strong>343m\/s<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">HC-SR04 Ultrasonic Sensor Pinout<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"330\" height=\"388\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultra.png?resize=330%2C388&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ultra\" class=\"wp-image-1493\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultra.png?w=330&amp;quality=100&amp;strip=all&amp;ssl=1 330w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultra.png?resize=255%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 255w\" sizes=\"(max-width: 330px) 100vw, 330px\" \/><\/figure><\/div>\n\n\n\n<p>Here&#8217;s the pinout of the HC-SR04 Ultrasonic Sensor.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span class=\"rnthl rntcred\">VCC<\/span><\/td><td>Powers the sensor (5V)<\/td><\/tr><tr><td><span class=\"rnthl rntcgreen\">Trig<\/span><\/td><td>Trigger Input Pin<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">Echo<\/span><\/td><td>Echo Output Pin<\/td><\/tr><tr><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><td>Common GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Where to buy?<\/h2>\n\n\n\n<p>You can check the&nbsp;Ultrasonic Sensor HC-SR04 sensor&nbsp;on Maker Advisor&nbsp;to find the best price:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/ultrasonic-sensor-hc-sr04\/\" target=\"_blank\" rel=\"noreferrer noopener\">HC-SR04 Ultrasonic Sensor<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Arduino with HC-SR04 Sensor<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Arduino-UNO-Board-HC-SR04-Ultrasonic-Sensor-Module.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino UNO Board HC-SR04 Ultrasonic Sensor Module Arduino\" class=\"wp-image-104466\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Arduino-UNO-Board-HC-SR04-Ultrasonic-Sensor-Module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Arduino-UNO-Board-HC-SR04-Ultrasonic-Sensor-Module.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>This sensor is very popular among Arduino tinkerers. So, here we provide an example of how to use the HC-SR04 ultrasonic sensor with the Arduino. In this project, the ultrasonic sensor reads and writes the distance to an object in the serial monitor.<\/p>\n\n\n\n<p>The goal of this project is to help you understand how this sensor works. Then, you should be able to use this example in your own projects.<\/p>\n\n\n\n<p><strong>Parts Required<\/strong><\/p>\n\n\n\n<p>Here&#8217;s a list of the parts required to follow the next tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino UNO<\/a>&nbsp;\u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best Arduino Starter Kits<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/ultrasonic-sensor-hc-sr04\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ultrasonic Sensor (HC-SR04)<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Arduino with HC-SR04 Sensor Wiring<\/h2>\n\n\n\n<p>Follow the next schematic diagram to wire the HC-SR04 ultrasonic sensor to the Arduino.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"559\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?resize=1200%2C559&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-54434\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?w=1618&amp;quality=100&amp;strip=all&amp;ssl=1 1618w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?resize=300%2C140&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?resize=768%2C358&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?resize=1024%2C477&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/11\/ultrasonic-sensor-with-arduino-hc-sr04.jpg?resize=510%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 510w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The following table shows the connections you need to make:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Ultrasonic Sensor HC-SR04<\/strong><\/td><td><strong>Arduino<\/strong><\/td><\/tr><tr><td>VCC<\/td><td><span class=\"rnthl rntcred\">5V<\/span><\/td><\/tr><tr><td>Trig<\/td><td><span class=\"rnthl rntclblue\">Pin 11<\/span><\/td><\/tr><tr><td>Echo<\/td><td><span class=\"rnthl rntcyellow\">Pin 12<\/span><\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Upload the following code to your Arduino IDE.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * created by Rui Santos, https:\/\/randomnerdtutorials.com\n * \n * Complete Guide for Ultrasonic Sensor HC-SR04\n *\n    Ultrasonic sensor Pins:\n        VCC: +5VDC\n        Trig : Trigger (INPUT) - Pin11\n        Echo: Echo (OUTPUT) - Pin 12\n        GND: GND\n *\/\n \nint trigPin = 11;    \/\/ Trigger\nint echoPin = 12;    \/\/ Echo\nlong duration, cm, inches;\n \nvoid setup() {\n  \/\/Serial Port begin\n  Serial.begin (9600);\n  \/\/Define inputs and outputs\n  pinMode(trigPin, OUTPUT);\n  pinMode(echoPin, INPUT);\n}\n \nvoid loop() {\n  \/\/ The sensor is triggered by a HIGH pulse of 10 or more microseconds.\n  \/\/ Give a short LOW pulse beforehand to ensure a clean HIGH pulse:\n  digitalWrite(trigPin, LOW);\n  delayMicroseconds(5);\n  digitalWrite(trigPin, HIGH);\n  delayMicroseconds(10);\n  digitalWrite(trigPin, LOW);\n \n  \/\/ Read the signal from the sensor: a HIGH pulse whose\n  \/\/ duration is the time (in microseconds) from the sending\n  \/\/ of the ping to the reception of its echo off of an object.\n  pinMode(echoPin, INPUT);\n  duration = pulseIn(echoPin, HIGH);\n \n  \/\/ Convert the time into a distance\n  cm = (duration\/2) \/ 29.1;     \/\/ Divide by 29.1 or multiply by 0.0343\n  inches = (duration\/2) \/ 74;   \/\/ Divide by 74 or multiply by 0.0135\n  \n  Serial.print(inches);\n  Serial.print(&quot;in, &quot;);\n  Serial.print(cm);\n  Serial.print(&quot;cm&quot;);\n  Serial.println();\n  \n  delay(250);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Ultrasonic_Sensor_HC-SR04.c\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>First, you create variables for the trigger and echo pin called <span class=\"rnthl rntliteral\">trigPin<\/span> and <span class=\"rnthl rntliteral\">echoPin<\/span>, respectively. The trigger pin is connected to digital <span class=\"rnthl rntclblue\">Pin 11<\/span>, and the echo pin is connected to <span class=\"rnthl rntcyellow\">Pin 12<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int trigPin = 11;&nbsp;\nint echoPin = 12;<\/code><\/pre>\n\n\n\n<p>You also create three variables of type long: <span class=\"rnthl rntliteral\">duration<\/span> and <span class=\"rnthl rntliteral\">inches<\/span>. The <span class=\"rnthl rntliteral\">duration<\/span> variable saves the time between the emission and reception of the signal. The <span class=\"rnthl rntliteral\">cm<\/span> variable will save the distance in centimeters, and the <span class=\"rnthl rntliteral\">inches<\/span> variable will save the distance in inches.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>long duration, cm, inches;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the serial port at a baud rate of 9600, and set the trigger pin as an <span class=\"rnthl rntliteral\">OUTPUT<\/span> and the echo pin as an <span class=\"rnthl rntliteral\">INPUT<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Serial Port begin\nSerial.begin (9600);\n\/\/Define inputs and outputs\npinMode(trigPin, OUTPUT);\npinMode(echoPin, INPUT);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, trigger the sensor by sending a <span class=\"rnthl rntliteral\">HIGH<\/span> pulse of 10 microseconds. But, before that, give a short <span class=\"rnthl rntliteral\">LOW<\/span> pulse to ensure you&#8217;ll get a clean <span class=\"rnthl rntliteral\">HIGH<\/span> pulse:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite(trigPin, LOW);\ndelayMicroseconds(5);\ndigitalWrite(trigPin, HIGH);\ndelayMicroseconds(10);\ndigitalWrite(trigPin, LOW);<\/code><\/pre>\n\n\n\n<p>We use the&nbsp;<span class=\"rnthl rntliteral\">pulseIn()<\/span>&nbsp;function to get the sound wave travel time:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>duration = pulseIn(echoPin, HIGH);<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgreen\">The&nbsp;<span class=\"rnthl rntliteral\">pulseIn()<\/span>&nbsp;function reads a HIGH or a LOW pulse on a pin. It accepts as arguments the pin and the state of the pulse (either HIGH or LOW). It returns the length of the pulse in microseconds. The pulse length corresponds to the time it took to travel to the object plus the time traveled on the way back.<\/p>\n\n\n\n<p>Then, we calculate the distance to an object, taking into account the sound speed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted language-c\"><strong>distance = (traveltime\/2) x speed of sound<\/strong>\n\nThe speed of sound is: 343m\/s = 0.0343 cm\/uS = 1\/29.1 cm\/uS\n\nOr in inches:&nbsp;13503.9in\/s = 0.0135in\/uS = 1\/74in\/uS<\/pre>\n\n\n\n<p>We need to divide the travel time by 2 because we have to consider that the wave was sent, hit the object, and then returned to the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>cm = (duration\/2) \/ 29.1;\ninches = (duration\/2) \/ 74;<\/code><\/pre>\n\n\n\n<p>Finally, we print the results in the Serial Monitor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(inches);\nSerial.print(\"in, \");\nSerial.print(cm);\nSerial.print(\"cm\");\nSerial.println();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Source code with NewPing Library<\/h2>\n\n\n\n<p>You can also use the NewPing library. Download the library <a href=\"https:\/\/bitbucket.org\/teckel12\/arduino-new-ping\/get\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>After installing the NewPing library, you can upload the code provided below.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Posted on https:\/\/randomnerdtutorials.com\n * created by http:\/\/playground.arduino.cc\/Code\/NewPing\n*\/\n\n#include &lt;NewPing.h&gt;\n \n#define TRIGGER_PIN 11\n#define ECHO_PIN 12\n#define MAX_DISTANCE 200\n\n\/\/ NewPing setup of pins and maximum distance\nNewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); \n \nvoid setup() {\n   Serial.begin(9600);\n}\n \nvoid loop() {\n   delay(50);\n   unsigned int distance = sonar.ping_cm();\n   Serial.print(distance);\n   Serial.println(&quot;cm&quot;);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Ultrasonic_Sensor_HC-SR04_with_NewPing.c\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Getting the distance to an object using the NewPing library is much simpler.<\/p>\n\n\n\n<p>You start by including the NewPing library:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;NewPing.h&gt;<\/code><\/pre>\n\n\n\n<p>Then, define the trigger and echo pin. The trigger pin is connected to the Arduino digital <span class=\"rnthl rntclblue\">Pin 11<\/span> and the echo to <span class=\"rnthl rntcyellow\">Pin 12<\/span>. You also need to define the <span class=\"rnthl rntliteral\">MAX_DISTANCE<\/span> variable to be able to use the library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define TRIGGER_PIN 11\n#define ECHO_PIN 12\n#define MAX_DISTANCE 200<\/code><\/pre>\n\n\n\n<p>Then, you create a <span class=\"rnthl rntliteral\">NewPing<\/span> instance called <span class=\"rnthl rntliteral\">sonar<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, you initialize the Serial communication at a baud rate of 9600.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(9600);<\/code><\/pre>\n\n\n\n<p>Finally, in the <span class=\"rnthl rntliteral\">loop()<\/span>, you just need to use the <span class=\"rnthl rntliteral\">ping_cm()<\/span> method on the <span class=\"rnthl rntliteral\">sonar<\/span> object to get the distance in centimeters.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned int distance = sonar.ping_cm();<\/code><\/pre>\n\n\n\n<p>If you want to get the distance in inches, you can use <span class=\"rnthl rntliteral\">sonar.ping_in()<\/span> instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Arduino-UNO-Board-HC-SR04-Ultrasonic-Sensor-Module-Demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino UNO Board HC-SR04 Ultrasonic Sensor Module Arduino Demonstration\" class=\"wp-image-104467\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Arduino-UNO-Board-HC-SR04-Ultrasonic-Sensor-Module-Demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Arduino-UNO-Board-HC-SR04-Ultrasonic-Sensor-Module-Demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Upload the code to your Arduino board. Then, open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/serial-monitor.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"109\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/serial-monitor.png?resize=745%2C109&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-81671\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/serial-monitor.png?w=745&amp;quality=100&amp;strip=all&amp;ssl=1 745w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/serial-monitor.png?resize=300%2C44&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 745px) 100vw, 745px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The distance to the nearest object is printed in the Serial Monitor window.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"445\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-ultrasonic-sensor-example-demo.png?resize=601%2C445&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino with Ultrasonic Sensor Demo Serial Monitor\" class=\"wp-image-105366\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-ultrasonic-sensor-example-demo.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-ultrasonic-sensor-example-demo.png?resize=300%2C222&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this post, we&#8217;ve shown you how the HC-SR04 ultrasonic sensor works and how you can use it with the Arduino board. For a project example, you can build a<a href=\"https:\/\/randomnerdtutorials.com\/arduino-ultrasonic-sensor-with-leds-and-buzzer\/\" target=\"_blank\" rel=\"noreferrer noopener\"> Parking Sensor with LEDs and a buzzer<\/a>.<\/p>\n\n\n\n<p>If you are a beginner to the Arduino, we recommend following our <a href=\"https:\/\/randomnerdtutorials.com\/arduino-mini-course\/\">Arduino Mini-Course<\/a> that will help you get started&nbsp;quickly with this amazing board.<\/p>\n\n\n\n<p>If you like Arduino, you may also like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\">Arduino Step-by-step Projects course<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/guide-to-sd-card-module-with-arduino\/\" target=\"_blank\" rel=\"noreferrer noopener\">Guide to SD card module with Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/\" target=\"_blank\" rel=\"noreferrer noopener\">Guide to DHT11\/DHT22 Humidity and Temperature Sensor With Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-color-sensor-tcs230-tcs3200\/\" target=\"_blank\" rel=\"noreferrer noopener\">Guide for TCS230\/TCS3200 Color Sensor with Arduino<\/a><\/li><\/ul>\n\n\n\n<p>You can find <a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\" target=\"_blank\" rel=\"noreferrer noopener\">all our Arduino projects and tutorials here<\/a>.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is a guide about the Ultrasonic Sensor HC-SR04. We&#8217;ll explain how it works, show you some of its features and share an Arduino project example you can follow &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-ultrasonic-sensor-hc-sr04\/#more-1492\" aria-label=\"Read more about Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":105367,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,302,269,264],"tags":[],"class_list":["post-1492","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-0-guide","category-guide-project","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/Arduino-HC-SR04-Guide-Tutorial.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/1492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=1492"}],"version-history":[{"count":5,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/1492\/revisions"}],"predecessor-version":[{"id":107116,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/1492\/revisions\/107116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/105367"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=1492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=1492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=1492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}