{"id":8809,"date":"2018-04-17T01:20:25","date_gmt":"2018-04-17T05:20:25","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=8809"},"modified":"2018-04-25T03:30:06","modified_gmt":"2018-04-25T07:30:06","slug":"arduino-tutorials-for-testers-ultrasonic-sensor","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/arduino-tutorials-for-testers-ultrasonic-sensor\/","title":{"rendered":"Arduino Tutorials for Testers: Ultrasonic Sensor"},"content":{"rendered":"<p>In this tutorial, you will learn about how to interface ultrasonic sensor with Arduino and how to measure the distance of an object using ultrasonic sensor. This is the sixth tutorial of our &#8216;Arduino tutorials for testers&#8217; series. We are presenting some of the most commonly used hardware components along with some basics of electronics theory. So, if you are the kind that learns by doing, follow along!<\/p>\n<hr \/>\n<h3>Components Required:<\/h3>\n<ol>\n<li>Arduino Board<\/li>\n<li>Jumper Wires<\/li>\n<li>Any Object as a target (Book, mobile, human hand, etc)<\/li>\n<li>Ultrasonic Sensor (HC-SR04)<\/li>\n<\/ol>\n<figure id=\"attachment_8810\" aria-describedby=\"caption-attachment-8810\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/1_ODZShyJXz7Hi_J9VEbGa-Q.jpeg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8810 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/1_ODZShyJXz7Hi_J9VEbGa-Q.jpeg\" alt=\"Ultrasonic Sensor\" width=\"550\" height=\"327\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/1_ODZShyJXz7Hi_J9VEbGa-Q.jpeg 550w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/1_ODZShyJXz7Hi_J9VEbGa-Q-300x178.jpeg 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-8810\" class=\"wp-caption-text\">Fig. 1 Ultrasonic Sensor<\/figcaption><\/figure>\n<p>For this tutorial, we are using HC-SR04 ultrasonic sensor which has 4 pins as shown in the above figure 1. Ultrasonic sensors are very helpful in detecting objects and measuring the distance of an object. This sensor works based on the working principle of <strong>SONAR<\/strong> (<span>SOund Navigation And Ranging) and you can look at the datasheet <a href=\"http:\/\/www.micropik.com\/PDF\/HCSR04.pdf\">here<\/a>\u00a0for more specifications.<\/span><\/p>\n<p><strong>Note:<\/strong> You can get these components fairly easily at many places (including online). We bought for our engineers this <a href=\"https:\/\/www.amazon.in\/dp\/B01KNR581U\/ref=ox_ya_os_product\">Arduino super-starter kit<\/a>.<\/p>\n<hr \/>\n<h3>Working Principle of SONAR:<\/h3>\n<figure id=\"attachment_8811\" aria-describedby=\"caption-attachment-8811\" style=\"width: 2000px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Sonar_Principle.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8811 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Sonar_Principle.png\" alt=\"Working Principle of SONAR\" width=\"2000\" height=\"1073\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Sonar_Principle.png 2000w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Sonar_Principle-300x161.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Sonar_Principle-768x412.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Sonar_Principle-1024x549.png 1024w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/a><figcaption id=\"caption-attachment-8811\" class=\"wp-caption-text\">Fig. 2 Working Principle of SONAR<\/figcaption><\/figure>\n<p>In SONAR, the transmitter (Tx) sends a sound wave towards the targeted object. Sound waves travel from a transmitter toward an object and\u00a0reflect back from an object. The reflected sound wave travels back towards receiver and receiver will receive the waves. So, here SONAR system calculates the time required for the sound wave to travel from a transmitter to object and from object to a receiver. And the speed of the sound wave in the same medium is constant. So, we can calculate the distance of an object using the standard distance formula.<\/p>\n<p style=\"text-align: center;\">Distance = Speed X Time<\/p>\n<p><strong>Note:<\/strong> Here, you need to divide the time\/distance by 2 as the calculated time is the time required for round trip.<\/p>\n<p>For the ultrasonic sensor, when we send a trigger pulse\/signal on trigger pin, a sensor sends waves towards a targeted object and we start looking at Echo pin to get the response\u00a0of reflected\u00a0wave. When sensor gets a trigger, it transmits\u00a0the waves and set the echo pin to HIGH and the status of the Echo pin will be HIGH until it will not receive\u00a0the reflected wave. So, from the width\u00a0of the echo pulse, we can calculate the traveling time of transmitted and reflected wave. To trigger the ultrasonic sensor, you need to generate a HIGH pulse for at least\u00a010 microseconds on the trigger pin.<\/p>\n<hr \/>\n<h3>Pin Details of Ultrasonic Sensor:<\/h3>\n<ol>\n<li>Vcc &#8211; Vcc pin powers the sensor with +5V<\/li>\n<li>Trigger &#8211; It is an\u00a0input pin. To initialize\u00a0the measurement by sending ultrasonic sound waves, you need to provide a high signal for 10 microseconds on this pin.<\/li>\n<li>Echo &#8211; It is an Output pin.\u00a0<span>This pin goes high for a period of time which will be equal to the time taken for the ultrasonic wave to return back to the sensor.<\/span><\/li>\n<li>GND &#8211;\u00a0<span>This pin is connected to the Ground of the system.<\/span><\/li>\n<\/ol>\n<hr \/>\n<h3>Interfacing of Ultrasonic Sensor with Arduino UNO:<\/h3>\n<figure id=\"attachment_8812\" aria-describedby=\"caption-attachment-8812\" style=\"width: 717px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/ultrasonic.jpg\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8812 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/ultrasonic.jpg\" alt=\"Interfacing of Ultrasonic Sensor with Arduino Uno\" width=\"717\" height=\"567\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/ultrasonic.jpg 717w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/ultrasonic-300x237.jpg 300w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/a><figcaption id=\"caption-attachment-8812\" class=\"wp-caption-text\">Fig.3 Interfacing of Ultrasonic Sensor with Arduino Uno<\/figcaption><\/figure>\n<p><strong>Wiring Details:<\/strong><\/p>\n<ul>\n<li>Connect Trigger pin of a sensor to pin 13 of Arduino<\/li>\n<li>Connect Echo pin of a\u00a0sensor to pin 11 of Arduino<\/li>\n<li>Connect Vcc pin of a\u00a0sensor to Vcc pin of Arduino<\/li>\n<li>Connect GND pin of a\u00a0sensor to GND pin of Arduino<\/li>\n<\/ul>\n<p>Look at the above figure 3 and wiring details to interface ultrasonic sensor with Arduino Uno.<\/p>\n<hr \/>\n<h3>Write code to measure the distance\u00a0of targeted object:<\/h3>\n<p><strong>Things we know:<\/strong><\/p>\n<ol>\n<li>We will get the time required for ultrasonic waves to fly back from the targeted object by reading the Echo pin of the ultrasonic sensor.<\/li>\n<li>The speed of ultrasonic wave at room temperature is approximately\u00a0<span style=\"font-weight: 400;\">776.5 miles per hour (or you can calculate exact speed of waves by keeping the object at a known distance. Here, you know the distance, you will get traveling time from the sensor, now you need to use a formula to calculate the speed.<\/span><\/li>\n<li>Now, we need to calculate the distance using a formula:\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Distance = Speed X Time<\/li>\n<\/ol>\n<h4>Arduino Sketch\/Code:<\/h4>\n<pre lang=\"arduino\">int trigPin=13;  \/\/Sensor Trigger pin connected to Arduino pin 13\r\nint echoPin=11;  \/\/Sensor Echo pin connected to Arduino pin 11\r\nfloat pingTime;  \/\/time for ping to travel from sensor to target and return\r\nfloat distance;  \/\/Distance to Target in inches\r\nfloat speedOfSound=776.5; \/\/Speed of sound in miles per hour\r\n\r\nvoid setup() {\r\n  \/\/ put your setup code here, to run once:\r\n  Serial.begin(9600);        \/\/ Initialize serial monitor\r\n  pinMode(trigPin, OUTPUT);  \/\/ Set trigger pin as an output pin\r\n  pinMode(echoPin, INPUT);   \/\/ Set echo pin as an output pin\r\n}\r\n\r\nvoid loop() {\r\n  \/\/ put your main code here, to run repeatedly:\r\n  \/\/ Generate a high signal for min. 10us to give trigger to sensor   \r\n  digitalWrite(trigPin, LOW);  \/\/Set trigger pin low\r\n  delayMicroseconds(2000);     \/\/Let signal settle\r\n  digitalWrite(trigPin, HIGH); \/\/Set trigPin high\r\n  delayMicroseconds(15);       \/\/Delay in high state\r\n  digitalWrite(trigPin, LOW);  \/\/ping has now been sent\r\n\r\n  \/\/ Capture the High Width Pulse on Echo pin to calculate the travel time (pingTime) of a wave. \r\n  \/\/ pulseIn() function calculates the time from width of pulse \r\n  pingTime = pulseIn(echoPin, HIGH);  \/\/pingTime is presented in microceconds\r\n  \r\n  \/\/Print pingTime on Serial Monitor\r\n  Serial.print(\"pingTime : \");         \r\n  Serial.print(pingTime);\r\n  Serial.println(\" microseconds.\");            \r\n  \r\n  \/\/ Do conversation in units and Calculate Distance in cm using formula\r\n  pingTime=pingTime\/1000000; \/\/convert pingTime to seconds by dividing by 1000000 (microseconds in a second)\r\n  pingTime=pingTime\/3600; \/\/convert pingtime to hours by dividing by 3600 (seconds in an hour)\r\n  distance= speedofSound * pingTime;  \/\/This will be in miles, since speed of sound was miles per hour\r\n  distance=distance\/2; \/\/Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.\r\n  distance = distance*160934;  \/\/ distance in cm\r\n  \r\n  \/\/ Print distance on Serial Monitor\r\n  Serial.print(\"Target Distance: \");\r\n  Serial.print(distance);\r\n  Serial.println(\" cm.\");\r\n  delay(300);\r\n}\r\n<\/pre>\n<p>Above code\/sketch uses the <strong>pulseIn()<\/strong> function to read the travel time. This function requires 2 parameters. The first one is the name of the echo pin and the second one, you can write either HIGH or LOW. In the current case, we set that to HIGH. It means that the pulseIn() function will wait for the echo pin to go HIGH and it will start a timer, then it will wait for the echo pin to go LOW when the sound wave will fly-back which will stop the timer. In the end, the function will return the length of the pulse in microseconds.<\/p>\n<p>Upload the above sketch, put the object (mobile, book) in front of the sensor around 25 -50 cm away. Now, open the serial monitor and you will see the ping time and distance printed on the screen. Move the object near to and away from the sensor and see the distance. For us, this sensor is accurate up to 100 cm.<\/p>\n<hr \/>\n<p>Hope this tutorial was helpful to you. You can use this ultrasonic sensor for the number of different applications,\u00a0for example: detecting an object (in any anti-collision bots\/drones, in car parking guidance, detecting empty parking place in the parking lots, etc). In the next tutorial <a href=\"https:\/\/qxf2.com\/blog\/arduino-tutorials-for-testers-decoding-of-ir-receiver\/\">here<\/a>, you will learn about decoding of IR remote and this will take your skills to the next level. You will enter into the world of wireless. You can design and controls things with IR remote. You will get more idea about how products like TV, AC, Music Player were programmed.\u00a0If you follow along, at the end of this <a href=\"https:\/\/qxf2.com\/blog\/arduino-tutorial-series-for-testers\/\">series<\/a>, you will distinguish how hardware devices\/instruments\/products are designed, you can also guess how they were programmed, you will get ideas about how to test hardware and you can build and help others in building products.<\/p>\n<p><strong>Learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2 Services<\/a>.<\/strong><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about how to interface ultrasonic sensor with Arduino and how to measure the distance of an object using ultrasonic sensor. This is the sixth tutorial of our &#8216;Arduino tutorials for testers&#8217; series. We are presenting some of the most commonly used hardware components along with some basics of electronics theory. So, if you are [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159,117],"tags":[119],"class_list":["post-8809","post","type-post","status-publish","format-standard","hentry","category-arduino","category-hardware-testing","tag-arduino"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/8809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=8809"}],"version-history":[{"count":17,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/8809\/revisions"}],"predecessor-version":[{"id":9311,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/8809\/revisions\/9311"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=8809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=8809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=8809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}