








{"id":127,"date":"2020-10-12T21:36:39","date_gmt":"2020-10-12T21:36:39","guid":{"rendered":"https:\/\/osoyoo.info\/?p=127"},"modified":"2020-10-13T22:14:34","modified_gmt":"2020-10-13T22:14:34","slug":"arduino-basic-lesson-23-ultrasonic-sensor","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-23-ultrasonic-sensor\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 23: Ultrasonic Sensor"},"content":{"rendered":"<table>\n<tbody>\n<tr>\n<td>\n<div align=\"center\"><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-22-ir-track-sensor\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/div>\n<\/td>\n<td>\n<div align=\"center\"><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/div>\n<\/td>\n<td>\n<div align=\"center\"><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-24-sound-detection-sensor\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/p>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#3\">About the Ultrasonic Sensor HC-SR04<\/a>\n<ul>\n<li><a href=\"#3.1\">Features of HC-SR04<\/a><\/li>\n<li><a href=\"#3.2\">What is Ultrasonic Sensor ?<\/a><\/li>\n<li><a href=\"#3.3\">What is HC-SR04 ?<\/a><\/li>\n<li><a href=\"#3.4\">Timing diagram<\/a><\/li>\n<li><a href=\"#3.5\">Notice<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#4\">Examples<\/a>\n<ul>\n<li><a href=\"#4.1\">Using the Ultrasonic Sensor HC-SR04 Read Distance from the Serial Monitor<\/a>\n<ul>\n<li><a href=\"#4.1.1\">Circuit Diagram<\/a><\/li>\n<li><a href=\"#4.1.2\">Code Program<\/a><\/li>\n<li><a href=\"#4.1.3\">Running Result<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#4.2\">Display the Distance from the HC-SR04 Ultrasonic Sensor on an I2C LCD<\/a>\n<ul>\n<li><a href=\"#4.2.1\">Circuit Diagram<\/a><\/li>\n<li><a href=\"#4.2.2\">Code Program<\/a><\/li>\n<li><a href=\"#4.2.3\">Running Result<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p>The Ultrasonic Sensor sends out a high-frequency sound pulse and then times how long it takes for the echo of the sound to reflect back. The sensor has 2 openings on its front. One opening transmits ultrasonic waves, the other receives them. In this lesson we will show you how the HC-SR04 Ultrasonic Sensor works and how to use it with the Osoyoo Uno board.<\/p>\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO magic I\/O Shield for Arduino x1<\/li>\n<li>OSOYOO Ultrasonic Sensor HC-SR04 x 1<\/li>\n<li>OSOYOO I2C LCD1602 x 1<\/li>\n<li>4 pin PnP cable x 2<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<h2 id=\"2.2\">Software<\/h2>\n<ul>\n<li>Arduino IDE (version 1.6.4+)<\/li>\n<li>LiquidCrystal_I2C.h<\/li>\n<li>Wire.h<\/li>\n<\/ul>\n<h1>About Ultrasonic Sensor HC-SR04<\/h1>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson24\/1.jpg\" width=\"1000\" height=\"1000\" \/><\/p>\n<h2 id=\"3.1\"><b>Features of HC-SR04<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Power Supply :+5V DC<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Quiescent Current : &lt;2mA<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Working Currnt: 15mA<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Effectual Angle: &lt;15\u00b0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Ranging Distance : 2cm \u2013 400 cm\/1&#8243; &#8211; 13ft<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Resolution : 0.3 cm<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Measuring Angle: 30 degree<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Trigger Input Pulse width: 10uS<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Dimension: 45mm x 20mm x 15mm <\/span><\/li>\n<\/ul>\n<h2 id=\"3.2\">What is Ultrasonic Sensor ?<\/h2>\n<p>An Ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It measures distance by sending out a sound wave at a specific frequency and listening for that sound wave to bounce back. By recording the elapsed time between the sound wave being generated and the sound wave bouncing back, it is possible to calculate the distance between the sonar sensor and the object.<\/p>\n<h2 id=\"3.3\">What is <b>HC-SR04 ?<\/b><\/h2>\n<p>The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1\u201d to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson24\/HC-SR04.jpg\" width=\"1000\" height=\"566\" \/><\/p>\n<p>On the front of the ultrasonic range finder are two metal cylinders. These are transducers. Transducers convert mechanical forces into electrical signals. In the ultrasonic range finder, there is a transmitting transducer and receiving transducer. The transmitting transducer converts an electrical signal into the ultrasonic pulse, and the receiving transducer converts the reflected ultrasonic pulse back into an electrical signal. If you look at the back of the range finder, you will see an IC behind the transmitting transducer labelled MAX3232. This is the IC that controls the transmitting transducer. Behind the receiving transducer is an IC labelled LM324. This is a quad Op-Amp that amplifies the signal generated by the receiving transducer into a signal that\u2019s strong enough to transmit to the Arduino.<\/p>\n<h2 id=\"3.4\">Timing diagram<\/h2>\n<p><span style=\"font-weight: 400;\">The timing diagram of <\/span><span style=\"font-weight: 400;\">HC-SR04<\/span><span style=\"font-weight: 400;\"> is shown. To start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from receiver, it will set the Echo pin to high (5V) and delay for a period (width) which proportion to distance. To obtain the distance, measure the width (Ton) of Echo pin. <\/span><\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/timing-diagram-1.jpg\"><img loading=\"lazy\" class=\"wp-image-9376 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/timing-diagram-1.jpg\" alt=\"\" width=\"584\" height=\"396\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Time = Width of Echo pulse, in us (micro second)<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Distance in centimeters = Time \/ 58<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Distance in inches = Time \/ 148<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Or you can utilize the speed of sound, s<\/span>ince it is known that sound travels through air at about 344 m\/s (1129 ft\/s), you can take the time for the sound wave to return and multiply it by 344 meters (or 1129 feet) to find the total round-trip distance of the sound wave. Round-trip means that the sound wave traveled 2 times the distance to the object before it was detected by the sensor; it includes the &#8216;trip&#8217; from the sonar sensor to the object AND the &#8216;trip&#8217; from the object to the Ultrasonic sensor (after the sound wave bounced off the object). To find the distance to the object, simply divide the round-trip distance in half.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/400px-Ultrasonic_Sensor_Distance_Formula.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-9373\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/400px-Ultrasonic_Sensor_Distance_Formula.png\" alt=\"\" width=\"400\" height=\"71\" \/><\/a><\/p>\n<p>The time variable is the time it takes for the ultrasonic pulse to leave the sensor, bounce off the object, and return to the sensor. We actually divide this time in half since we only need to measure the distance to the object, not the distance to the object <em>and<\/em> back to the sensor. The speed variable is the speed at which sound travels through air.<\/p>\n<p>The speed of sound in air changes with temperature and humidity. Therefore, in order to accurately calculate distance, we\u2019ll need to consider the ambient temperature and humidity. The formula for the speed of sound in air with temperature and humidity accounted for is:<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/latex.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-9374\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/latex.png\" alt=\"\" width=\"428\" height=\"119\" \/><\/a><\/p>\n<p>For example, at 20 \u00b0C and 50% humidity, sound travels at a speed of:<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/latex-1.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-9375\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/latex-1.png\" alt=\"\" width=\"285\" height=\"38\" \/><\/a><\/p>\n<h2 id=\"3.5\">Note<\/h2>\n<p>The accuracy of Ultrasonic sensor can be affected by the temperature and humidity of the air it is being used in. However, for these tutorials and almost any project you will be using these sensors in, this change in accuracy will be negligible.<br \/>\nIt is important to understand that some objects might not be detected by ultrasonic sensors. This is because some objects are shaped or positioned in such a way that the sound wave bounces off the object, but are deflected away from the Ultrasonic sensor. It is also possible for the object to be too small to reflect enough of the sound wave back to the sensor to be detected. Other objects can absorb the sound wave all together (cloth, carpeting, etc), which means that there is no way for the sensor to detect them accurately. These are important factors to consider when designing and programming a robot using an ultrasonic sensor.<\/p>\n<h1 id=\"4\">Examples<\/h1>\n<h2 id=\"4.1\">Using the Ultrasonic Sensor HC-SR04 Read Distance from the Serial Monitor<\/h2>\n<p>We will start with a simple ultrasonic range finder that measures the output distance to the serial monitor.<\/p>\n<h3 id=\"4.1.1\">Connection<\/h3>\n<p>The HC-SR04 ultrasonic range finder has four pins: Vcc, Trig, Echo, and GND. The Vcc pin supplies the power to generate the ultrasonic pulses. The GND pin is connected to ground. The Trig pin is where the Arduino sends the signal to start the ultrasonic pulse. The Echo pin is where the ultrasonic range finder sends the information about the duration of the trip taken by the ultrasonic pulse to the Osoyoo Uno board.<\/p>\n<p>First, please plug OSOYOO Magic I\/O shield into UNO board:<\/p>\n<p><img loading=\"lazy\" class=\"size-full aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson4\/UNO%2BBase-min.png\" width=\"1000\" height=\"600\" \/><\/p>\n<p>Then connect the Ultrasonic Sensor to the port D7D8 of the Magic I\/O shield with 4-pin PNP cable as below digram:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson24\/ultrasonic.jpg\" width=\"1010\" height=\"614\" \/><\/p>\n<h3 id=\"4.1.2\">Code Program<\/h3>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up <a href=\"https:\/\/osoyoo.com\/driver\/HC-SR04_Ultrasonic_Sensor.zip\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/osoyoo.com\/driver\/HC-SR04_Ultrasonic_Sensor.zip<\/a> onto your Arduino.<\/p>\n<p>#define echoPin 8<br \/>\n#define trigPin 7<\/p>\n<p>void setup() {<br \/>\nSerial.begin (9600);<br \/>\npinMode(trigPin, OUTPUT);<br \/>\npinMode(echoPin, INPUT);<br \/>\n}<\/p>\n<p>void loop() {<br \/>\nint distance;<\/p>\n<p>distance=watch();<br \/>\nSerial.print(&#8220;Distance = &#8220;);<br \/>\nSerial.print(distance);<br \/>\nSerial.println(&#8221; cm&#8221;);<br \/>\ndelay(500);<\/p>\n<p>}<br \/>\nint watch(){<br \/>\nlong echo_distance;<br \/>\ndigitalWrite(trigPin,LOW);<br \/>\ndelayMicroseconds(5);<br \/>\ndigitalWrite(trigPin,HIGH);<br \/>\ndelayMicroseconds(15);<br \/>\ndigitalWrite(trigPin,LOW);<br \/>\necho_distance=pulseIn(echoPin,HIGH);<br \/>\necho_distance=echo_distance*0.01657; \/\/how far away is the object in cm<br \/>\nSerial.println((int)echo_distance);<br \/>\nreturn echo_distance;<br \/>\n}<\/p>\n<h3 id=\"4.1.3\">Running Result<\/h3>\n<p>A few seconds after the upload finishes, open the Serial Monitor, move the baffle, you will see the distance as below:<\/p>\n<p><center><iframe src=\"https:\/\/www.youtube.com\/embed\/FG5jbTg01iM\" width=\"1280\" height=\"720\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center><strong>Note: <\/strong>Receiving \u201c<strong>Out of range\u201d <\/strong>means <span class=\"\">the distance from the ultrasonic sensor is greater than or equal to 400 cm, or less than or equal to 2 cm.<\/span><\/p>\n<h2 id=\"4.2\">Display the Distance from the HC-SR04 Ultrasonic Sensor on an I2C LCD<\/h2>\n<p>If you want to display the results from the HC-SR04 Ultrasonic Sensor on an I2C LCD you can use the following source.<\/p>\n<h3 id=\"4.2.1\">Connection<\/h3>\n<p>Connect the Ultrasonic Sensor to the port D7D8 of the Magic I\/O shield with 4-pin PNP cables, and connect the I2C LCD1602 to the port I2C of the Magic I\/O shield with 4-pin PNP cable as below digram:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson24\/1602+ultrasonic.jpg\" width=\"1010\" height=\"614\" \/><\/p>\n<h3 id=\"4.2.2\">Code Program<\/h3>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up <a href=\"https:\/\/github.com\/osoyoo\/Osoyoo-development-kits\/tree\/master\/Osoyoo%20lessons%20for%20Arduino\/HC-SR04_Ultrasonic_Sensor\" target=\"_blank\" rel=\"noopener noreferrer\">the following sketch <\/a>onto your Arduino.<\/p>\n<p>Please make sure you have installed the libraries LiquidCrystal_I2C.h<\/p>\n<pre> <span style=\"color: #5e6d03;\">#include<\/span> <span style=\"color: #434f54;\">&lt;<\/span><span style=\"color: #d35400;\">Wire<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #000000;\">h<\/span><span style=\"color: #434f54;\">&gt;<\/span>\r\n<span style=\"color: #5e6d03;\">#include<\/span> <span style=\"color: #434f54;\">&lt;<\/span><b><span style=\"color: #d35400;\">LiquidCrystal_I2C<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #000000;\">h<\/span><span style=\"color: #434f54;\">&gt;<\/span>\r\n<b><span style=\"color: #d35400;\">LiquidCrystal_I2C<\/span><\/b> <span style=\"color: #000000;\">lcd<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">0x27<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">16<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">2<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">echoPin<\/span> <span style=\"color: #000000;\">8<\/span>\r\n<span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">trigPin<\/span> <span style=\"color: #000000;\">7<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">init<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">backlight<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">begin<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">9600<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">trigPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">echoPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">INPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #00979c;\">float<\/span> <span style=\"color: #000000;\">duration<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">distance<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">trigPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \r\n \u00a0<span style=\"color: #d35400;\">delayMicroseconds<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">2<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n  <span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">trigPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">delayMicroseconds<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">10<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">trigPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n  <span style=\"color: #000000;\">duration<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #d35400;\">pulseIn<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">echoPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">distance<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">duration<\/span> <span style=\"color: #434f54;\">\/<\/span> <span style=\"color: #000000;\">2<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #434f54;\">*<\/span> <span style=\"color: #000000;\">0.0344<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"Distance = \"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">distance<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\" cm\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">setCursor<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"Distance:\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">setCursor<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\" \"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">setCursor<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">9<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">distance<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">setCursor<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">14<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #000000;\">lcd<\/span><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"cm\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">400<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<\/pre>\n<h3 id=\"4.2.3\">Running Result<\/h3>\n<p>A few seconds after the upload finishes, move a board close to the sensor or remove it farther. You can see the value displayed on the LCD changes accordingly; it indicates the distance between the board and the ultrasonic sensor.<\/p>\n<p><center><iframe src=\"https:\/\/www.youtube.com\/embed\/AkNWeho8cUQ\" width=\"1280\" height=\"720\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-22-ir-track-sensor\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a> <a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a> <a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-24-sound-detection-sensor\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software About the Ultrasonic Sensor HC-SR04 Features of HC-SR04 What is Ultrasonic Sensor ? What is HC-SR04 ? Timing diagram Notice Examples Using the Ultrasonic Sensor HC-SR04 Read Distance from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":11,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions\/141"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/166"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}