{"id":346,"date":"2022-01-02T11:16:04","date_gmt":"2022-01-02T18:16:04","guid":{"rendered":"https:\/\/shmalex.com\/?p=346"},"modified":"2022-01-02T11:20:53","modified_gmt":"2022-01-02T18:20:53","slug":"pic18-mcu-ir-motion-detector","status":"publish","type":"post","link":"https:\/\/electronsandxploits.com\/?p=346","title":{"rendered":"PIC18 MCU IR motion detector"},"content":{"rendered":"\n<p>       Using a PIC18 MCU a IR motion detector circuit that is currently configured for security purposes was built. The code and I\/O devices can be changed for other functions such as triggering an automatic door with a servo or many other functions that require a motion sensor to be used. The circuit is built on a breadboard and uses the PIC18F45K20 MCU connected to a PIR sensor configured as an input device, the range of the sensor was adjusted to monitor an area of approximately 3 meters and adjusted to reset after 3 seconds. Three devices are configured for output from the MCU, these are an LED, buzzer and LCD display.&nbsp; The PIC18F45K20 MCU is programmed to display the message \u201cNO MOTION\u201d on the LCD during the circuit&#8217;s steady state process. When motion is detected in the designated area a signal is sent from the PIR sensor to the MCU, the MCU processes the data and activates the LCD to display \u201cMOTION DETECTED!\u201d accompanied by turning on an LED and buzzer. After 5 seconds, the system will reset and await for the next signal from the PIR sensor. After the 5 seconds have passed from time of activation the circuit will go back to its steady state condition.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1080\" height=\"922\" src=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project-1080x922.png\" alt=\"\" class=\"wp-image-350\" srcset=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project-1080x922.png 1080w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project-300x256.png 300w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project-768x655.png 768w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project-600x512.png 600w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project-945x806.png 945w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/pir-project.png 1269w\" sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><figcaption>Circuit Design<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"386\" src=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520.jpg\" alt=\"\" class=\"wp-image-240\" srcset=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520.jpg 725w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520-300x160.jpg 300w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520-600x319.jpg 600w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><figcaption>PIC18 MCU pins<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"1080\" src=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart-838x1080.png\" alt=\"\" class=\"wp-image-351\" srcset=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart-838x1080.png 838w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart-233x300.png 233w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart-768x990.png 768w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart-600x774.png 600w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart-945x1218.png 945w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/motion-sensor-flow-chart.png 975w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><figcaption>Flow chart<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1080\" height=\"810\" src=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-1080x810.jpg\" alt=\"\" class=\"wp-image-352\" srcset=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-1080x810.jpg 1080w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-300x225.jpg 300w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-768x576.jpg 768w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-1536x1152.jpg 1536w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-2048x1536.jpg 2048w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-600x450.jpg 600w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2022\/01\/IMG_20200429_215416933-945x709.jpg 945w\" sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><figcaption>Working Circuit<\/figcaption><\/figure><\/div>\n\n\n\n<p>Below is the code written in C for this design.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/* IR Motion Sensor for Security\n * This program will detect motion using a PIR sensor. If no motion is detected an LCD\n * will give a message \"NO MOTION.\" When motion is detected an LED and buzzer will turn \n * on, and the LCD message will change to \"MOTION DETECTED!.\" The IR sensor takes 5 seconds\n * to reset and after that it will be ready to trigger the alarms again.\n *\/\n\/\/ RB0 wired to LCD rs (pin 33)\n\/\/ RB1 wired to LCD en (pin 34)\n\/\/ RB2 wired to PIR sensor (pin 35)\n\/\/ RC0 wired to LED (pin 15)\n\/\/ RC1 wired to Buzzer (pin 16)\n\n#pragma config FOSC = INTIO67 \/\/Internal oscillator block, port function on RA6 and RA7\n#pragma config BORV = 30, PWRT = ON \/\/Brown-Out-Rese Voltage=3V, Power-up Timer enable enabled\n#pragma config WDTEN = OFF \/\/Watchdog timer disabled\n#pragma config PBADEN = OFF \/\/PORT A\/D disabled\n#pragma config HFOFST = OFF, MCLRE = OFF \/\/Fast Start-up and MCLR pin disabled\n#include &lt;pic18f45k20.h&gt;\n\n#define ldata PORTD         \/\/lcd data pins on PORTD\n#define rs PORTBbits.RB0    \/\/rs of lcd on PORTB0 (pin 33)\n#define en PORTBbits.RB1    \/\/en of lcd on PORTB1 (pin 34))\n\nvoid MSDelay(unsigned char);    \/\/Function to create 1 ms of delay\nvoid lcdcmd(unsigned char);     \/\/command function\nvoid lcddata (unsigned char);    \/\/data function\n\n\nvoid __interrupt(high_priority) Motion_Sensor(void) \/\/Interrupt from trigger of motion sensor\n{\n    INTCON3bits.INT2IF = 0; \/\/clear INT2IF flag\n    PORTCbits.RC0 = 1;      \/\/turns on LED (RC0, pin 15))\n    lcdcmd(0x01);       \/\/clear LCD\n    lcddata('M');        \/\/display M\n    lcddata('O');        \/\/display O\n    lcddata('T');        \/\/display T\n    lcddata('I');        \/\/display I\n    lcddata('O');        \/\/display O\n    lcddata('N');        \/\/display N\n    lcddata(' ');        \/\/display _\n    lcddata('D');        \/\/display D\n    lcddata('E');        \/\/display E\n    lcddata('T');        \/\/display T\n    lcddata('E');        \/\/display E\n    lcddata('C');        \/\/display C\n    lcddata('T');        \/\/display T\n    lcddata('E');        \/\/display E\n    lcddata('D');        \/\/display D\n    lcddata('!');        \/\/display !\n    \n    unsigned char a, b;  \/\/loop delay for sensor to reset\n    for(a=0; a&lt;255; a++)\n    {\n        PORTCbits.RC1 = 1;  \/\/Set Buzzer (RC1, pin 16)\n        MSDelay(1);\n        PORTCbits.RC1 = 0;  \/\/Clear Buzzer (RC1, pin 16)\n        MSDelay(1);\n        for(b=0; b&lt;56; b++);\n    }\n    \/\/Could add LED to turn off here rather than having it turn off in main\n}\n\n\nvoid main(void)\n{\n    TRISB = 0xFC;       \/\/defining RB2 as input, RB0,RB1,RB3,RB4... as output\n    TRISC = 0;          \/\/defining PORTC as output\n    TRISD = 0;          \/\/defining PORTD as output for LCD data\n    \n    RCONbits.IPEN = 1;         \/\/enable priority\n    INTCONbits.GIEH = 1;       \/\/Global high priority enable\n    INTCONbits.GIEL = 1;       \/\/Global peripheral enable\n    INTCON3bits.INT2IE = 1;    \/\/Enable INT2\n    INTCON3bits.INT2IP = 1;    \/\/define INT2 as high priority\n    INTCON2bits.INTEDG2 = 1;   \/\/trigger INT2 on rising edge\n        \n    PORTCbits.RC0 = 0;  \/\/Turns off LED (RC0, pin 15)\n    PORTCbits.RC1 = 0;  \/\/Turns off Buzzer (RC1, pin 16)\n    \n    en = 0;             \/\/initializing, clear enable\n    MSDelay(50);\n    lcdcmd(0x38);       \/\/initialize LCD 2 lines, 5x7 matrix\n    lcdcmd(0x0E);       \/\/display on, cursor\n    lcdcmd(0x01);       \/\/clear LCD\n    lcdcmd(0x06);       \/\/shift cursor right\n    lcddata('N');        \/\/display N\n    lcddata('O');        \/\/display O\n    lcddata(' ');        \/\/display _\n    lcddata('M');        \/\/display M\n    lcddata('O');        \/\/display O\n    lcddata('T');        \/\/display T\n    lcddata('I');        \/\/display I\n    lcddata('O');        \/\/display O\n    lcddata('N');        \/\/display N\n    MSDelay(255);\n}\n\nvoid lcdcmd(unsigned char value) \/\/command\n{\n    ldata = value;      \/\/put received argument on PORTD\n    rs = 0;             \/\/its the data\n    en = 1;             \/\/set enable\n    MSDelay(2);\n    en = 0;             \/\/clear enable\n    MSDelay(5);   \n}\n\nvoid lcddata (unsigned char value) \/\/data\n{\n    ldata = value;   \/\/put received argument on PORTD\n    rs = 1; \/\/sets the data\n    en = 1; \/\/set enable\n    MSDelay(2);\n    en = 0; \/\/clear enable\n    MSDelay(5);\n}\n\nvoid MSDelay(unsigned char mili) \/\/For loop delay, loops for 1ms\n{\n    unsigned char i, j;\n    for(i=0; i &lt; mili; i++)\n        for(j=0; j &lt; 100; j++);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using a PIC18 MCU a IR motion detector circuit that is currently configured for security purposes was built. The code and I\/O devices can be changed for other functions such as triggering an automatic door with a servo or many other functions that require a motion sensor to be used. The circuit is built on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":350,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects"],"_links":{"self":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346"}],"version-history":[{"count":5,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions\/356"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/media\/350"}],"wp:attachment":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}