{"id":2630,"date":"2014-11-23T19:16:01","date_gmt":"2014-11-23T19:16:01","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2630"},"modified":"2014-11-24T12:22:27","modified_gmt":"2014-11-24T12:22:27","slug":"using-the-ultrasound-sensor-hc-sr04","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-the-ultrasound-sensor-hc-sr04\/","title":{"rendered":"Using the ultrasound sensor HC SR04"},"content":{"rendered":"<p>My first foray into Ardunio programming is to implement a robot based upon the Arduino Motor shield and some sensors &#8211; starting with the <a href=\"http:\/\/www.micropik.com\/PDF\/HCSR04.pdf\" title=\"HC SR04\" target=\"_blank\">HC SR04<\/a>. <\/p>\n<p>In this case I want a sensor that will tell me the distance from objects, so hopefully the robot will not go around smashing into walls etc. This is where the HC SR04 comes in.<\/p>\n<p>The HC SR04 has four pins. We simply connect the VCC pin to the 5V pin on the Arduino, the GND to the Arduino&#8217;s ground pin and then we&#8217;re left with two other pins. <\/p>\n<p>The trigger and echo pins are digital pins, the idea being we send a 10 microsecond pulse to the trigger pin and the module will send back (via the echo pin) a value whose duration we can use to calculate the distance &#8220;pinged&#8221;. But we don&#8217;t just read the digital pin, we instead use the <em>pulseIn<\/em> function.<\/p>\n<p><em>I&#8217;m indebted to others who have kindly supplied information on how to program this sensor, I&#8217;ve added references at the bottom of this post. I&#8217;m writing this post simply to detail the steps and code I successfully used to get this working. The referenced posts are definitely the best posts for full information on this sensor, hardware connections and code.<\/em><\/p>\n<p>Here&#8217;s a simple C++ class (header and source) for handling this sensor<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#ifndef _ULTRASOUND_h\r\n#define _ULTRASOUND_h\r\n\r\n#if defined(ARDUINO) &amp;&amp; ARDUINO &gt;= 100\r\n   #include &quot;Arduino.h&quot;\r\n#else\r\n   #include &quot;WProgram.h&quot;\r\n#endif\r\n\r\nclass Ultrasound\r\n{\r\nprivate:\r\n\tint triggerPin;\r\n\tint echoPin;\r\n\r\npublic:\r\n\r\n\tvoid init(int tiggerPin, int echoPin);\r\n\tlong ping();\r\n};\r\n\r\n#endif\r\n<\/pre>\n<p>and the corresponding source<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;Ultrasound.h&quot;\r\n\r\nvoid Ultrasound::init(int triggerPin, int echoPin)\r\n{\r\n   this-&gt;triggerPin = triggerPin;\r\n   this-&gt;echoPin = echoPin;\r\n\r\n   pinMode(triggerPin, OUTPUT);\r\n   pinMode(echoPin, INPUT);\r\n}\r\n\r\nlong Ultrasound::ping()\r\n{\r\n   digitalWrite(triggerPin, LOW);\r\n   delayMicroseconds(2);\r\n   digitalWrite(triggerPin, HIGH);\r\n   delayMicroseconds(10);\r\n   digitalWrite(triggerPin, LOW);\r\n\r\n   long duration = pulseIn(echoPin, HIGH);\r\n   long distance = (duration \/ 2) \/ 29.1;\r\n   \/\/ &gt;= 200 or &lt;= 0 are error values, or out of range\r\n   return distance &gt;= 200 || distance &lt;= 0 ? -1 : distance;\r\n}\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"http:\/\/www.micropik.com\/PDF\/HCSR04.pdf\" title=\"Ultrasonic Ranging Module HC - SR04\" target=\"_blank\">Ultrasonic Ranging Module HC &#8211; SR04<\/a><br \/>\n<a href=\"http:\/\/www.instructables.com\/id\/Simple-Arduino-and-HC-SR04-Example\/\" title=\"Simple Arduino and HC-SR04 Example\" target=\"_blank\">Simple Arduino and HC-SR04 Example<\/a><br \/>\n<a href=\"http:\/\/randomnerdtutorials.com\/complete-guide-for-ultrasonic-sensor-hc-sr04\/\" title=\"Complete Guide for Ultrasonic Sensor HC-SR04\" target=\"_blank\">Complete Guide for Ultrasonic Sensor HC-SR04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My first foray into Ardunio programming is to implement a robot based upon the Arduino Motor shield and some sensors &#8211; starting with the HC SR04. In this case I want a sensor that will tell me the distance from objects, so hopefully the robot will not go around smashing into walls etc. This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[88,89],"tags":[],"class_list":["post-2630","post","type-post","status-publish","format-standard","hentry","category-arduino","category-electronics"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2630"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2630\/revisions"}],"predecessor-version":[{"id":2643,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2630\/revisions\/2643"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}