{"id":644,"date":"2014-11-28T11:41:44","date_gmt":"2014-11-28T09:41:44","guid":{"rendered":"http:\/\/dety.net.ua\/?p=644"},"modified":"2014-11-28T11:42:10","modified_gmt":"2014-11-28T09:42:10","slug":"msp430-pir-motion-sensor-hc-s501-relay-module-beeper","status":"publish","type":"post","link":"https:\/\/dety.net.ua\/?p=644","title":{"rendered":"MSP430 + PIR Motion sensor HC-S501 + Relay Module + Beeper"},"content":{"rendered":"<p>A simple alarm system on MSP430 Launchpad based on <a href=\"http:\/\/blog.suspended-chord.info\/2010\/09\/12\/connecting-a-parallax-pir-sensor-module-to-the-launchpad\/\" target=\"_blank\">http:\/\/blog.suspended-chord.info\/2010\/09\/12\/connecting-a-parallax-pir-sensor-module-to-the-launchpad\/<\/a>.<\/p>\n<p>Code for Energia:<\/p>\n<blockquote><p>int calibrationTime = 30;<\/p>\n<p>\/\/the time when the sensor outputs a low impulse<br \/>\nlong unsigned int lowIn;<\/p>\n<p>\/\/the amount of milliseconds the sensor has to be low<br \/>\n\/\/before we assume all motion has stopped<br \/>\nlong unsigned int pause = 5000;<\/p>\n<p>boolean lockLow = true;<br \/>\nboolean takeLowTime;<\/p>\n<p>int pirPin = A5;\u00a0\u00a0\u00a0 \/\/the digital pin connected to the PIR sensor&#8217;s output<br \/>\nint ledPin = 13;<br \/>\nint spkPin = 12;<br \/>\nint relPin = 11;<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/SETUP<br \/>\nvoid setup(){<br \/>\n\/\/\u00a0 Serial.begin(9600);<br \/>\npinMode(pirPin, INPUT);<br \/>\npinMode(ledPin, OUTPUT);<br \/>\npinMode(spkPin, OUTPUT);<br \/>\npinMode(relPin, OUTPUT);<br \/>\ndigitalWrite(pirPin, LOW);<\/p>\n<p>\/\/give the sensor some time to calibrate<br \/>\n\/\/\u00a0 Serial.print(&#8220;calibrating sensor &#8220;);<br \/>\nfor(int i = 0; i &lt; calibrationTime; i++){<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.print(&#8220;.&#8221;);<br \/>\ndelay(1000);<br \/>\n}<br \/>\n\/\/\u00a0\u00a0\u00a0 Serial.println(&#8221; done&#8221;);<br \/>\n\/\/\u00a0\u00a0\u00a0 Serial.println(&#8220;SENSOR ACTIVE&#8221;);<br \/>\ndelay(50);<br \/>\n}<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/LOOP<br \/>\nvoid loop(){<\/p>\n<p>if(digitalRead(pirPin) == HIGH){<br \/>\ndigitalWrite(ledPin, HIGH);\u00a0\u00a0 \/\/the led visualizes the sensors output pin state<br \/>\ndigitalWrite(spkPin, HIGH);<br \/>\ndigitalWrite(relPin, LOW);<br \/>\nif(lockLow){<br \/>\n\/\/makes sure we wait for a transition to LOW before any further output is made:<br \/>\nlockLow = false;<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.println(&#8220;&#8212;&#8220;);<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.print(&#8220;motion detected at &#8220;);<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.print(millis()\/1000);<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.println(&#8221; sec&#8221;);<br \/>\ndelay(50);<br \/>\n}<br \/>\ntakeLowTime = true;<br \/>\n}<\/p>\n<p>if(digitalRead(pirPin) == LOW){<br \/>\ndigitalWrite(ledPin, LOW);\u00a0 \/\/the led visualizes the sensors output pin state<br \/>\ndigitalWrite(spkPin, LOW);<br \/>\ndigitalWrite(relPin, HIGH);<br \/>\nif(takeLowTime){<br \/>\nlowIn = millis();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/save the time of the transition from high to LOW<br \/>\ntakeLowTime = false;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/make sure this is only done at the start of a LOW phase<br \/>\n}<br \/>\n\/\/if the sensor is low for more than the given pause,<br \/>\n\/\/we assume that no more motion is going to happen<br \/>\nif(!lockLow &amp;&amp; millis() &#8211; lowIn &gt; pause){<br \/>\n\/\/makes sure this block of code is only executed again after<br \/>\n\/\/a new motion sequence has been detected<br \/>\nlockLow = true;<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.print(&#8220;motion ended at &#8220;);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/output<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.print((millis() &#8211; pause)\/1000);<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.println(&#8221; sec&#8221;);<br \/>\ndelay(50);<br \/>\n}<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>The result on the maket plate:<\/p>\n<p><a href=\"https:\/\/dety.net.ua\/wp-content\/uploads\/2014\/11\/IMG_20141128_113146.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-645\" src=\"https:\/\/dety.net.ua\/wp-content\/uploads\/2014\/11\/IMG_20141128_113146-300x225.jpg\" alt=\"IMG_20141128_113146\" width=\"300\" height=\"225\" srcset=\"https:\/\/dety.net.ua\/wp-content\/uploads\/2014\/11\/IMG_20141128_113146-300x225.jpg 300w, https:\/\/dety.net.ua\/wp-content\/uploads\/2014\/11\/IMG_20141128_113146.jpg 320w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>The system will turn on the light before a door and make sound in the another room as an alert.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A simple alarm system on MSP430 Launchpad based on http:\/\/blog.suspended-chord.info\/2010\/09\/12\/connecting-a-parallax-pir-sensor-module-to-the-launchpad\/. Code for Energia: int calibrationTime = 30; \/\/the time when the sensor outputs a low impulse long unsigned int lowIn; \/\/the amount of milliseconds the sensor has to be low \/\/before we assume all motion has stopped long unsigned int pause = 5000; boolean lockLow [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-gadgets","category-novosti"],"_links":{"self":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=644"}],"version-history":[{"count":2,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":647,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions\/647"}],"wp:attachment":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}