{"id":165,"date":"2015-09-22T02:58:38","date_gmt":"2015-09-22T02:58:38","guid":{"rendered":"https:\/\/einhugur.com\/blog\/?page_id=165"},"modified":"2021-04-15T13:19:08","modified_gmt":"2021-04-15T13:19:08","slug":"hc-sr04-ultrasonic-sensor","status":"publish","type":"page","link":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/","title":{"rendered":"HC-SR04 and US-015 &#8211; Ultrasonic sensors"},"content":{"rendered":"<h2>In this example we are going to connect to HC-SR04 and or US-015\u00a0ultrasonic sensor.<\/h2>\n<h6><span style=\"color: #808080;\">(Updated 18. Oct 2015, adding US-015 sensor and sensor specs)<br \/>\n(Updated 18. Apr 2016, adding more info for accuracy for the HC-SR04 sensor)<br \/>\n<\/span><\/h6>\n<p>A ultrasonic sensor will send ultrasonic sound out which will echo on objects in-front of it and come back to the sensor. Using speed of sound then from the echo we can calculate the distance to the object.<\/p>\n<p>A sensor like this does not give good readings if object in-front of it is not at straight angle.<\/p>\n<p><strong>Disclaimer:<br \/>\n<\/strong>We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI.<\/p>\n<p><strong>The specs for the sensors:<\/strong><\/p>\n<p>Both sensors have pretty much same spec and in our test on short distance there was no notable difference. That does not mean there is no difference, their ability to detect small objects in the way at long distance may differ for example.<\/p>\n<p>Both sensors have exactly the same pin layout and functionality, so same wiring and same programming will apply for both of them.<\/p>\n<figure id=\"attachment_173\" aria-describedby=\"caption-attachment-173\" style=\"width: 326px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?ssl=1\"><img data-attachment-id=\"173\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/hc-sr04\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?fit=326%2C371&amp;ssl=1\" data-orig-size=\"326,371\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1442889122&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;500&quot;,&quot;shutter_speed&quot;:&quot;0.058823529411765&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"HC-SR04\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?fit=264%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?fit=326%2C371&amp;ssl=1\" loading=\"lazy\" class=\"size-full wp-image-173\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?resize=326%2C371&#038;ssl=1\" alt=\"HC-SR04 Ultra sonic sensor\" width=\"326\" height=\"371\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?w=326&amp;ssl=1 326w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?resize=264%2C300&amp;ssl=1 264w\" sizes=\"(max-width: 326px) 100vw, 326px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-173\" class=\"wp-caption-text\">HC-SR04 Ultra sonic sensor<\/figcaption><\/figure>\n<figure id=\"attachment_265\" aria-describedby=\"caption-attachment-265\" style=\"width: 317px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?ssl=1\"><img data-attachment-id=\"265\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/img_0151\/\" data-orig-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?fit=317%2C435&amp;ssl=1\" data-orig-size=\"317,435\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1445186919&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;500&quot;,&quot;shutter_speed&quot;:&quot;0.058823529411765&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;6&quot;}\" data-image-title=\"US-015\" data-image-description=\"\" data-medium-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?fit=219%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?fit=317%2C435&amp;ssl=1\" loading=\"lazy\" class=\"size-full wp-image-265\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?resize=317%2C435&#038;ssl=1\" alt=\"US-015 Ultra sonic sensor\" width=\"317\" height=\"435\" srcset=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?w=317&amp;ssl=1 317w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/IMG_0151-e1445187364805.jpg?resize=219%2C300&amp;ssl=1 219w\" sizes=\"(max-width: 317px) 100vw, 317px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-265\" class=\"wp-caption-text\">US-015 Ultra sonic sensor<\/figcaption><\/figure>\n<p style=\"padding-left: 30px;\"><strong>HC-SR04:<\/strong><\/p>\n<p style=\"padding-left: 60px;\">Range: 2 &#8211; 400 cm<br \/>\nResolution: 3 mm<br \/>\nMeasuring angle: 15 degrees<br \/>\nWorking voltage: DC 5 V<br \/>\nPins: 4<\/p>\n<p style=\"padding-left: 30px;\"><strong>US-015:<\/strong><\/p>\n<p style=\"padding-left: 60px;\">Range: 2 &#8211; 400 cm<br \/>\nResolution: 3 mm<br \/>\nMeasuring angle: 15 degrees<br \/>\nWorking voltage: DC 5 V<br \/>\nPins: 4<\/p>\n<p><strong>Pieces we use in this example:<\/strong><\/p>\n<ul>\n<li>Raspberry PI 2<\/li>\n<li>Cobbler and Cobbler cable<\/li>\n<li>Bread board<\/li>\n<li>1 K-ohm resistor<\/li>\n<li>470 ohm resistor and 120 ohm resistor <span style=\"color: #808080;\"><span style=\"color: #808080;\"><span style=\"color: #808080;\">(or some combination\u00a0that can combined provide a little above 515 ohm)<\/span><\/span><\/span><span style=\"color: #808080;\">(Resistors are cheap, if your going to experiment a lot then do your self a favour and just buy a big set, I bought full set with 2420 of them for only $16) &#8211; I don&#8217;t got them yet so in this example I just used whatever I had, I probably would have used 470 and 100 ohm instead of 470 and 120, but in this example it makes no difference.)\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li><span style=\"color: #999999;\"><span style=\"color: #000000;\">\u00a0HC-SR04 or US-015<\/span>\u00a0(They cost $0.70 Aliexpress per piece, and usually lower if you buy many, I found them on Amazon for as low as $1.99 per piece)<\/span><\/li>\n<li><a href=\"https:\/\/github.com\/xojo\/GPIO\">Wiring PI Xojo module from\u00a0Paul Lefebvre<\/a><\/li>\n<\/ul>\n<hr \/>\n<p><strong>If you are not familiar with breadboards, how to place a cobbler on the breadboard or need to know how to read the color codings on the resistors then click here bellow<\/strong><\/p>\n<p><a class=\"mb-button mb-style-glass mb-size-small mb-corners-default mb-text-style-default \" style=\"background-color: #2b7dc4;\" href=\"https:\/\/einhugur.com\/blog\/index.php\/understanding-your-breadboard-and-placement-of-raspberry-cobbler\/\">\u00a0<i class=\"fa fa-code-fork\"><\/i>\u00a0 Using breadboard<\/a>\u00a0 <a class=\"mb-button mb-style-glass mb-size-small mb-corners-default mb-text-style-default \" style=\"background-color: #2b7dc4;\" href=\"https:\/\/einhugur.com\/blog\/index.php\/basic-electronics\/resistor-color-codings\/\">\u00a0<i class=\"fa fa-bars\"><\/i>\u00a0 Resistor color codings<\/a><\/p>\n<hr \/>\n<p><strong>First thing is understanding the wiring on the sensor:<\/strong><\/p>\n<figure id=\"attachment_173\" aria-describedby=\"caption-attachment-173\" style=\"width: 326px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?ssl=1\"><img data-attachment-id=\"173\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/hc-sr04\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?fit=326%2C371&amp;ssl=1\" data-orig-size=\"326,371\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1442889122&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;500&quot;,&quot;shutter_speed&quot;:&quot;0.058823529411765&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"HC-SR04\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?fit=264%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?fit=326%2C371&amp;ssl=1\" loading=\"lazy\" class=\"size-full wp-image-173\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?resize=326%2C371&#038;ssl=1\" alt=\"HC-SR04 Ultra sonic sensor\" width=\"326\" height=\"371\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?w=326&amp;ssl=1 326w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR04.jpg?resize=264%2C300&amp;ssl=1 264w\" sizes=\"(max-width: 326px) 100vw, 326px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-173\" class=\"wp-caption-text\">HC-SR04 Ultra sonic sensor<\/figcaption><\/figure>\n<p>This sensor comes with good markings so its very easy to wire it. We will be putting it on breadboard so I am not showing it with wires at this time like I usually do.<\/p>\n<ul>\n<li><span style=\"color: #808080;\">(5V)<\/span>\u00a0Red is the 5V input which goes in VCC on the sensor &#8211;<span style=\"color: #808080;\">\u00a0I always use red for the power input.<\/span><\/li>\n<li><span style=\"color: #808080;\">(GPIOX1)<\/span>\u00a0Yellow is will go in a pin called Trig on the sensor, its used to trigger the sensor to let it start measuring.<\/li>\n<li><span style=\"color: #808080;\">Echo\u00a0<span style=\"color: #000000;\">this one will be connected to resistors, see bellow<\/span><\/span>. Note the echo will come back as 5V. (<span style=\"color: #ff0000;\">Be warned do not connect this pin directly to the Raspberry, it will damage the GPIO on the Raspberry)<\/span><\/li>\n<li><span style=\"color: #808080;\">(GND)<\/span>\u00a0Blue is the ground which goes in GND on the sensor,\u00a0<span style=\"color: #808080;\">I always use blue for ground.<\/span><\/li>\n<\/ul>\n<p><strong>Understanding what we are going to do:<\/strong><\/p>\n<p>Because the Echo pin is putting out\u00a0to high voltage for\u00a0the GPIO pin then we will be reducing its voltage by using resistor.<\/p>\n<p>But first we will make a decision to protect the pin from programming mistakes, that is cases like\u00a0if you by accident put output on the input pin. Therefore we choose to put 1K resistor between the pin and the ground. To lower\u00a0the voltage from the sensor Echo before it goes into the GPIO\u00a0we have to calculate:<\/p>\n<p><em>We know the following things:<\/em><\/p>\n<ul>\n<li><em>Voltage that we have: 5 V<\/em><\/li>\n<li><em>Desired voltage: 3,3 V<\/em><\/li>\n<li>Resistor that we choose to go to the ground: 1K = 1000 Ohm<br \/>\n(<a href=\"https:\/\/einhugur.com\/blog\/index.php\/basic-electronics\/voltage-dividers\/\">See also our article about Voltage dividers<\/a>)<\/li>\n<\/ul>\n<p>So we can calculate:<\/p>\n<p style=\"padding-left: 30px;\">3,3 = 5 * (1000 \/ (R + 1000))<br \/>\n0.66 = \u00a0(1000 \/ (R + 1000))<br \/>\n0.66R +\u00a0660 = 1000<br \/>\n0.66R = 340<br \/>\n<span style=\"text-decoration: underline;\"><strong>R = 515 Ohm<\/strong><\/span><\/p>\n<p style=\"text-align: left;\">This will give us the following circuit:<\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?ssl=1\"><img data-attachment-id=\"170\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/screen-shot-2015-09-22-at-00-01-38\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?fit=505%2C382&amp;ssl=1\" data-orig-size=\"505,382\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Ultrasonic wiring\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?fit=300%2C227&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?fit=474%2C359&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-170\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?resize=474%2C359&#038;ssl=1\" alt=\"Ultrasonic wiring\" width=\"474\" height=\"359\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?w=505&amp;ssl=1 505w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Screen-Shot-2015-09-22-at-00.01.38.png?resize=300%2C227&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p style=\"text-align: left;\">(Use this circuit diagram to help you when wiring the breadboard further down in this article)<\/p>\n<p><strong>Connecting cobbler and breadboard:<\/strong><\/p>\n<p>Make sure your Raspberry PI is not powered on when you do the connections. In this example then we have put the cobbler onto a breadboard.<\/p>\n<p>For the 515 Ohm I serial connect a 470 Ohm and 120 Ohm Resistor, getting total of 590 Ohm which is good value to generate HIGH but yet not damage the pin. Its important here when finding resistor to fit into the 515 calculated value to not go under 515, while going higher is always safe it will just drop the voltage more. If dropping it to much then you will not get HIGH pulse that you need.<\/p>\n<p><em>470 Ohm = \u00a0Yellow &#8211; Violet &#8211; Yellow <span style=\"color: #808080;\">(Read towards the golden stripe)<\/span><\/em><br \/>\n<em>120 Ohm = \u00a0Brown &#8211; Red &#8211; Brown\u00a0\u00a0<span style=\"color: #808080;\">(Read towards the golden stripe)<\/span><\/em><\/p>\n<p>The 1K resistor we also use would be:<br \/>\n<em>1\u00a0KOhm = \u00a0Brown &#8211; Black &#8211; Red\u00a0<span style=\"color: #808080;\">(Read towards the golden stripe)<\/span><\/em><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?ssl=1\"><img data-attachment-id=\"82\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/gpio-header\/\" data-orig-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?fit=440%2C870&amp;ssl=1\" data-orig-size=\"440,870\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"GPIO Header\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?fit=152%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?fit=440%2C870&amp;ssl=1\" loading=\"lazy\" class=\"size-medium wp-image-82 alignnone\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header-152x300.png?resize=152%2C300&#038;ssl=1\" alt=\"GPIO Header\" width=\"152\" height=\"300\" srcset=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?resize=152%2C300&amp;ssl=1 152w, https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?w=440&amp;ssl=1 440w\" sizes=\"(max-width: 152px) 100vw, 152px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p style=\"text-align: left;\"><strong>In my example then I have it connected as follows:<\/strong><\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?ssl=1\"><img data-attachment-id=\"175\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/ultrasoniconbreadboard\/\" data-orig-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?fit=583%2C332&amp;ssl=1\" data-orig-size=\"583,332\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1442889073&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;640&quot;,&quot;shutter_speed&quot;:&quot;0.058823529411765&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"ultrasonic sensor on bread board\" data-image-description=\"\" data-medium-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?fit=300%2C171&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?fit=474%2C270&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-175\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?resize=474%2C270&#038;ssl=1\" alt=\"ultrasonic sensor on bread board\" width=\"474\" height=\"270\" srcset=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?w=583&amp;ssl=1 583w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/UltrasonicOnBreadBoard.jpg?resize=300%2C171&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p style=\"text-align: left;\"><span style=\"color: #808080;\"><i>Use common sense when connecting those look at the circuit further up in this guide, and if your breadboard is different in any way then the breadboard coords in this\u00a0guide will not be accurate or you, so\u00a0don&#8217;t rely on them.<\/i><\/span><\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?ssl=1\"><img data-attachment-id=\"202\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/ultra-sonic_bb\/\" data-orig-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?fit=1131%2C1011&amp;ssl=1\" data-orig-size=\"1131,1011\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Ultra Sonic_bb\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?fit=300%2C268&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?fit=474%2C424&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-202\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?resize=474%2C424&#038;ssl=1\" alt=\"Ultra Sonic_bb\" width=\"474\" height=\"424\" srcset=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?w=1131&amp;ssl=1 1131w, https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?resize=300%2C268&amp;ssl=1 300w, https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?resize=1024%2C915&amp;ssl=1 1024w, https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/Ultra-Sonic_bb.png?w=948&amp;ssl=1 948w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<ul>\n<li style=\"text-align: left;\">Cobbler placed on breadboard, placed\u00a0so that Pin 1 goes to D1, (or if you have huge breadboard then make sure Pin 1 goes to row 1, and that pin 2 is on the other side of the middle canal)<\/li>\n<li style=\"text-align: left;\">I placed the sensor at far bottom, making sure it turns so that each pin on the sensor has its own row or own lane in the breadboard.<\/li>\n<li style=\"text-align: left;\">Red wire from 5V Cobbler Pin 2 <span style=\"color: #808080;\">(J-1 on my breadboard)<\/span> to the sensors VCC (I-30 on my breadboard).<\/li>\n<li style=\"text-align: left;\">Yellow wire from GPIO4 which is cobbler pin 7 <span style=\"color: #808080;\">(B-4 on my breadboard)<\/span> to the sensors Trig pin <span style=\"color: #808080;\">(F-29 on my breadboard).<\/span><\/li>\n<li style=\"text-align: left;\">Blue wire from GND Cobbler pin 39 (<span style=\"color: #808080;\">B-20\u00a0on my breadboard<\/span>) to the sensors GND (<span style=\"color: #808080;\"><b>I<\/b>-27 on my breadboard<\/span>)<\/li>\n<li style=\"text-align: left;\">470 ohm resistor from sensors Echo pin (<span style=\"color: #808080;\">F-28\u00a0on my breadboard<\/span>) to the other side of the canal <span style=\"color: #808080;\">(D-28\u00a0on my breadboard)\u00a0<\/span><\/li>\n<li style=\"text-align: left;\">120\u00a0ohm resistor \u00a0from the 470 ohm one<span style=\"color: #808080;\">\u00a0(C-28\u00a0on my breadboard)<\/span> to empty lane\u00a0<span style=\"color: #808080;\">(C-25 on my breadboard)<\/span><\/li>\n<li style=\"text-align: left;\">Green wire from GPIO5 which is cobbler pin 29 <span style=\"color: #808080;\">(B-15 on my breadboard)\u00a0<span style=\"color: #000000;\">to the 120 ohm resistor\u00a0<span style=\"color: #808080;\">(B-25\u00a0on my breadboard)\u00a0<\/span><\/span><\/span><\/li>\n<li style=\"text-align: left;\">1K resistor from the 120 ohm resistor\u00a0<span style=\"color: #808080;\">(A-25\u00a0on my breadboard) \u00a0<span style=\"color: #000000;\">to the ground,\u00a0\u00a0<span style=\"color: #808080;\">(A-20 on my breadboard)<\/span><\/span><\/span><\/li>\n<\/ul>\n<p><strong>The Xojo code:<\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important\u00a0!!\u00a0Paul Lefebvre&#8217;s GPIO library for Xojo has a tiny bug in it, check if yours has it and fix it if needed before using this code. The bug is that the constant HIGH\u00a0is defined as 0, but should be defined as 1.\u00a0<\/strong><\/span><\/p>\n<p><span style=\"color: #ff0000;\"><strong>If it is not fixed then this sensor code will just put your application in endless loop.<\/strong><\/span><\/p>\n<p>The open event of the Window:<\/p>\n<pre lang=\"xojo\">Sub Open()\nGPIO.SetupGPIO\nEnd Sub<\/pre>\n<p>Our function to talk to the sensor:<\/p>\n<pre lang=\"xojo\">Function ReadSensorHCSR04(triggerPin as Integer,echoPin as Integer) As Double\nDim startTime as UInteger\nDim endTime as UInteger\nconst kSpeedOfSound = 340.29\n\nGPIO.PinMode(triggerPin,GPIO.OUTPUT)\n\nGPIO.DigitalWrite(triggerPin,GPIO.LOW)\nGPIO.PinMode(echoPin,GPIO.INPUT)\nGPIO.Delay(100)\n\nGPIO.DigitalWrite(triggerPin,GPIO.HIGH)\nGPIO.DelayMicroseconds(10)\nGPIO.DigitalWrite(triggerPin,GPIO.LOW)\n\nwhile(GPIO.DigitalRead(echoPin) = GPIO.LOW)\n\/\/ We just wait for it to send the signal back so nothingto do here\nwend\n\n\/\/ We want to measure the time lenght of the HIGH pules in the echo.\nstartTime = GPIO.Micros\n\nwhile(GPIO.DigitalRead(echoPin) = GPIO.HIGH)\n\/\/ We just wait for it to send the signal back so nothingto do here\nwend\n\nendTime = GPIO.Micros\n\n\/\/speed = distance \/ time as we all should know !\nreturn (endTime - startTime) * 0.000001* (kSpeedOfSound \/ 2) * 100 \/\/ * 100 to get it in cm\nEnd Function<\/pre>\n<p>Our button to test the function:<\/p>\n<pre lang=\"xojo\">Sub Action()\nMsgBox Format(ReadSensorHCSR04(4,5),\"#.0\") + \" cm\"\nEnd Sub<\/pre>\n<hr \/>\n<p><strong>18. April 2016<\/strong>\u00a0&#8211; Ulrich\u00a0Bogon pointed out to me that the specs for the HC-SR04 tell that if not echo is detected then the sensor will stay at high for 200 m\/s to indicate the failure.<\/p>\n<p>To take this into account then you might want to change the\u00a0return code to:<\/p>\n<pre lang=\"xojo\">Dim timeDelta as UInteger = (endTime - startTime)\n\nreturn if(timeDelta &gt;= 20000, -1, (timeDelta) * 0.000001* (kSpeedOfSound \/ 2) * 100) \/\/ * 100 to get it in cm\n\n\/\/ Where return value of -1 would then hint that nothing was detected.<\/pre>\n<p>further more then he also pointed out that you can improve accuracy by measuring temperature to choose the best speed of sound constant.<\/p>\n<p>A good method to set that would be something like:<\/p>\n<pre lang=\"xojo\">temperature = 20.0 \/\/ In C\nspeedOfSound = 331.5 + (0,6 x 20)<\/pre>\n<p>When this is written then we do not have any information if same or something similar applies to the US-015 sensor.<\/p>\n<hr \/>\n<p>Thats it for now !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example we are going to connect to HC-SR04 and or US-015\u00a0ultrasonic sensor. (Updated 18. Oct 2015, adding US-015 sensor and sensor specs) (Updated 18. Apr 2016, adding more info for accuracy for the HC-SR04 sensor) A ultrasonic sensor will send ultrasonic sound out which will echo on objects in-front of it and come &hellip; <a href=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr04-ultrasonic-sensor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HC-SR04 and US-015 &#8211; Ultrasonic sensors<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":94,"menu_order":70,"comment_status":"open","ping_status":"closed","template":"","meta":{"spay_email":""},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Pd75nk-2F","jetpack-related-posts":[{"id":94,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/","url_meta":{"origin":165,"position":0},"title":"Xojo GPIO","date":"September 19, 2015","format":false,"excerpt":"Guides for GPIO in Xojo \u00a0Infrared motion detection (Updated 1. May 2016) Shows\u00a02 ways of coding: Using Interrupts or simple polling. Sensors: \u00a0HC-SR501,\u00a0ST-00082 Mini,\u00a0Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module Uses:\u00a0Basic GPIO Relay board In this example we \u00a0wire up a relay board and let your Xojo\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":113,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/flame-sensor\/","url_meta":{"origin":165,"position":1},"title":"Flame sensor","date":"September 19, 2015","format":false,"excerpt":"In this example we are going to connect to flame\u00a0sensor. Programming for this sensor works on exactly the same principle as the\u00a0HC-SR501 infrared sensor and the F85 vibration sensor except the signal from this sensor comes inverted. As in it will send 1 when everything is all right and zero\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/FlameSensor-e1442681877884-225x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":128,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/dht11-and-dht22-humidity-temperature-sensors-3-pin-version\/","url_meta":{"origin":165,"position":2},"title":"DHT11 and DHT22 humidity & temperature sensors (3 pin version)","date":"September 20, 2015","format":false,"excerpt":"In this example we are going to connect\u00a0to DHT11 and or DHT22 humidity & temperature sensor. This sensor reads both the humidity and temperature. (This guide was updated on 24. Oct 2015, greatly improving the DHT11 device driver code and added DHT22 device driver code and wiring pictures for the\u2026","rel":"","context":"Similar post","img":{"alt_text":"DHT22 sensor","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/DHT22.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":79,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/","url_meta":{"origin":165,"position":3},"title":"Infrared motion detection","date":"September 19, 2015","format":false,"excerpt":"In this example we are going to connect to infrared motion detection sensor (various of sensors work, see bellow the ones we have tested). (Updated 24. Oct 2015, adding code with interrupts and changed setup to use a breadboard) (Updated 8. Nov\u00a02015, adding ST-00082 Mini sensor) (Updated 1. May 2016,\u2026","rel":"","context":"Similar post","img":{"alt_text":"HC-SR501 Sensor_bb","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":108,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/f85-vibration-sensor\/","url_meta":{"origin":165,"position":4},"title":"F85 Vibration sensor","date":"September 19, 2015","format":false,"excerpt":"In this example we are going to connect to\u00a0vibration sensor. This example is super easy after doing the infrared motion detection with the HC-SR501 in a previous example, since basically same code will work. I found this sensor to be excellent, it gives readings when I expect it to and\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/Sensor-connection-e1442664059628-225x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":345,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/using-logic-gates\/","url_meta":{"origin":165,"position":5},"title":"Using logic gates","date":"November 8, 2015","format":false,"excerpt":"In this guide we are going to learn how to use logic gates. \u00a0Even if you can usually do things by just taking more GPIO pins and do the logic in a code then there will be times you may want to do things in hardware. For example you might\u2026","rel":"","context":"Similar post","img":{"alt_text":"LogicCircuits","src":"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/165"}],"collection":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":2,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/165\/revisions"}],"predecessor-version":[{"id":974,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/165\/revisions\/974"}],"up":[{"embeddable":true,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/94"}],"wp:attachment":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}