{"id":698,"date":"2025-10-19T09:34:00","date_gmt":"2025-10-19T07:34:00","guid":{"rendered":"https:\/\/avlos.gr\/wpress\/?p=698"},"modified":"2025-10-19T11:03:47","modified_gmt":"2025-10-19T09:03:47","slug":"distance-sensor","status":"publish","type":"post","link":"https:\/\/avlos.gr\/wpress\/en\/distance-sensor\/","title":{"rendered":"Distance Sensor"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Ultrasonic Sensor HC-SR04 and Arduino<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article we will see how the HC-SR04 ultrasonic sensor works and how to use it with Arduino. This is the most popular sensor for measuring distance. We can use it to build obstacle avoidance robots with Arduino, and&nbsp;that&#8217;s what we&#8217;ll be doing next.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Description of the HC-SR04 material<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The HC-SR04 is an affordable and easy-to-use distance measurement sensor. It has a range from 2 cm to 400 cm. The sensor consists of two ultrasonic transducers. One is a transmitter that emits ultrasonic pulses of sound and the other is a receiver that listens to the reflected waves. It&#8217;s basically a SONAR like the ones submarines use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the figure below we see the image of the sensor.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"218\" height=\"216\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2.jpg\" alt=\"HC-SR04 sensor\" class=\"wp-image-700\" style=\"width:172px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2.jpg 218w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2-150x150.jpg 150w\" sizes=\"auto, (max-width: 218px) 100vw, 218px\" \/><figcaption class=\"wp-element-caption\">HC-SR04 sensor<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The sensor has 4 pins. VCC and GND go to the 5V and GND pins on the Arduino, and Trig and Echo go to any Arduino digital pin. Using the Trig pin we send the ultrasonic wave from the transmitter and with the Echo pin we receive the reflected signal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the HC-SR04 Ultrasonic Distance Sensor Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As shown in the image below the sensor emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle in its path it will be reflected back to the unit. By taking into account the travel time and the speed of sound we can calculate the distance. For this purpose we use the following basic formula to calculate the distance:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">D = U x T Distance equal speed times time.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"359\" height=\"165\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_2.jpg\" alt=\"Operation of the HC-SR04\" class=\"wp-image-701\" style=\"width:272px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_2.jpg 359w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_2-300x138.jpg 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><figcaption class=\"wp-element-caption\">Operation of the HC-SR04<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To generate the ultrasound we need to set the Trig pin high for 10 \u00b5s. This will send out a group of 8 ultrasound cycles that will travel at the speed of sound. The Echo spike goes up immediately after sending the group of 8 ultrasound cycles and starts listening or waiting for this wave to be reflected by an object. If there is no object or reflected pulse, the Echo pin will time out after 38 ms and return to the low state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we receive a reflected pulse, the Echo pin will fall earlier than this 38ms. Depending on the time the Echo pin was HIGH, we can determine the distance the sound wave traveled, thus the distance from the sensor to the object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The diagram below shows the function we described.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"142\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_3.jpg\" alt=\"HC-SR04 operating diagram\" class=\"wp-image-702\" style=\"width:343px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_3.jpg 378w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_3-300x113.jpg 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><figcaption class=\"wp-element-caption\">HC-SR04 operating diagram<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We actually know both the speed and time values. Time is the time the Echo pin was HIGH and velocity is the speed of sound which is 340 m\/s. There is one extra step we need to do, and that is to divide the final result by 2. We need to do this because we are measuring how long it takes the sound wave to travel to the object and reflect back. Assume the Echo pin was HIGH for 2 ms. If we want to express the result of the distance in cm, we can convert the value of the speed of sound from 340m\/s to 34cm\/ms.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So if the Echo pin was HIGH for 2ms (which we measure using the pulseIn() function for the Arduino, the distance from the sensor to the object is 34cm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">D = (U x T) \/ 2 = (34cm\/ms x 2ms) \/ 2 = 34cm.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to connect HC-SR04 ultrasonic sensor to Arduino<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The connection of the Arduino Uno with the distance sensor is shown in the figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"188\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_4.jpg\" alt=\"Connecting the HC-SR04\" class=\"wp-image-703\" style=\"width:269px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_4.jpg 339w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/\u0391\u03b9\u03c3\u03b8\u03b7\u03c4\u03ae\u03c1\u03b1\u03c2_\u0391\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2_4-300x166.jpg 300w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><figcaption class=\"wp-element-caption\">Connecting the HC-SR04<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The GRD and VCC\u00a0 pins of the module must be connected to the ground and 5V pins on the Arduino board respectively, and the trig and echo pins to any Digital I\/O pin on the Arduino board.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Arduino code for the HC-SR04 ultrasonic sensor<\/h2>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\/*\nUltrasonic Sensor HC-SR04 and Arduino Tutorial\nby Dejan Nedelkovski,\nwww.HowToMechatronics.com\n*\/\n\n\/\/ defines pins numbers\nconst int trigPin = 9;\nconst int echoPin = 10;\n\n\/\/ defines variables\nlong duration;\nint distance;\n\nvoid setup() {\npinMode(trigPin, OUTPUT); \/\/ Sets the trigPin as an Output\npinMode(echoPin, INPUT); \/\/ Sets the echoPin as an Input\nSerial.begin(9600); \/\/ Starts the serial communication\n}\n\nvoid loop() {\n\/\/ Clears the trigPin\ndigitalWrite(trigPin, LOW);\ndelayMicroseconds(2);\n\n\/\/ Sets the trigPin on HIGH state for 10 micro seconds\ndigitalWrite(trigPin, HIGH);\ndelayMicroseconds(10);\ndigitalWrite(trigPin, LOW);\n\n\/\/ Reads the echoPin, returns the sound wave travel time in microseconds\nduration = pulseIn(echoPin, HIGH);\n\n\/\/ Calculating the distance\n\ndistance = duration * 0.034 \/ 2;\n\n\/\/ Prints the distance on the Serial Monitor\nSerial.print(\"Distance: \");\nSerial.println(distance);\ndelay(500);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Explanation of the code<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First we need to define the Trig and Echo pins. In this case they are pins number 9 and 10 on the Arduino board and I call them trigPin and echoPin. We need a Long variable called &#8220;duration&#8221; for the travel time we will get from the sensor and an integer variable for the distance &#8220;distance&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\/\/ defines pins numbers\nconst int trigPin = 9;\nconst int echoPin = 10;\n\n\/\/ defines variables\nlong duration;\nlong distance;\n\nIn the setup we need to set trigPin as output and echoPin as input and also start serial communication to display the results on the screen.\n\nvoid setup() {\npinMode(trigPin, OUTPUT); \/\/ Sets the trigPin as an Output\npinMode(echoPin, INPUT); \/\/ Sets the echoPin as an Input\nSerial.begin(9600); \/\/ Starts the serial communication\n}\n\nIn the loop() loop we first need to make sure trigPin is clear, so we need to set this pin to LOW state for 2 \u00b5s. Now to generate the Ultra sound wave we need to set the trigPin to HIGH state for 10 \u00b5s.\n\n\/\/ Clears the trigPin\ndigitalWrite(trigPin, LOW);\ndelayMicroseconds(2);\n\n\/\/ Sets the trigPin on HIGH state for 10 micro seconds\ndigitalWrite(trigPin, HIGH);\ndelayMicroseconds(10);\ndigitalWrite(trigPin, LOW);\n\nUsing the pulseIn() function we read the travel time and put that value into the 'duration' variable. This function has 2 parameters, the first is the name of the Echo pin and the second is the state of the pulse we are reading, either High or Low.\n\n\/\/ Reads the echoPin, returns the sound wave travel time in microseconds\nduration = pulseIn(echoPin, HIGH);\n\nIn this case, we need this set to HIGH, as the HC-SR04 sensors set the Echo pin to High after the transmitter sends the 8-cycle ultrasonic burst. This essentially starts the timing and once we receive the reflected sound wave the Echo pin will go Low which stops the timing. Finally the function will return the pulse length in microseconds.\n\nTo get the distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation earlier.\n\n\/\/ Reads the echoPin, returns the sound wave travel time in microseconds\nduration = pulseIn(echoPin, HIGH);\n\n\/\/ Calculating the distance\ndistance = duration * 0.034 \/ 2;\n\u03a3\u03c4\u03bf \u03c4\u03ad\u03bb\u03bf\u03c2 \u03b8\u03b1 \u03b5\u03ba\u03c4\u03c5\u03c0\u03ce\u03c3\u03bf\u03c5\u03bc\u03b5 \u03c4\u03b7\u03bd \u03c4\u03b9\u03bc\u03ae \u03c4\u03b7\u03c2 \u03b1\u03c0\u03cc\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2 \u03c3\u03c4\u03b7\u03bd \u03bf\u03b8\u03cc\u03bd\u03b7.\n\nThe information I quoted I got mainly from the site\n\nhttps:&#47;&#47;howtomechatronics.com\/tutorials\/arduino\/ultrasonic-sensor-hc-sr04\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ultrasonic Sensor HC-SR04 and Arduino In this article we will see how the HC-SR04 ultrasonic sensor works and how to use it with Arduino. This is the most popular sensor for measuring distance. We can use it to build obstacle&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,75],"tags":[73],"class_list":["post-698","post","type-post","status-publish","format-standard","hentry","category-home","category-robotics","tag-robotics"],"_links":{"self":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/comments?post=698"}],"version-history":[{"count":8,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/698\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/698\/revisions\/710"}],"wp:attachment":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/media?parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/categories?post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/tags?post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}