{"id":989,"date":"2018-08-12T19:50:43","date_gmt":"2018-08-12T19:50:43","guid":{"rendered":"https:\/\/zhutarduino.wordpress.com\/?page_id=989"},"modified":"2019-10-20T18:15:34","modified_gmt":"2019-10-20T18:15:34","slug":"learning-arduino-for-beginners-ep38-ultrasonic-sensors-hc-sr04","status":"publish","type":"page","link":"https:\/\/thezhut.com\/?page_id=989","title":{"rendered":"Learning Arduino for beginners EP#38 ultrasonic sensors (HC-SR04)"},"content":{"rendered":"<h1>In this tutorial we will take a look at using ultrasonic sensors, we are going to make a proximity alarm using a hc-sr04 with our Arduino board and a potentiometer to make an adjustable range proximity alarm&#8230;..<\/h1>\n<h3>Click on video below for tutorial&#8230;<\/h3>\n<p><iframe loading=\"lazy\" title=\"Learning Arduino for beginners EP#38 ultrasonic sensors (HC-SR04)\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/AffID2T2Flw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h2>Parts List:<\/h2>\n<p>affiliate links<\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=ARDUINO Board&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=cf1489bb25158efe8ff0103f9af33e5b\" target=\"_blank\" rel=\"noopener noreferrer\">ARDUINO Board<\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=Ultrasonic sensor (hc-sr04)&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=aec580ec8f4e20606b40e3ebb48ad292\" target=\"_blank\" rel=\"noopener noreferrer\">Ultrasonic sensor (hc-sr04)<\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=arduino speaker&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=a4f7efbffa7f27e667a8c4ea2364ba85\" target=\"_blank\" rel=\"noopener noreferrer\">speaker<\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=tip120&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=5d79325934a723d6ccc71b48aca62805\" target=\"_blank\" rel=\"noopener noreferrer\">tip120<\/a><\/p>\n<p>potentiometer<\/p>\n<p>Bread board<\/p>\n<p>jumper wires<\/p>\n<h2>Schematic:<\/h2>\n<p><a href=\"https:\/\/thezhut.com\/wp-content\/uploads\/2018\/08\/hc-sro4_schem.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-990\" src=\"https:\/\/thezhut.com\/wp-content\/uploads\/2018\/08\/hc-sro4_schem.jpg?w=300\" alt=\"\" width=\"640\" height=\"444\" srcset=\"https:\/\/thezhut.com\/wp-content\/uploads\/2018\/08\/hc-sro4_schem.jpg 1272w, https:\/\/thezhut.com\/wp-content\/uploads\/2018\/08\/hc-sro4_schem-300x208.jpg 300w, https:\/\/thezhut.com\/wp-content\/uploads\/2018\/08\/hc-sro4_schem-768x533.jpg 768w, https:\/\/thezhut.com\/wp-content\/uploads\/2018\/08\/hc-sro4_schem-1024x710.jpg 1024w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>ARDUINO Code:<\/h2>\n<p>#define trigPin 9<br \/>\n#define echoPin 10<br \/>\nlong duration;<br \/>\nint distance;<br \/>\nint val;<\/p>\n<p>void setup()<br \/>\n{<br \/>\nSerial.begin(9600);<br \/>\npinMode(trigPin, OUTPUT);<br \/>\npinMode(echoPin, INPUT);<\/p>\n<p>}<\/p>\n<p>void loop()<br \/>\n{<br \/>\nval = (analogRead(A1));<\/p>\n<p>digitalWrite(trigPin, LOW);<br \/>\ndelayMicroseconds(2);<br \/>\ndigitalWrite(trigPin, HIGH);<br \/>\ndelayMicroseconds(10);<br \/>\ndigitalWrite(trigPin, LOW);<br \/>\nduration = pulseIn(echoPin, HIGH);<br \/>\n\/\/distance = duration * 0.034 \/ 2; \/\/ cm<br \/>\ndistance = duration * 0.0133 \/ 2; \/\/ in<br \/>\nSerial.println(distance);<br \/>\nval = map(val, 0, 1023, 6, 36);<br \/>\nSerial.println(val);<br \/>\nif (distance &lt;= val)<br \/>\n{<\/p>\n<p>tone(3, 500, 150);<br \/>\ndelay(150);<br \/>\ntone(3, 300, 150);<br \/>\ndelay(150);<\/p>\n<p>}<br \/>\n\/\/delay(500);<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we will take a look at using ultrasonic sensors, we are going to make a proximity alarm using a hc-sr04 with our Arduino board and a potentiometer to make an adjustable range proximity alarm&#8230;.. Click on video &hellip; <a href=\"https:\/\/thezhut.com\/?page_id=989\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":809,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-989","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=989"}],"version-history":[{"count":3,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/989\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/989\/revisions\/1478"}],"up":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/809"}],"wp:attachment":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}