{"id":1677,"date":"2024-06-26T16:57:17","date_gmt":"2024-06-26T16:57:17","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1677"},"modified":"2024-08-11T16:54:24","modified_gmt":"2024-08-11T16:54:24","slug":"ultrasonic-sensor","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1677","title":{"rendered":"Ultrasonic Sensor"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-784623dc004497c5566bd09e593129a5 wp-block-paragraph\" style=\"color:#5c5c5c\">Ultrasonic sensors use sound to determine the distance between the sensor and the closest object in its path. The sensor sends out a sound wave at a specific frequency. It then listens for that specific sound wave to bounce off of an object and come back. The sensor keeps track of the time between sending the sound wave and the sound wave returning.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"311\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/ultrasonic-sensor-1024x311.jpg\" alt=\"\" class=\"wp-image-1679\" style=\"width:784px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/ultrasonic-sensor-1024x311.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/ultrasonic-sensor-300x91.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/ultrasonic-sensor-768x233.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/ultrasonic-sensor.jpg 1317w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"249\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pin-of-ultrasonic-sensor-1024x249.jpg\" alt=\"\" class=\"wp-image-1681\" style=\"width:646px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pin-of-ultrasonic-sensor-1024x249.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pin-of-ultrasonic-sensor-300x73.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pin-of-ultrasonic-sensor-768x187.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pin-of-ultrasonic-sensor.jpg 1247w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"400\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/timing-of-ultrasonic-sensor-1024x400.jpg\" alt=\"\" class=\"wp-image-1684\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/timing-of-ultrasonic-sensor-1024x400.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/timing-of-ultrasonic-sensor-300x117.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/timing-of-ultrasonic-sensor-768x300.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/timing-of-ultrasonic-sensor.jpg 1349w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d0707aa206c22e42d1b56db73c26763c wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s initialize the sensor with 10\u00b5s pulse. Wait until ECHO pin goes high then start the timer. Wait until ECHO pin goes low then stop the timer. Let\u2019s initialize the Timer0 with overflow vect. Since overflow gives only 32\u00b5s we have to calculate all the value. Our first target is to initialize the sensor<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"43\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-3-1.jpg\" alt=\"\" class=\"wp-image-1687\" style=\"width:436px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-3-1.jpg 816w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-3-1-300x16.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-3-1-768x40.jpg 768w\" sizes=\"(max-width: 816px) 100vw, 816px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include&lt;avr\/interrupt.h&gt;\n#define DDR_TR        DDRB\n#define DDR_ECHO      DDRB\n#define DDR_TR_PIN    DDB4\n#define DDR_ECHO_PIN  DDB3\n#define PORT_TR       PORTB\n#define PORT_TR_PIN   PORTB4\n#define PORT_ECHO     PINB\n#define PORT_ECHO_PIN PINB3\nDDR_TR|=(1&lt;&lt;DDR_TR_PIN);\nDDR_ECHO&amp;=~(1&lt;&lt;DDR_ECHO_PIN);\nsei();\nTIMSK|=(1&lt;&lt;TOIE0);\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"45\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-1.jpg\" alt=\"\" class=\"wp-image-1690\" style=\"width:367px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-1.jpg 689w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-1-300x20.jpg 300w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t ultrasonic(void)\n{\n    uint8_t value;\n    PORT_TR|=(1&lt;&lt;PORT_TR_PIN);\n\t_delay_us(10);\n\tPORT_TR&amp;=~(1&lt;&lt;PORT_TR_PIN);\n\twhile(!(PORT_ECHO&amp;(1&lt;&lt;PORT_ECHO_PIN)));\n\ttimer0_init();\n\twhile(PORT_ECHO&amp;(1&lt;&lt;PORT_ECHO_PIN));\n\ttimer0_stop();\n\tvalue=TCNT0;\n\treturn value;\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"193\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-1024x193.jpg\" alt=\"\" class=\"wp-image-1695\" style=\"width:690px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-1024x193.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-300x56.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-768x144.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5.jpg 1383w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&lt;avr\/interrupt.h&gt;\n  #include&quot;ultrasonic.h&quot;\n  #include&quot;lcd.h&quot;\nchar str&#x5B;40];\nuint8_t time,overflow=1;\nfloat distance;\nint main(void)\n  {\n   LCD_INIT();         \/\/LCD intilize\n   system_init();\n   LCD_write_string(1,1,&quot;Object tracking&quot;);\n   while(1)\n     {\n\t    time=ultrasonic();\n\t    if(overflow==1){\n\t\t   distance=.5488*time;\n\t    }else if(overflow==2){\n\t\t   distance=.5488*time+140.5;\n\t\t   overflow=1;\n\t    }else if(overflow==3){\n\t\t   distance=.5488*time+281;\n\t\t   overflow=1;\n\t    }else if(overflow==4){\n\t\t   distance=.5488*time+421.5;\n\t\t   overflow=1;\n\t    }\n\t    sprintf(str,&quot;Distance:%.4f&quot;,distance);\n\t    LCD_write_string(1,2,str);\n\t    _delay_ms(1000); \/\/delay for LCD update\n     }\n return 0;\n}\nISR(TIMER0_OVF_vect)\n{\n\toverflow++;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-321985109a2ce685598af9bb1a58877a wp-block-paragraph\" style=\"color:#2f2f2f\">We just introduce the basic object detection. You can use it for water level detection in a reservoir tank or any object nearby. We can also use it in mini radar technology that will be discussed in ARM section.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Ultrasonic-Sensor.rar\" style=\"border-radius:10px;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-17a795776e992f41d589c0d2becd3090 wp-block-paragraph\" style=\"color:#222222\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Ultrasonic-Sensor.rar\">Ultrasonic Sensor.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ultrasonic sensors use sound to determine the distance between the sensor and the closest object in its path. The sensor sends out a sound wave at a specific frequency. It then listens for that specific sound wave to bounce off of an object and come back. The sensor keeps track of the time between sending [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1677","post","type-post","status-publish","format-standard","hentry","category-timer-counter"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1677"}],"version-history":[{"count":13,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1677\/revisions"}],"predecessor-version":[{"id":2605,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1677\/revisions\/2605"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}