{"id":207,"date":"2014-12-02T04:53:24","date_gmt":"2014-12-02T04:53:24","guid":{"rendered":"http:\/\/guceret.com\/wp\/?p=207"},"modified":"2014-12-02T09:31:27","modified_gmt":"2014-12-02T09:31:27","slug":"arduino-with-hc-sr04-ultrasonic-sensor","status":"publish","type":"post","link":"https:\/\/guceret.com\/wp\/?p=207","title":{"rendered":"Arduino with HC-SR04 ultrasonic sensor"},"content":{"rendered":"<p>I will be using the HC-SR04 ultrasonic sensor to measure sensor. I am using Arduino MEGA2560.  <\/p>\n<p><!--more--><\/p>\n<p>The speed of sound is 340.29 m\/s then convert this to 29.38 us \/ cm.  <\/p>\n<p>340.29 m\/s then  34029\/1000000 cm\/us then inverting  29.38 us\/cm. <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\r\n\/*\r\nHC-SR04 Ping distance sensor]\r\nVCC to arduino 5v GND to arduino GND\r\nEcho to Arduino pin 13 Trig to Arduino pin 12\r\nSome code and wiring inspired by http:\/\/en.wikiversity.org\/wiki\/User:Dstaub\/robotcar\r\n*\/\r\n\r\n#define t 4\r\n#define e 5\r\n#define led_2 52\r\n\r\n\r\nvoid setup() {\r\n  Serial.begin (9600);\r\n  pinMode(t, OUTPUT);\r\n  pinMode(e, INPUT);\r\n  pinMode ( led_2 ,  OUTPUT);\r\n  digitalWrite(led_2, LOW);\r\n\r\n}\r\n\r\n\r\nvoid loop() {\r\n  long duration, distance;\r\n  \/\/ The speed of sound is 340.29 m\/s or 29.38 microseconds per centimeter.\r\n  \/\/ The ping travels out and back, so to find the distance of the\r\n  \/\/ object we take half of the distance travelled\r\n  \r\n  \/\/ Trigger Pulse 10us\r\n  digitalWrite(t, LOW);  \r\n  delayMicroseconds(2); \r\n  digitalWrite(t, HIGH);\r\n  delayMicroseconds(10); \r\n  digitalWrite(t, LOW);\r\n  \r\n  \/\/ Reads a pulse (either HIGH or LOW) on a pin. if value is HIGH, pulseIn() waits for the pin to go HIGH, \r\n  \/\/ starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds.\r\n  \/\/ it returns an unsigned long datatype\r\n  duration = pulseIn(e, HIGH);\r\n  distance = (duration\/2) \/ 29.38;\r\n\r\n  if (distance &gt;= 200 || distance &lt;= 0){\r\n    Serial.println(&quot;Out of range&quot;);\r\n  }\r\n  else {\r\n    Serial.print(distance);\r\n    Serial.println(&quot; cm&quot;);\r\n    if ( distance &lt; 25 ){\r\n      digitalWrite( led_2 , HIGH);\r\n    } else {\r\n      digitalWrite ( led_2 , LOW );\r\n    }\r\n  }\r\n  \r\n  \r\n  \r\n  \r\n  delay(500);\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I will be using the HC-SR04 ultrasonic sensor to measure sensor. I am using Arduino MEGA2560.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-arduino"],"_links":{"self":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=207"}],"version-history":[{"count":8,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}