{"id":79,"date":"2015-09-19T12:29:18","date_gmt":"2015-09-19T12:29:18","guid":{"rendered":"https:\/\/einhugur.com\/blog\/?page_id=79"},"modified":"2016-05-19T17:34:57","modified_gmt":"2016-05-19T17:34:57","slug":"hc-sr501-sensor","status":"publish","type":"page","link":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/","title":{"rendered":"Infrared motion detection"},"content":{"rendered":"<p>In this example we are going to connect to infrared motion detection sensor (various of sensors work, see bellow the ones we have tested).<\/p>\n<div class=\"table-responsive\"><table  style=\"width:500px; \"  class=\"easy-table easy-table-default \" >\n<caption>Infrared motion detector sensors that we have tested<\/caption>\n<thead>\r\n<tr><th  style=\"width:50%;text-align:left\" >Name<\/th>\n<th  style=\"width:50%;text-align:left\" >Tested<\/th>\n<\/tr>\n<\/thead>\n<tbody>\r\n<tr><td  style=\"text-align:left\" >HC-SR501<\/td>\n<td  style=\"text-align:left\" > Tested by us<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >ST-00082 Mini<\/td>\n<td  style=\"text-align:left\" > Tested by user<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module<\/td>\n<td  style=\"text-align:left\" > Tested by us<\/td>\n<\/tr>\n<\/tbody><\/table><\/div>\n<p><span style=\"color: #808080;\">(Updated 24. Oct 2015, adding code with interrupts and changed setup to use a breadboard)<br \/>\n(Updated 8. Nov\u00a02015, adding ST-00082 Mini sensor)<br \/>\n(Updated 1. May 2016, adding\u00a0Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector module) <\/span><\/p>\n<p><span style=\"color: #000000;\">I found this sensor to be excellent, it gives readings when I expect it to and not readings when I don&#8217;t expect it to.<\/span><\/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>Pieces we use are:<\/strong><\/p>\n<ul>\n<li>Raspberry PI 2<\/li>\n<li>Cobbler and Cobbler cable<\/li>\n<li>HC-SR501 sensor<span style=\"color: #999999;\"> (They go as low as $0.78 on Aliexpress where I got this one from probably even lower if you buy more than one unit). Or one of the other sensors that we have listed above.<\/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 or not familiar with how to place a cobbler on the breadboard 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><\/p>\n<hr \/>\n<p><strong>First thing is connecting the sensor:<\/strong><\/p>\n<p>Make sure that your Raspberry PI is not turned on when your wiring it up. Also if your not <a href=\"https:\/\/einhugur.com\/blog\/index.php\/understanding-your-breadboard-and-placement-of-raspberry-cobbler\/\">familiar with breadboards and placing the cobbler then click here<\/a>.<\/p>\n<figure id=\"attachment_81\" aria-describedby=\"caption-attachment-81\" style=\"width: 230px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection.jpg?ssl=1\"><img data-attachment-id=\"81\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/hc-sr501-connection\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection.jpg?fit=387%2C504&amp;ssl=1\" data-orig-size=\"387,504\" 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;1442662568&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;64&quot;,&quot;shutter_speed&quot;:&quot;0.033333333333333&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"HC-SR501 wiring\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection.jpg?fit=230%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection.jpg?fit=387%2C504&amp;ssl=1\" loading=\"lazy\" class=\"size-medium wp-image-81\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection-230x300.jpg?resize=230%2C300&#038;ssl=1\" alt=\"Wiring of the HC-SR501\" width=\"230\" height=\"300\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection.jpg?resize=230%2C300&amp;ssl=1 230w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-connection.jpg?w=387&amp;ssl=1 387w\" sizes=\"(max-width: 230px) 100vw, 230px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-81\" class=\"wp-caption-text\">Wiring of the HC-SR501<\/figcaption><\/figure>\n<blockquote><p>If you got the ST-00082 then the spec is very similar. It is easy replacement, on the\u00a0ST-00082 yellow will go to OUT, red to VDD, and blue to GND.<\/p>\n<figure id=\"attachment_369\" aria-describedby=\"caption-attachment-369\" style=\"width: 168px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/ST-00082.png?ssl=1\"><img data-attachment-id=\"369\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/st-00082\/\" data-orig-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/ST-00082.png?fit=168%2C236&amp;ssl=1\" data-orig-size=\"168,236\" 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=\"ST-00082\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/ST-00082.png?fit=168%2C236&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/ST-00082.png?fit=168%2C236&amp;ssl=1\" loading=\"lazy\" class=\"size-full wp-image-369\" src=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/ST-00082.png?resize=168%2C236&#038;ssl=1\" alt=\"ST-00082\" width=\"168\" height=\"236\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-369\" class=\"wp-caption-text\">ST-00082<\/figcaption><\/figure><\/blockquote>\n<p>If you got the one shown on the picture bellow then this one is the smallest one, we found no official name on it but its sold as &#8220;Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module&#8221;. <span style=\"color: #ff0000;\"><strong>WARNING do not attempt to connect it directly to the GPIO pin like the other sensors<\/strong><\/span>. Specs on its output pin on the net were fluctuating from 3.3V to 4 V so I measured just in case and found it to be 4.4 V ! <a href=\"https:\/\/einhugur.com\/blog\/index.php\/mini-ir-pyroelectric-infrared-pir-motion-human-sensor-detector-module\/\">Click here for guide on how to secure the GPIO pin for 4.4 V to be able to use this sensor.<\/a><\/p>\n<figure id=\"attachment_504\" aria-describedby=\"caption-attachment-504\" style=\"width: 150px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?ssl=1\"><img data-attachment-id=\"504\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/minimotionsensorsoldered\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?fit=150%2C314&amp;ssl=1\" data-orig-size=\"150,314\" 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;1462123799&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;64&quot;,&quot;shutter_speed&quot;:&quot;0.03030303030303&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"MiniMotionSensorSoldered\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?fit=143%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?fit=150%2C314&amp;ssl=1\" loading=\"lazy\" class=\"size-full wp-image-504\" src=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?resize=150%2C314&#038;ssl=1\" alt=\"This one we found no name for except just &quot;Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module&quot;\" width=\"150\" height=\"314\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?w=150&amp;ssl=1 150w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/MiniMotionSensorSoldered.jpg?resize=143%2C300&amp;ssl=1 143w\" sizes=\"(max-width: 150px) 100vw, 150px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-504\" class=\"wp-caption-text\">This one we found no name for except just &#8220;Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module&#8221;<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Note that when shown on the breadboard the sensor is shown from the other side, so check the picture above to make sure the wires go in right place.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?ssl=1\"><img data-attachment-id=\"336\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/hc-sr501-sensor_bb\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?fit=800%2C724&amp;ssl=1\" data-orig-size=\"800,724\" 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=\"HC-SR501 Sensor_bb\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?fit=300%2C272&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?fit=474%2C429&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-336\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?resize=474%2C429&#038;ssl=1\" alt=\"HC-SR501 Sensor_bb\" width=\"474\" height=\"429\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?w=800&amp;ssl=1 800w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/HC-SR501-Sensor_bb.png?resize=300%2C272&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<ul>\n<li><span style=\"color: #808080;\">(5V)<\/span> Red is the 5V input &#8211; I always use red for the power input.<\/li>\n<li><span style=\"color: #808080;\">(GPIOX)<\/span> Yellow is the Data pin.<\/li>\n<li><span style=\"color: #808080;\">(GND)<\/span> Blue is the ground.<\/li>\n<\/ul>\n<p><span style=\"color: #808080;\">(Inspect your sensor carefully for markings, I have seen sensor of this type that had the pins in different order, mine had no markings on it so I just had to know the pin order)<\/span><\/p>\n<p>&nbsp;<\/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<ul>\n<li style=\"text-align: left;\">Red to pin 2 (5V)<\/li>\n<li style=\"text-align: left;\">Blue to pin 39\u00a0(GND)<\/li>\n<li style=\"text-align: left;\">Yellow to GPIO 5 which is pin 29.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>The Xojo code:<\/strong><\/p>\n<p>Using interrupts or not interrupts?<\/p>\n<p><em>If just polling the hardware then you need to poll it in really tight loop which will occupy your Raspberry PI a lot, also even if you poll in really tight loop then you still can miss events. So usually you will want to use interrupts if you can.<\/em><\/p>\n<p>But but wait, the interrupt code polls also!! Why is polling any better than polling?<\/p>\n<p><em>Polling the interrupt counter you can do at low intervals making your Raspberry PI not \u00a0hog the CPU, and still not miss any events as the interrupt handler has logged them down.<\/em><\/p>\n<p><strong>The Xojo code with interrupts:<\/strong><\/p>\n<p>When using interrupts then we must be really careful to not call anything from within the interrupt since Xojo does not support that. So the idea is we have a safe function that only logs the interrupt then we poll the log. That way event is never lost and timer or whatever method you use to poll does not need to be very intensive.<\/p>\n<p>We just have a Window here with one Listbox on it and one Timer. You can set the Timer to 1000 or maybe 500 depending on what responsiveness your looking for.<\/p>\n<p>The open event of the Window:<\/p>\n<pre lang=\"xojo\">Sub Open()\r\n GPIO.PinMode(5,GPIO.INPUT)\r\n GPIO.PullUpDnControl(5,GPIO.PUD_DOWN)\r\n \r\n if GPIO.WIringPiISR(5,GPIO.EDGE_RISING, AddressOf InteruptModule.SensorInterupt) = -1 then\r\n MsgBox \"Could not register interupt for pin number 5\"\r\n return\r\n end if\r\n \r\n Timer1.Mode = Timer.ModeMultiple\r\nEnd Sub<\/pre>\n<p>The timer action event:<\/p>\n<pre lang=\"xojo\">Sub Action()\r\n If InteruptModule.DetectionCount &gt; 0 then\r\n Listbox1.AddRow(\"Motion detected\")\r\n \r\n InteruptModule.DetectionCount = 0\r\n end if\r\nEnd Sub<\/pre>\n<p>We create a module called InteruptModule, the Module has one property and one method:<\/p>\n<pre lang=\"xojo\">Protected DetectionCount As Integer\r\n\r\nProtected Sub SensorInterupt()\r\n \/\/ We need some pragmas here to make this safe since this is called from interupt.\r\n #Pragma StackOverflowChecking False\r\n #Pragma DisableBackgroundTasks\r\n \r\n \/\/ We only count the events here.\r\n DetectionCount = DetectionCount + 1\r\nEnd Sub<\/pre>\n<p>We need to patch Paul&#8217;s GPIO module, adding the following to the module (the function we are adding\u00a0likely already exists but is not fully implemented so verify that or just blindly override it) and remember to add the four constants also:<\/p>\n<pre lang=\"xojo\">Protected Const EDGE_FALLING = 1\r\nProtected Const EDGE_BOTH = 3\r\nProtected Const EDGE_RISING = 2\r\nProtected Const EDGE_SETUP = 0\r\n\r\nProtected Function WIringPiISR(pin as integer,edgeType as integer, p as ptr) As Integer\r\n \/\/This function registers a function to received interrupts on the specified pin.\r\n \/\/ The edgeType parameter is either INT_EDGE_FALLING, INT_EDGE_RISING,\r\n \/\/ INT_EDGE_BOTH or INT_EDGE_SETUP. If it is INT_EDGE_SETUP then no\r\n \/\/ initialisation of the pin will happen \u2013 it\u2019s assumed that you have already\r\n \/\/ setup the pin elsewhere (e.g. with the gpio program), but if you specify one\r\n \/\/ of the other types, then the pin will be exported and initialised as specified.\r\n \/\/ This is accomplished via a suitable call to the gpio utility program,\r\n \/\/ so it need to be available.\r\n \/\/\r\n \/\/ The pin number is supplied in the current mode \u2013 native wiringPi, BCM_GPIO,\r\n \/\/ physical or Sys modes.\r\n \/\/\r\n \/\/ This function will work in any mode, and does not need root privileges to work.\r\n \/\/\r\n \/\/ The function will be called when the interrupt triggers.\r\n \/\/ When it is triggered, it\u2019s cleared in the dispatcher before calling your\r\n \/\/ function, so if a subsequent interrupt fires before you finish your handler,\r\n \/\/ then it won\u2019t be missed. (However it can only track one more interrupt,\r\n \/\/ if more than one interrupt fires while one is being handled then they will be ignored)\r\n \/\/\r\n \/\/ This function is run at a high priority (if the program is run\r\n \/\/ using sudo, or as root) and executes concurrently with the main program.\r\n \/\/ It has full access to all the global variables, open file handles and so on.\r\n \/\/\r\n \/\/ See the isr.c example program for more details on how to use this feature.\r\n \r\n \r\n #If TargetARM And TargetLinux Then\r\n Soft Declare Function wpISR Lib \"libwiringPi.so\" Alias \"wiringPiISR\" (pin As Integer, mode As Integer, p as ptr) as Integer\r\n return wpISR(pin, edgeType,p)\r\n #Endif\r\n \r\nEnd Function<\/pre>\n<p><strong>The Xojo code without using interrupts (polling the hardware directly):<\/strong><\/p>\n<p>I have a timer to poll the sensor. we set the timer up and the pin mode in the Window Open event.<\/p>\n<pre lang=\"xojo\">Sub Open()\r\n\r\nConst kInfraRedSensorPin = 5\r\n\r\nGPIO.SetupGPIO\r\n\r\nGPIO.PinMode(kInfraRedSensorPin, GPIO.INPUT)\r\n\r\nTimer1.Mode = Timer.ModeMultiple\r\nTimer1.Enabled = true\r\n\r\nEnd Sub<\/pre>\n<p>In the Timer.Action event I got the following code:<\/p>\n<p>(If you want it more responsive then you can increase the cycles of the for loop)<\/p>\n<pre lang=\"xojo\">Sub Action()\r\n\r\nDim i as Integer\r\nConst kInfraRedSensorPin = 5\r\n\r\nfor n as Integer = 1 to 10\r\ni = GPIO.DigitalRead(kInfraRedSensorPin)\r\n\r\nif i = 1 then\r\nListbox1.AddRow \"Motion detected\"\r\nreturn\r\nend if\r\nApp.DoEvents(1)\r\nnext\r\n<\/pre>\n<p style=\"text-align: left;\">End Sub<\/p>\n<p>Thats all there\u00a0is to it!<\/p>\n<hr \/>\n<p><a href=\"http:\/\/s.click.aliexpress.com\/e\/3JmyzZR7Y\" target=\"_parent\"><img src=\"https:\/\/i2.wp.com\/g03.a.alicdn.com\/kf\/HTB1_wanJFXXXXc5aXXXq6xXFXXXK\/Multimetro-HoldPeak-HP-770D-Auto-Range-font-b-Digital-b-font-font-b-Multimeter-b-font.jpg_220x220.jpg?w=474\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product &#8211; HoldPeak HP-770D Multimeter<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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, adding\u00a0Mini IR Pyroelectric Infrared PIR &hellip; <a href=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Infrared motion detection<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":94,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"spay_email":""},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Pd75nk-1h","jetpack-related-posts":[{"id":507,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/mini-ir-pyroelectric-infrared-pir-motion-human-sensor-detector-module\/","url_meta":{"origin":79,"position":0},"title":"Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module","date":"May 1, 2016","format":false,"excerpt":"Connecting the\u00a0Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module. This guide is detail guide for one of the sensor types on this guide here:\u00a0Infrared motion detection We\u00a0found no official name for this sensor\u00a0but its sold as \"Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module\". WARNING do\u2026","rel":"","context":"Similar post","img":{"alt_text":"MiniMotionSensor","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/MiniMotionSensor.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":137,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/ir-infrared-obstacle-reflection-photoelectric-sensor\/","url_meta":{"origin":79,"position":1},"title":"IR Infrared Obstacle Reflection Photoelectric Sensor","date":"September 20, 2015","format":false,"excerpt":"In this example we are going to connect to infrared obstacle reflection photoelectric sensor motion detection sensor. I am not going to deny it, I find this sensor to be awful rubbish. Without reading the data you can hook it up and see it when the 2nd green lights goes\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/IRSensor-e1442754741492-225x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":94,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/","url_meta":{"origin":79,"position":2},"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":108,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/f85-vibration-sensor\/","url_meta":{"origin":79,"position":3},"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":79,"position":4},"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":[]},{"id":113,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/flame-sensor\/","url_meta":{"origin":79,"position":5},"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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/79"}],"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=79"}],"version-history":[{"count":27,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/79\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/79\/revisions\/600"}],"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=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}