{"id":345,"date":"2015-11-08T03:02:51","date_gmt":"2015-11-08T03:02:51","guid":{"rendered":"https:\/\/einhugur.com\/blog\/?page_id=345"},"modified":"2016-05-01T19:34:58","modified_gmt":"2016-05-01T19:34:58","slug":"using-logic-gates","status":"publish","type":"page","link":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/using-logic-gates\/","title":{"rendered":"Using logic gates"},"content":{"rendered":"<p>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 have sensor that does some boolean logic then sends it as RF signal to the Raspberry PI as one result then you may want to do some logic on the sensor side, or you might need to save up to not use to many GPIO pins.<\/p>\n<p>For a test case then we will be doing a circuit in this example that uses one GPIO pin that should get triggered when a Infrared sensor is triggered and if there is \u00a0fire. <em>(Yes I know this is not good real world example but its good for the purpose of learning how to use the logic gates)<\/em><\/p>\n<p>So what we will be using is one NOT gate and one AND gate, we will put the NOT gate infant of the Flame sensor, and then the AND gate will connect the result of the NOT gate with the result of the Infra red sensor.<\/p>\n<p><strong>Disclaimer:<\/strong><br \/>\nWe 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<h2>Pieces we use in this example:<\/h2>\n<ul>\n<li>Raspberry PI 2<\/li>\n<li>Cobbler and Cobbler cable<\/li>\n<li>Bread board and mini breadboard<\/li>\n<li>1 K-ohm resistor<\/li>\n<li>560\u00a0ohm resistor<\/li>\n<li>IC74HC08N and gate circuit.<\/li>\n<li>IC74HC04N not gate circuit.<\/li>\n<li>Flame sensor<span style=\"color: #808080;\"> (Can use some other sensors also since this guide is focusing on gates and not the sensors)<\/span><\/li>\n<li>HC-SR501 infrared sensor. <span style=\"color: #808080;\">\u00a0(Can use some other sensors also since this guide is focusing on gates and not the sensors)<\/span><\/li>\n<\/ul>\n<p><strong>If you don&#8217;t know resistor color codings to find correct resistors then you can click the link bellow<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/einhugur.com\/blog\/index.php\/basic-electronics\/resistor-color-codings\/\">Resistor color codings<\/a><\/li>\n<\/ul>\n<h2>Putting it all together:<\/h2>\n<p>We will be focusing on our logic circuit as a separate piece first and worry about the sensors and GPIO part later.<\/p>\n<p>I used\u00a0<strong>IC74HC08N<\/strong> for the AND gate circuit which has 4 separate AND gates in it and I used\u00a0<strong>IC74HC04N<\/strong> for the NOT gate which has 6 separate NOT gates in it.<\/p>\n<p><a href=\"https:\/\/einhugur.com\/blog\/index.php\/logic-gates\/\">Click here for more explanations on gates and suggestion of other gates that is good to have in your collection.<\/a><\/p>\n<p>So this here is how our logic circuit would look like:<\/p>\n<p><a href=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?ssl=1\"><img data-attachment-id=\"347\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/using-logic-gates\/logiccircuits\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?fit=798%2C505&amp;ssl=1\" data-orig-size=\"798,505\" 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=\"LogicCircuits\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?fit=300%2C190&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?fit=474%2C300&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-347\" src=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?resize=474%2C300&#038;ssl=1\" alt=\"LogicCircuits\" width=\"474\" height=\"300\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?w=798&amp;ssl=1 798w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuits.png?resize=300%2C190&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>The circuits I used \u00a0had multiple gates in them but we only use one in each. You could use them all if you wanted.<\/p>\n<p>Point marked as\u00a02\u00a0will go to the Infrared sensor, point marked as 3 will go to the Flame sensor, and Point marked number one will go to GPIO pin on the Raspberry PI. Red 5V will go to 5V and Blue will go to ground.<\/p>\n<p>The outputs from the gates can be up to 5V so we have to use resistors before sending the result back to the Raspberry PI GPIO.<\/p>\n<blockquote><p>TTL gates usually are as follows:<\/p>\n<p style=\"padding-left: 30px;\">Input: 0 V to 0,8V for LOW, and 2 to 5V for HIGH<\/p>\n<p style=\"padding-left: 30px;\">Output: 0 V to 0,5 V for LOW and 2,7V to 5V for HIGH<\/p>\n<p>For CMOS gate then values can be more like:<\/p>\n<p style=\"padding-left: 30px;\">Input: 0 V to 1,5V for LOW, and 3,5\u00a0to 5V for HIGH<\/p>\n<p style=\"padding-left: 30px;\">Output: 0 V to 0,05 V for LOW and 4,95V to 5V for HIGH<\/p>\n<\/blockquote>\n<p>We apply same method as we used in the Ultrasonic examples to calculate the Resistors. \u00a0R1 was chosen as 1K, so R2 is calculated as:<\/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 \/>\nR2 = 515 Ohm, nearest one with higher value I had was 560 Ohm which I used.<\/p>\n<p style=\"text-align: left;\">For the inputs into the Logic gates from the Flame sensor and Infra red sensor we will risk that high and low fall in between the TTL spec shown above for Input of gates. If it does not then we would need to probably work around it with some transistors.<\/p>\n<p>On a mini breadboard this circuit will look like this:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?ssl=1\"><img data-attachment-id=\"348\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/using-logic-gates\/logiccircuitsbb\/\" data-orig-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?fit=451%2C381&amp;ssl=1\" data-orig-size=\"451,381\" 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=\"LogicCircuitsBB\" data-image-description=\"\" data-medium-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?fit=300%2C253&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?fit=451%2C381&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-348\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?resize=451%2C381&#038;ssl=1\" alt=\"LogicCircuitsBB\" width=\"451\" height=\"381\" srcset=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?w=451&amp;ssl=1 451w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicCircuitsBB.png?resize=300%2C253&amp;ssl=1 300w\" sizes=\"(max-width: 451px) 100vw, 451px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>At top you can see the wires that will go to the main board. (or directly to the sensors)<\/p>\n<p style=\"padding-left: 30px;\"><em>Red: +5V<br \/>\nBlue: GND<br \/>\nWhite: GPIO<br \/>\nGreen: Infrared sensor<br \/>\nYellow: Flame sensor<\/em><\/p>\n<p>Once this is done then all we will have to do is to connect the Red, White, Green, Yellow and Blue wires to the main board which will have the Cobbler and the sensors.<\/p>\n<p><a href=\"https:\/\/einhugur.com\/blog\/index.php\/understanding-your-breadboard-and-placement-of-raspberry-cobbler\/\">If you are not familiar with breadboards and placement of the cobbler then check this link out.<\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?ssl=1\"><img data-attachment-id=\"365\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/using-logic-gates\/logicwithbreadboard\/\" data-orig-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?fit=600%2C598&amp;ssl=1\" data-orig-size=\"600,598\" 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=\"LogicWithBreadBoard\" data-image-description=\"\" data-medium-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?fit=300%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?fit=474%2C472&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-365\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?resize=474%2C472&#038;ssl=1\" alt=\"LogicWithBreadBoard\" width=\"474\" height=\"472\" srcset=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/11\/LogicWithBreadBoard.png?resize=300%2C300&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>Note since we had so many blue and red wires then I connected the Reds and blue to the Voltage rail on the breadboard.<\/p>\n<p>The white wire got connected to GPIO 5.<\/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<h3>The Xojo code:<\/h3>\n<p>Here we just use exactly same code as for the <a href=\"https:\/\/einhugur.com\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/\">Infrared sensor guide<\/a>. <em>(Using the Interrupt version of it)<\/em><\/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><strong>The open event of the Window:<\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"xojo\">\n<pre class=\"de1\"><span class=\"kw1\">Sub<\/span> Open<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>\r\n GPIO<span class=\"sy0\">.<\/span><span class=\"me1\">PinMode<\/span><span class=\"br0\">(<\/span><span class=\"nu1\">5<\/span>,GPIO<span class=\"sy0\">.<\/span><span class=\"me1\">INPUT<\/span><span class=\"br0\">)<\/span>\r\n GPIO<span class=\"sy0\">.<\/span><span class=\"me1\">PullUpDnControl<\/span><span class=\"br0\">(<\/span><span class=\"nu1\">5<\/span>,GPIO<span class=\"sy0\">.<\/span><span class=\"me1\">PUD_DOWN<\/span><span class=\"br0\">)<\/span>\r\n\u00a0\r\n <span class=\"kw1\">if<\/span> GPIO<span class=\"sy0\">.<\/span><span class=\"me1\">WIringPiISR<\/span><span class=\"br0\">(<\/span><span class=\"nu1\">5<\/span>,GPIO<span class=\"sy0\">.<\/span><span class=\"me1\">EDGE_RISING<\/span>, <span class=\"kw1\">AddressOf<\/span> InteruptModule<span class=\"sy0\">.<\/span><span class=\"me1\">SensorInterupt<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">=<\/span> <span class=\"sy0\">-<\/span><span class=\"nu1\">1<\/span> <span class=\"kw1\">then<\/span>\r\n MsgBox <span class=\"st0\">\"Could not register interupt for pin number 5\"<\/span>\r\n <span class=\"kw1\">return<\/span>\r\n <span class=\"kw1\">end<\/span> <span class=\"kw1\">if<\/span>\r\n\u00a0\r\n Timer1<span class=\"sy0\">.<\/span><span class=\"me1\">Mode<\/span> <span class=\"sy0\">=<\/span> Timer<span class=\"sy0\">.<\/span><span class=\"me1\">ModeMultiple<\/span>\r\n<span class=\"kw1\">End<\/span> <span class=\"kw1\">Sub<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>The timer action event:<\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"xojo\">\n<pre class=\"de1\"><span class=\"kw1\">Sub<\/span> Action<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>\r\n <span class=\"kw1\">If<\/span> InteruptModule<span class=\"sy0\">.<\/span><span class=\"me1\">DetectionCount<\/span> &amp;gt; <span class=\"nu1\">0<\/span> <span class=\"kw1\">then<\/span>\r\n Listbox1<span class=\"sy0\">.<\/span><span class=\"me1\">AddRow<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Motion detected\"<\/span><span class=\"br0\">)<\/span>\r\n\u00a0\r\n InteruptModule<span class=\"sy0\">.<\/span><span class=\"me1\">DetectionCount<\/span> <span class=\"sy0\">=<\/span> <span class=\"nu1\">0<\/span>\r\n <span class=\"kw1\">end<\/span> <span class=\"kw1\">if<\/span>\r\n<span class=\"kw1\">End<\/span> <span class=\"kw1\">Sub<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>We create a module called InteruptModule, the Module has one property and one method:<\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"xojo\">\n<pre class=\"de1\"><span class=\"kw1\">Protected<\/span> DetectionCount <span class=\"kw1\">As<\/span> <span class=\"kw2\">Integer<\/span>\r\n\u00a0\r\n<span class=\"kw1\">Protected<\/span> <span class=\"kw1\">Sub<\/span> SensorInterupt<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>\r\n <span class=\"co2\">\/\/ We need some pragmas here to make this safe since this is called from interupt.<\/span>\r\n <span class=\"kw3\">#Pragma<\/span> StackOverflowChecking <span class=\"kw1\">False<\/span>\r\n <span class=\"kw3\">#Pragma<\/span> DisableBackgroundTasks\r\n\u00a0\r\n <span class=\"co2\">\/\/ We only count the events here.<\/span>\r\n DetectionCount <span class=\"sy0\">=<\/span> DetectionCount <span class=\"sy0\">+<\/span> <span class=\"nu1\">1<\/span>\r\n<span class=\"kw1\">End<\/span> <span class=\"kw1\">Sub<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>We need to patch Paul\u2019s 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<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"xojo\">\n<pre class=\"de1\"><span class=\"kw1\">Protected<\/span> <span class=\"kw1\">Const<\/span> EDGE_FALLING <span class=\"sy0\">=<\/span> <span class=\"nu1\">1<\/span>\r\n<span class=\"kw1\">Protected<\/span> <span class=\"kw1\">Const<\/span> EDGE_BOTH <span class=\"sy0\">=<\/span> <span class=\"nu1\">3<\/span>\r\n<span class=\"kw1\">Protected<\/span> <span class=\"kw1\">Const<\/span> EDGE_RISING <span class=\"sy0\">=<\/span> <span class=\"nu1\">2<\/span>\r\n<span class=\"kw1\">Protected<\/span> <span class=\"kw1\">Const<\/span> EDGE_SETUP <span class=\"sy0\">=<\/span> <span class=\"nu1\">0<\/span>\r\n\u00a0\r\n<span class=\"kw1\">Protected<\/span> <span class=\"kw1\">Function<\/span> WIringPiISR<span class=\"br0\">(<\/span>pin <span class=\"kw1\">as<\/span> <span class=\"kw2\">integer<\/span>,edgeType <span class=\"kw1\">as<\/span> <span class=\"kw2\">integer<\/span>, p <span class=\"kw1\">as<\/span> <span class=\"kw2\">ptr<\/span><span class=\"br0\">)<\/span> <span class=\"kw1\">As<\/span> <span class=\"kw2\">Integer<\/span>\r\n <span class=\"co2\">\/\/This function registers a function to received interrupts on the specified pin.<\/span>\r\n <span class=\"co2\">\/\/ The edgeType parameter is either INT_EDGE_FALLING, INT_EDGE_RISING,<\/span>\r\n <span class=\"co2\">\/\/ INT_EDGE_BOTH or INT_EDGE_SETUP. If it is INT_EDGE_SETUP then no<\/span>\r\n <span class=\"co2\">\/\/ initialisation of the pin will happen \u2013 it\u2019s assumed that you have already<\/span>\r\n <span class=\"co2\">\/\/ setup the pin elsewhere (e.g. with the gpio program), but if you specify one<\/span>\r\n <span class=\"co2\">\/\/ of the other types, then the pin will be exported and initialised as specified.<\/span>\r\n <span class=\"co2\">\/\/ This is accomplished via a suitable call to the gpio utility program,<\/span>\r\n <span class=\"co2\">\/\/ so it need to be available.<\/span>\r\n <span class=\"co2\">\/\/<\/span>\r\n <span class=\"co2\">\/\/ The pin number is supplied in the current mode \u2013 native wiringPi, BCM_GPIO,<\/span>\r\n <span class=\"co2\">\/\/ physical or Sys modes.<\/span>\r\n <span class=\"co2\">\/\/<\/span>\r\n <span class=\"co2\">\/\/ This function will work in any mode, and does not need root privileges to work.<\/span>\r\n <span class=\"co2\">\/\/<\/span>\r\n <span class=\"co2\">\/\/ The function will be called when the interrupt triggers.<\/span>\r\n <span class=\"co2\">\/\/ When it is triggered, it\u2019s cleared in the dispatcher before calling your<\/span>\r\n <span class=\"co2\">\/\/ function, so if a subsequent interrupt fires before you finish your handler,<\/span>\r\n <span class=\"co2\">\/\/ then it won\u2019t be missed. (However it can only track one more interrupt,<\/span>\r\n <span class=\"co2\">\/\/ if more than one interrupt fires while one is being handled then they will be ignored)<\/span>\r\n <span class=\"co2\">\/\/<\/span>\r\n <span class=\"co2\">\/\/ This function is run at a high priority (if the program is run<\/span>\r\n <span class=\"co2\">\/\/ using sudo, or as root) and executes concurrently with the main program.<\/span>\r\n <span class=\"co2\">\/\/ It has full access to all the global variables, open file handles and so on.<\/span>\r\n <span class=\"co2\">\/\/<\/span>\r\n <span class=\"co2\">\/\/ See the isr.c example program for more details on how to use this feature.<\/span>\r\n\u00a0\r\n\u00a0\r\n <span class=\"kw3\">#If<\/span> TargetARM <span class=\"kw1\">And<\/span> TargetLinux <span class=\"kw1\">Then<\/span>\r\n <span class=\"kw1\">Soft<\/span> <span class=\"kw1\">Declare<\/span> <span class=\"kw1\">Function<\/span> wpISR <span class=\"kw1\">Lib<\/span> <span class=\"st0\">\"libwiringPi.so\"<\/span> Alias <span class=\"st0\">\"wiringPiISR\"<\/span> <span class=\"br0\">(<\/span>pin <span class=\"kw1\">As<\/span> <span class=\"kw2\">Integer<\/span>, mode <span class=\"kw1\">As<\/span> <span class=\"kw2\">Integer<\/span>, p <span class=\"kw1\">as<\/span> <span class=\"kw2\">ptr<\/span><span class=\"br0\">)<\/span> <span class=\"kw1\">as<\/span> <span class=\"kw2\">Integer<\/span>\r\n <span class=\"kw1\">return<\/span> wpISR<span class=\"br0\">(<\/span>pin, edgeType,p<span class=\"br0\">)<\/span>\r\n <span class=\"kw3\">#Endif<\/span>\r\n\u00a0\r\n<span class=\"kw1\">End<\/span> <span class=\"kw1\">Function<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>Run it with sudo to get admin mode to get full access to GPIO.\u00a0<\/strong><\/p>\n<p>When running it then we got record in the listbox whenever the flame sensor detected fire and the infrared sensor detected a human.<\/p>\n<p><code><br \/>\n<a href=\"http:\/\/s.click.aliexpress.com\/e\/ZbqFEQnmY\" target=\"_parent\"><img src=\"https:\/\/i0.wp.com\/g04.a.alicdn.com\/kf\/HTB14YxAHFXXXXbEXFXXq6xXFXXXS\/1-4w-font-b-resistors-b-font-pack-121-values-x-20pcs-2420pcs-0-33-4.jpg_220x220.jpg?w=474\" alt=\"\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product - Carbon film resistor assorted kit 0.33 ohm - 4.7M ohm 1\/4w 5% tolerance commonly used 122 values 20x 122= 2440pcs<\/span><\/a><br \/>\n<\/code><\/p>\n<p><code><br \/>\n<a href=\"http:\/\/s.click.aliexpress.com\/e\/z3NvvJIYr\" target=\"_parent\"><img src=\"https:\/\/i1.wp.com\/g03.a.alicdn.com\/kf\/HTB1g.ZcIpXXXXbCXpXXq6xXFXXXA\/15Values-TO-92-Transistor-Assorted-Kit-S9012-S9013-S9014-S8050-S8550-font-b-2N3904-b-font.jpg_220x220.jpg?w=474\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product - 15Values TO-92 Transistor Assorted Kit S9012 S9013 S9014 S8050 S8550 2N3904 2N3906 BC327 BC337 Tl431 A42 A92 A1015 C1815 13001<\/span><\/a><br \/>\n<\/code><\/p>\n<p><code><br \/>\n<a href=\"http:\/\/s.click.aliexpress.com\/e\/iIiqnAayZ\" target=\"_parent\"><img src=\"https:\/\/i1.wp.com\/g02.a.alicdn.com\/kf\/HTB1nynvHVXXXXaiXFXXq6xXFXXXQ\/6pcs-font-b-mini-b-font-solderless-prototype-font-b-breadboard-b-font-protoboard-170-tie.jpg_220x220.jpg?w=474\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product - 5 piece mini solderless prototype breadboard \/ protoboard 170 tie points 45.7mm x 34.5mm<\/span><\/a><br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 have sensor that does some &hellip; <a href=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/using-logic-gates\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using logic gates<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":94,"menu_order":142,"comment_status":"open","ping_status":"closed","template":"","meta":{"spay_email":""},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Pd75nk-5z","jetpack-related-posts":[{"id":94,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/","url_meta":{"origin":345,"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":617,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/bmp180-barometer-sensor\/","url_meta":{"origin":345,"position":1},"title":"BMP180 Barometer sensor","date":"May 22, 2016","format":false,"excerpt":"In this example we are going to connect to BMP180 barometer sensor, we will be using brake-out board called GY-68. This sensor is really small even the full brake-outboard is only size of a fingernail. The board does not come with the header pins soldered on, but with mine the\u2026","rel":"","context":"Similar post","img":{"alt_text":"BMP180Soldered","src":"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/BMP180Soldered.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":353,"url":"https:\/\/einhugur.net\/blog\/index.php\/logic-gates\/","url_meta":{"origin":345,"position":2},"title":"Logic gates","date":"November 4, 2015","format":false,"excerpt":"The AND gate: Result of AND gate is (a AND b) or if there are many inputs (a AND b AND c) The NAND gate: Result of NAND gate is NOT (a\u00a0AND\u00a0b) or if there are many inputs NOT (a AND b AND c) The OR\u00a0gate: Result of OR\u00a0gate is\u00a0\u00a0(a\u2026","rel":"","context":"Similar post","img":{"alt_text":"AND Gates","src":"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/11\/AND-Gates.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":674,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/i2c-logic-level-converter-building-our-own\/","url_meta":{"origin":345,"position":3},"title":"I2C logic level converter - Building our own","date":"May 29, 2016","format":false,"excerpt":"In this example we are going to create a I2C logic level converter. Disclaimer: 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. The problem: Some I2C\u2026","rel":"","context":"Similar post","img":{"alt_text":"Be careful about which wires are actually connected and which are not. The SCL wires are crossing others but you see by the dots which they actually connect to.","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/I2CScheme.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":507,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/mini-ir-pyroelectric-infrared-pir-motion-human-sensor-detector-module\/","url_meta":{"origin":345,"position":4},"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":108,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/f85-vibration-sensor\/","url_meta":{"origin":345,"position":5},"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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/345"}],"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=345"}],"version-history":[{"count":17,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/345\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/345\/revisions\/484"}],"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=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}