Freescale Semiconductor Application Note Document Number: AN4305 Rev. 0, August 2011 Low-Cost Passive IR Sensing with RS08KA by: Pavel Krenek Applications Engineering Roznov CSC, Czech Republic Contents 1 Introduction This document describes how to implement a passive infrared (PIR) motion detector using Freescale's low-cost RS08KA microcontroller family. The base function of this design is to switch on appliances, especially lighting, in response to movement. The main idea behind this solution is to replace the common analog motion detector with an ultra low-cost digital design, based on the MC9RS08KA1. The most important part of this document consists of the detection algorithm and the method for measuring the analog signal from the PIR sensor. This document focuses on a software sigma-delta analog-todigital conversion method, and shows in detail all parts of the hardware and software implementation of a passive infrared (PIR) detector. 1 Introduction.................................................................1 2 Sigma-delta conversion ..............................................3 2.1 Sigma-delta conversion method..................................3 2.2 Implementation of sigma-delta conversion in PIR application...................................................................3 3 Hardware implementation...........................................5 3.1 Power supply and switching.......................................5 3.2 User input measurement.............................................7 3.3 Sigma-delta measurement...........................................8 3.4 Zero-cross detection and light dimming.....................9 3.5 Complete schematic....................................................9 4 Software implementation..........................................12 4.1 Precharging and discharging bandgap voltage.........13 The main feature of this document is a demonstration of the capabilities of the low-cost RS08KA series of MCUs. It also shows fully functional and practical implementation and use of ultra-low-cost microcontrollers, but does not include some necessary peripherals for measuring and creating complex solutions using PIR sensing. 4.2 Single-sample measurement.....................................15 4.3 Filtration of signal from PIR Sensor.........................17 4.4 Sensitivity setting......................................................18 5 Using the PIR detector..............................................19 6 Bill of materials.........................................................19 All schematics, board layout, and complete software pack are available on freescale.com. 7 References.................................................................20 Features include: • Design of low-cost PIR motion detector based on HC9RS08KA MCU © 2011 Freescale Semiconductor, Inc. Introduction • • • • • Implementation of software sigma-delta converter based on analog comparator Implementation of measurement of input user values Filtration method for signals from the PIR sensor Power switching by using two additional components: relay and triac Possibility of light dimming for the triac Capacitive power supply 12V DC 5V DC 230V AC APPLIANCE MC9RS08KA PIR sensor Microcontroller Switching relay Figure 1. Block schematic for all applications using relay output Capacitive power supply 12V DC 5V DC 230V AC APPLIANCE MC9RS08KA PIR sensor Microcontroller Triac switching Figure 2. Block schematic for all applications using triac output Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 2 Freescale Semiconductor, Inc. Sigma-delta conversion 2 Sigma-delta conversion The key point of this reference design is to demonstrate how to use the low-cost RS08KA microcontroller family in a PIR sensor application. The RS08KA MCU family does not include an internal A/D converter; therefore, measuring the signal from the PIR sensor is managed via a modified conversion based on an analog comparator and other passive components. This measuring method is detailed in the following part of this document. The basic principle is shown in Figure 3. Analog value High-pass filter Integrator Comparator Sigma-delta software Digital output Negative feedback Figure 3. Block digram of basic sigma-delta conversion 2.1 Sigma-delta conversion method One of the most important aspects of the sigma-delta technique is the ability to use it for converting an analog signal to a digital signal. Some high-end MCUs implement a hardware sigma-delta ADC peripheral. The RS08KA family of MCUs does not include an AD converter; therefore the converter is implemented via software and external components. The application discussed in this document uses an internal analog comparator with bandgap reference.This method of signal conversion uses a minimum of additional components. The most important feature of this conversion method is the ability to realize an application like this on low-cost microcontrollers without an analog-to-digital (A/D) conversion peripheral. The principle used in the detection method is based on a modified sigma-delta A/D converter. The accuracy of the conversion is dependent on a couple of factors: • Input leakage current of the analog sense pin • Fast charge and discharge of pin in its quiescent state Leakage current in the integration capacitor will also cause errors in accuracy due to asymmetric charge/discharge conditions. In this application, however, it is the difference in consecutive A/D values which will cause an event trigger. As a consequence, small changes in absolute accuracy will not affect the overall result, making such a method a good choice for this application. This conversion method is linear throughout the range of conversion. This method has a lower conversion speed but one that is still fully sufficient for a PIR application. 2.2 Implementation of sigma-delta conversion in PIR application Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 3 Sigma-delta conversion The input signal from the PIR sensor is capacitively coupled to the integration capacitor Ci (15 nF) by capacitor Cf (150 µF). The higher value of DC filtration capacitor Cf allows using lower values for the input and output resistors. This helps with higher noise resistance. Amplification is set depending on the PIR output voltage range. The PIR sensors have a very low voltage output (for example, about tens of microvolts); therefore amplification must be as high as possible. In this application an amplification of about 1400 is used. The most important element of the measuring process is that the charging and discharging of the integration capacitor are independent of output feedback from the analog comparator. The input voltage is compared on the analog comparator with the internal bandgap reference (1.2 V), and the result from the comparator is used as negative feedback to the negative input of this comparator. The output voltage from the analog comparator is sampled by two software counters in the MCU. One is used as a loop counter and the second counts the number of output results in logic 1. ref 1.2 V + - SW acomp -IN GPIO MCU Iin Rout Cf input signal OUT Iout Rin Ci 3.3V 0V t 1.2V 0V t Figure 4. Basic block scheme of sigma-delta conversion method If the value of the negative input to the comparator is lower than the bandgap value, the counter is decremented and the output value is set to a logic 1 (capacitor Ci is charged). In the second case, if the value is greater than the bandgap value, then the counter is incremented and the output value is set to logical 0 (capacitor Ci is discharged). By charging and discharging capacitor Ci, the voltage level is maintained at the bandgap level. User software running on the MCU processes the results from the comparator and returns a value representing the measured voltage. Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 4 Freescale Semiconductor, Inc. Hardware implementation The PIR application uses 15-bit resolution in this sigma-delta conversion method. This means that one voltage sample consists of 32768 logical states. A detailed schematic is shown in Figure 5. Figure 5. Complete schematic of sigma-delta converter 3 Hardware implementation The hardware implementation includes two versions of a PIR application with different power switching outputs and power supplies: • Relay power output version • Triac power output version Each of these hardware implementations consists of three individual parts: • Sigma-delta measurement • User input • Power switching (with power supply) All of these are described in detail in the following subsections. The final version of the board for both solutions is shown at the end. 3.1 Power supply and switching Capacitive power supply with relay switching Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 5 Hardware implementation For this PIR application, a capacitive transformerless power supply was chosen. The complete schematic is shown in Figure 6. This capacitive power supply uses a bridge rectifier with two Zener diodes. The Zener diodes in the bridge work as rectifier diodes and also stabilize the output voltage to 24 V. The application uses two voltages. First, 24 V is used for driving the relay output. The second voltage is a stabilized 5 V for the MCU supply. Positive voltage regulator 78L05 is used to reduce voltage ripple. Maximal output current is about 45 mA . Figure 6 also includes the switching part of the application. The first method of power switching uses a component relay. The relay is driven by the MCU via a bipolar transistor with protection diode D5. The switching section is designed for 24 V relay output. 470K 1 A C3 0.33uF-275V CON_2_TB J2 1 1 + C4 100uF BZV55 D3 2 R3 220 S1M-13-F D4 1 2W S1M-13-F VIN NC4 NC5 C1 0.1UF VOUT 1 MC78L05 2 K1 3 2 4 1 1 D5 LL4148 2 J4 1 4 5 1 J3 1 8 BZV55 2 2 B 1 D2 VCC5V U1 1 GND GND GND GND 470K J1 D1 2 2 3 6 7 R2 1 R1 1 OJE-SS J5 R4 3 1 1 1 POWER SUPPLY & SWITCHING PART 2 Q1 PW R_OUT 10K Figure 6. Capacitive power supply and relay switching schematic Capacitive power supply without rectifier bridge and with triac switching This variant of the power supply has lower power loading and is therefore targeted only for the triac version. This capacitive power supply uses one Zener diode for rectification and a second diode for creating virtual ground. Linear negative voltage regulator 79L05 is used to stabilize the output voltage, due to the polarity of the triac switching. For this reason the logic of the design must be reversed from 5 V to –5 V, compared to the relay design. All connections are shown in Figure 7. The schematic also includes power switching with a triac. The triac is connected to –5 V power supply and is driven by the MCU via resistor R15 (680 Ω). The main disadvantage of this low-cost power supply is in using the negative voltage regulator, because the price of the negative regulator compared to positive is much higher. Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 6 Freescale Semiconductor, Inc. Hardware implementation U2 A B 2 R14 330 J1 C10 0.33uF-275V 1 2 4 5 D2 BZV55-C 1 D1 + C9 470uF NC4 NC5 VOUT 1 GND GND GND GND R16 1M P5V VIN 2 3 6 7 1 8 MC79L05 2 LL4148 P5V CON_2_TB J2 1 1 LINE J3 1 1 1 J4 R15 680 PW R_OUT Q2 2 1 1 3 J5 1 1 POWER SUPPLY & TRIAC SWITCHING PART Figure 7. Schematic of capacitive power supply without rectifier bridge and with triac switching 3.2 User input measurement Input measurement is a time delay measurement taken from a capacitor and a trimmer, with an optional sensor for both input values. Setting and measuring brightness is implemented using a trimmer and an ambient light resistor with parallel connected resistor. The parallel connected resistor has the same value as the trimmer and therefore serves as the resistivity limiter of the ambient light resistor. Because of the low number of MCU pins, the value of the two variables must be combined into one. Because BKGD is used for code programing and GPIO, in the application jumper SH1 shown in the schematic serves as a separator before programming. Measurement of the time delay depends only on the value of the trimmer R7, which is set by the user. The complete schematic for measuring input values is shown in Figure 8.This section of the application is identical for both versions of the implementation. Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 7 Hardware implementation INPUT MEASURING OUT/BKGD 1 SH1 R5 1.0K 2 1 2 3 R6 47K R12 47K 2 R7 47K LIGHT_MEAS 1 3 R8 11-20K TIME_SET C10 0.015UF C9 0.015UF VCC5V V 1.7V 1.2V 3.3V log 1 log 0 0 T - measured time t Figure 8. Schematic of ambient light and time delay measuring 3.3 Sigma-delta measurement The PIR application uses one dual-element PIR sensor for motion detection. The complete schematic of the measurement section is shown in Figure 9. An LHI778 module from PerkinElmer was used for the PIR sensor. Two 100 nF capacitors are connected to the PIR sensor for noise filtration, and a load resistor (R11) is used to ensure the correct operation of the motion sensor. C6 and R9 function as a hi-pass filter to remove the DC signal. For the purposes of motion detection, it is sufficient to sense a signal of 2 Hz. The integration capacitor is charged and discharged via resistor R10 on output pin BKGD. The BKGD pin is also used for user input measuring of time delay and brightness via RC circuits. This measurement section is identical for the relay version and the triac version of PIR implementation. Measuring the low voltage signal from the PIR sensor is managed by one MCU peripheral analog comparator. Because of the MCU's low pin count, only the negative input of the comparator is used, and input voltage from the PIR sensor is compared with the internal bandgap reference. The sigma-delta conversion is described in detail in the "Sigma-delta conversion" section of this application note. Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 8 Freescale Semiconductor, Inc. Hardware implementation BDM1 1 3 5 VCC5V VCC5V 2 4 6 HDR 2X3 C5 0.1UF Q2 D 3 C8 0.1UF GND C6 150UF MCU CONNECTION R9 3.3K R10 4.7M LIGHT_MEAS TIME_SET OUT/BKGD RESET ACMP_IN PW R_OUT + S 2 1 R11 100K 5 6 2 1 7 8 U2 PTA5/KBIP5 VDD PTA4/KBIP4 PTA3/ACMPO/BKGD/MS PTA2/KBIP2/TCLK/RESET/VPP PTA1/KBIP1/ACMPPTA0/KBIP0/ACMP+ VSS VCC5V 3 4 C7 0.1UF MC9RS08KA1CSC 0.015UF C11 LHI778 Figure 9. Schematic of sigma-delta measurement circuit 3.4 Zero-cross detection and light dimming One of the advanced features of the triac design is the ability to use it for light dimming. This feature is included only in the version with triac power output. Light dimming means continuous regulation of light brightness when is the light turned on and also turned off. For light dimming it is important to have a circuit for measuring zero cross. This simple zero cross detector is created by two diodes and two resistors with one capacitor, as shown in Figure 10. Resistors R8 and R9 must have a package with voltage tolerance at 230 V (minimal recommended package is 1206). Light dimming has a positive impact on bulb life span. ZERO-CROSS DETECTION R8 P5V LINE 1 150K D3 LL4148 ZERO-CROSS 1 2 R9 150K D4 LL4148 2 C8 100PF Figure 10. Zero-cross detection circuit 3.5 Complete schematic The complete schematic of each application is shown here: with relay power output (Figure 11) and triac power output (Figure 12). Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 9 B C 2 1 1 1 1 1 5 R7 47K R6 47K R8 11-20K VCC5V C9 0.015UF 2 R5 1.0K R12 47K C10 0.015UF 2 0 SH1 S1M-13-F 2W TIME_SET LIGHT_MEAS OUT/BKGD 4 1 1 4 Q1 BC817-40LT1G OJE-SS 2 K1 3 1 + 10K R4 D5 LL4148 C4 100uF LHI778 Q2 GND S D 1 C8 0.1UF 3 2 VCC5V C6 150UF C5 0.1UF R11 100K R9 3.3K SIGMA-DELTA CONVERTER 2 BZV55 D3 2 POWER SUPPLY & SWITCHING PART 1 1 1 1 INPUT VALUES J5 J4 J3 J2 1 BZV55 S1M-13-F D4 1 1 2 D2 D1 R3 220 C3 0.33uF-275V 470K 470K 2 + Freescale Semiconductor, Inc. A B A CON_2_TB J1 R2 R1 4 1 2 D 1 3 1 2 3 1 3 C1 0.1UF 3 0.015UF C11 R10 4.7M PW R_OUT 3 U1 NC4 NC5 VIN 1 MC78L05 VOUT VCC5V 2 4 6 HDR 2X3 BDM1 LIGHT_MEAS TIME_SET OUT/BKGD RESET ACMP_IN PW R_OUT 1 3 5 5 6 2 1 7 8 VCC5V 2 MC9RS08KA1CSC 2 4 3 C7 0.1UF VCC5V Monday, January 03, 2011 Date: 1 Sheet 3 SCH-26843 PDF: SPF-26843 Document Number Size A3 Approved: Petr Gargulak Main schematic page Page Title: Drawn by: Pavel Krenek PIR reference design 1. maje 1009 765 61 Roznov p. R. Czech republic, Europe Freescale Semiconductor RCSC 1 of 3 Rev X1 This document contains information proprietary to Freescale Semiconductor and shall not be used for engineering design, procurement or manufacture in whole or in part without the express written permission of Freescale Semiconductor. ICAP Classification: FCP: ____ FIUO: X PUBI: ____ Drawing Title: Designer: Pavel Krenek PTA5/KBIP5 VDD PTA4/KBIP4 PTA3/ACMPO/BKGD/MS PTA2/KBIP2/TCLK/RESET/VPP PTA1/KBIP1/ACMPPTA0/KBIP0/ACMP+ VSS MCU CONNECTION U2 BDM CONNECTION 4 5 8 GND GND GND GND 2 3 6 7 10 2 5 A B C D Hardware implementation Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 1 1 1 1 1 1 1 1 Q2 5 P5V C5 0.015UF R3 47K 2 R1 1.0K R4 11-20K INPUT VALUES J5 J4 2 1 CON_2_TB B A R15 680 PW R_OUT LINE 1 LL4148 D1 D2 BZV55-C 2 + C9 470uF 4 5 U2 NC4 NC5 VIN 1 MC78L05 VOUT P5V C6 0.015UF LIGHT_MEAS TIME_MEAS R17 47K R2 47K 2 OUT/BKGD 4 LHI778 Q1 GND S D 1 C4 0.1UF 3 2 P5V C2 150UF C1 0.1UF R7 100K R6 3.3K SIGMA-DELTA CONVERTER POWER SUPPLY & TRIAC SWITCHING PART 0 SH1 3 P5V C10 0.33uF-275V (275VAC) R16 1M 1 2 8 GND GND GND GND 2 3 6 7 4 + A B C J3 J2 J1 R14 330 1 2 3 0.015UF C7 R5 4.7M 3 150K R8 C8 100PF R9 150K 2 4 6 HDR 2X3 BDM1 LIGHT_MEAS TIME_MEAS OUT/BKGD ZERO-CROSS ACMP_IN PW R_OUT 1 3 5 5 6 2 1 7 8 P5V D4 LL4148 ZERO-CROSS D3 LL4148 MC9RS08KA1CSC 2 4 3 P5V C3 0.1UF Freescale Semiconductor, Inc. Tuesday, January 11, 2011 Date: 1 Sheet 3 SCH-26843 PDF: SPF-26843 Document Number Size A3 Approved: Main schematic page Page Title: Drawn by: Pavel Krenek PIR reference design 1. maje 1009 765 61 Roznov p. R. Czech republic, Europe Freescale Semiconductor RCSC 1 of 4 Rev X1 This document contains information proprietary to Freescale Semiconductor and shall not be used for engineering design, procurement or manufacture in whole or in part without the express written permission of Freescale Semiconductor. ICAP Classification: FCP: ____ FIUO: X PUBI: ____ Drawing Title: Designer: Pavel Krenek PTA5/KBIP5 VDD PTA4/KBIP4 PTA3/ACMPO/BKGD/MS PTA2/KBIP2/TCLK/RESET/VPP PTA1/KBIP1/ACMPPTA0/KBIP0/ACMP+ VSS U1 P5V MCU CONNECTION BDM CONNECTION LINE 2 ZERO CROSS DETECTION 1 2 1 2 D 1 3 1 2 3 1 5 A B C D Hardware implementation Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 11 Software implementation 4 Software implementation This section describes all algorithms and programming techniques used in the PIR motion solution. The main part of the software is focused on measuring voltage from the PIR sensor, but also included are input measurement with filtration and the complete structure of application. The software is not platform-dependent. It can be used also on higher-end MCU platforms. The behavior of the main application is shown in Figure 13. Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 12 Freescale Semiconductor, Inc. Software implementation Main function Initialization: Clock, WDT, GPIO, RC measuring, PWR output PWR output ON (five second delay) Bandgap precharger for (;;) Single sample measurement Motion filtering Pir poll function Figure 13. Motion detection software flowchart Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 13 Software implementation 4.1 Precharging and discharging bandgap voltage After all peripherals are initialized, the relay is turned on for five seconds. Then the function to precharge the input filtration capacitor is called via the GPIO pin. Precharging of this capacitor allows speedup of sigma-delta converter initialization. The bandgap precharging procedure is shown in Figure 14. Bandgap Precharger Bandgap precharger Voltage measurement Sigma Delta deinitialization Voltage < 1.2 V ? YES Charging NO Discharging NO Sigma Delta initialization Voltage measurement Voltage ~ 1.2 ? End Figure 14. Bandgap precharging flowchart Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 14 Freescale Semiconductor, Inc. Software implementation The next code block shows the function for precharging of integration capactior to bandgap value. This function serves for faster charging and discharging of input DC capacitor via the GPIO pin. /**************************************************************************//*! * @brief function is used for pre-charging or pre-discharging of input * DC-filtration capacitor * ******************************************************************************/ void SWSD_BandgapPreCharger(void) { Byte cnt; cnt = 0; // capacitor is charged ~5 V if(SWSD_OneSampleMeasuring() == 0) { do {// discharging capacitor to voltage 1.2 V SWSD_ChargingTimeDelay(SWSD_DISCHARGE); }while((SWSD_OneSampleMeasuring() <= BANDGAP_SAMPLES) && (++cnt)); } // capacitor is discharge ~0 V else if(SWSD_OneSampleMeasuring() == BANDGAP_SAMPLES) { do {// charging to bandgap value 1.2 V SWSD_ChargingTimeDelay(SWSD_CHARGE); }while((SWSD_OneSampleMeasuring() >= BANDGAP_SAMPLES) && (++cnt)); } SWSD_Init(); // initialization of sigma-delta measuring } /**************************************************************************//*! * @brief 100ms delay with init and deinit of analog comparator * ******************************************************************************/ static void SWSD_ChargingTimeDelay(byte charge_state) { Byte ix; SWSD_DeInit(); ACMP_IN_DD = OUTPUT; ACMP_IN_D = charge_state; } // // // deinitialization of analog comparator direction to output CHARGE or DISCHARGE of input DC-filter capacitor for(ix=0; ix<SWSD_REPEAT; ix++) { asm nop; __RESET_WATCHDOG(); } SWSD_Init(); // initialization of sigma-delta converter ACMP_IN_DD = INPUT; // direction to input 4.2 Single-sample measurement The basis of sigma-delta measuring is a function performing single-sample measurement. This function returns a number of values of logical 1 in one cycle — this number depends on the resolution set in the header file. The next section shows the flowchart and code block for this function. Syntax: • Word SWSD_OneSampleMeasuring(void); Parameters: • None Return: Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 15 Software implementation • cnt - value of logical 1 in buffer Description: • This function include two counters and counts actual logical states depending on the resolution set in the header file. Single-sample measurement Single-sample measurement Set number of samples Decrement Max_samples Increment Counter0 Increment Counter1 YES Samples < Max_samples YES ACOMP OUT = 1 NO NO Return sample value Figure 15. Sigma delta single-sample measurement flowchart Following code block shows function for measuring of one sample. /**************************************************************************//*! * @brief Reading data from analog comparator * * @return number of log 1 in one sample (depend on the number of bits) ******************************************************************************/ Word SWSD_OneSampleMeasuring(void) { Word i; Word temp_cnt0, cnt; cnt = temp_cnt0 = 0; i = SWSD_SAMPLES; Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 16 Freescale Semiconductor, Inc. Software implementation } do // counting values of log 1 on the output of comparator { // comaparator output is in log 1 if(ACMP_INTERNAL_OUT) //---------------------------------------------< { // < ACMP_OUT_D = 1; // SECTION A < cnt++; // < } //---------------------------------------------< // comparator output is in log 0 else //---------------------------------------------< { // < ACMP_OUT_D = 0; // < temp_cnt0++; // SECTION B < asm nop; // < asm nop; // < asm nop; // < }; //---------------------------------------------< } while (--i); // number of all samples depend on the bit number return cnt; // return complete number of logical 1 in one sample //-----------------------------------------------------------------------------// For the correct measuring alghorithm of sigma delta must be time intervals // of both section (SECTION A and SECTION B) same. //------------------------------------------------------------------------------ 4.3 Filtration of signal from PIR Sensor Filtration of the signal from the PIR sensor is a necessary condition for the correct detection of movement. The most important feature of the filtration is the removal of spurious signals (peaks and noise). The first element of the motion filtration is the use of two four-byte buffers for continuously saving the data in a one-second time range. The next step creates moving averages by using the two sums of the values stored in in each buffer and calculating the difference between these sums. This calculated value is compared with the static threshold which corresponds to the maximum output noise from the PIR sensor. Complete source code of the filtration function is shown in the following code block. The system of motion filtration is displayed in detail in Figure 16. Syntax: • Word PIR_MotionFiltering(Word new_value); Parameters: • new_value Return: • diff - difference between two moving averages /**************************************************************************//*! * @brief Function for filtering of accidental peaks and noise. * The base is derivation of processed signal ******************************************************************************/ Word PIR_MotionFiltering(Word new_value) { // local variables static Word buff0[PIR_BUFF_SIZE_4B]; static Word buff1[PIR_BUFF_SIZE_4B]; static LWord sum0; static LWord sum1; static Byte ix; Word diff; Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 17 Software implementation // sum of first 4 samples sum0 += new_value; sum0 -= buff0[ix % PIR_BUFF_SIZE_4B]; // following sum of next 4 samples sum1 += buff0[ix % PIR_BUFF_SIZE_4B]; sum1 -= buff1[ix % PIR_BUFF_SIZE_4B]; buff1[ix % PIR_BUFF_SIZE_4B] = buff0[ix % PIR_BUFF_SIZE_4B]; buff0[ix % PIR_BUFF_SIZE_4B] = new_value; // differention between two following sums if(sum0 > sum1) diff = (Word)(sum0 - sum1); else diff = (Word)(sum1 - sum0); } ix++; return diff; 100ms 25ms 1 2 3 t buff1[4 Bytes] buff0[4 Bytes] 4 5 6 7 8 sum1 sum0 diff = abs(sum0 - sum1) Figure 16. Motion filtering system 4.4 Sensitivity setting The system can be made more or less sensitive by assigning larger or smaller values to the macro used to detect movement. The sensitivity can be changed in file "main.h" by increasing the macro MOTION_THRESHOLD. The default sensitivity setting in the software corresponds to the minimum threshold (maximum possible sensitivity). Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 18 Freescale Semiconductor, Inc. Using the PIR detector 5 Using the PIR detector This passive infrared solution is targeted only for indoor situations, especially indoor light switches. The main advantage of this design is the opportunity to make adjustments for the intended purpose, for example for use in burglar alarm systems, monitoring visitor presence, and light switches. Figure 17 shows the real detection range of the PIR detector. The next important aspect of the hardware design is the use of the correct type of Fresnel lens. For our purposes a Fresnel lens with two sensor elements was used. This design is ready for implementation of different Fresnel lenses for various application types (home lighting, garden lighting, etc.). pir detector 6m 8m Figure 17. PIR detector maximum range 6 Bill of materials Part reference Table 1. Bill of Materials Quantity Component symbol Description MC9RS08KA1CSC 1 U2 Microcontroller MC78L05 1 U1 Voltage regulator 100 nF 4 C1, C5, C7, C8 Ceramic capacitor 330 nF / 275 V 1 C3 Polypropylen capacitor 100 µF 1 C4 Electrolytic capacitor 150 µF 1 C6 Electrolytic capacitor 15 nF 3 C9, C10, C11 Ceramic capacitor Table continues on the next page... Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 19 References Table 1. Bill of Materials (continued) Part reference Quantity Component symbol Description BZV55 2 D1, D2 Zener diode S1M-13-F 2 D3, D4 Rectifier diode LL4148 1 D5 Small signal diode HDR 2x3 1 BDM1 Header BDM CON_2_TB 1 J1 Power connector Long male pin 4 J2, J3, J4, J5 Switching connector OJE-SS 1 K1 Relay BC817-40LT1G 1 Q1 NPN transistor LHI778 1 Q2 Passive infrared sensor 470k 2 R1, R2 Resistor 0.25 Ω 220 1 R3 Resistor 2 Ω 10k 1 R4 Resistor 0.25 Ω 1k 1 R5 Resistor 0.25 Ω 47k 2 R6, R7 Potentiometer 11-20k 1 R8 Light dependent resistor 3.3k 1 R9 Resistor 0.25 Ω 4.7M 1 R10 Resistor 0.25 Ω 100k 1 R11 Resistor 0.25 Ω 47k 1 R12 Resistor 0.25 Ω 0 1 SH1 Shortcut connector 7 References For more information, see the appropriate device reference manual and the documents in the following table. Table 2. References Document Title Freescale document DRM001 Passive Infrared (PIR) Intruder Detection Using the MC68HC908JK1/3 Freescale document MC9RS08KA2 MC9RS08KA2/MC9RS08KA1 Data Sheet (Also serves as reference manual) Freescale document AN3587 Cloth Iron Controlled by MC9RS08KA2 Wikipedia article on PIR (en.wikipedia.org/wiki/Passive_infrared_sensor) Passive infrared sensor Table continues on the next page... Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 20 Freescale Semiconductor, Inc. References Table 2. References (continued) Document PerkinElmer document LHI778 Title Pyroelectric Dual Element Detectors For Motion Sensing (http://www.perkinelmer.com/PDFs/Downloads/ DTS_LHi778_LHi878_PYD1388.pdf) Low-Cost Passive IR Sensing with RS08KA, Rev. 0, August 2011 Freescale Semiconductor, Inc. 21 How to Reach Us: Home Page: www.freescale.com Web Support: http://www.freescale.com/support USA/Europe or Locations Not Listed: Freescale Semiconductor Technical Information Center, EL516 2100 East Elliot Road Tempe, Arizona 85284 +1-800-521-6274 or +1-480-768-2130 www.freescale.com/support Europe, Middle East, and Africa: Freescale Halbleiter Deutschland GmbH Technical Information Center Schatzbogen 7 81829 Muenchen, Germany +44 1296 380 456 (English) +46 8 52200080 (English) +49 89 92103 559 (German) +33 1 69 35 48 48 (French) www.freescale.com/support Japan: Freescale Semiconductor Japan Ltd. Headquarters ARCO Tower 15F 1-8-1, Shimo-Meguro, Meguro-ku, Tokyo 153-0064 Japan 0120 191014 or +81 3 5437 9125 support.japan@freescale.com Asia/Pacific: Freescale Semiconductor China Ltd. Exchange Building 23F No. 118 Jianguo Road Chaoyang District Beijing 100022 China +86 10 5879 8000 support.asia@freescale.com For Literature Requests Only: Freescale Semiconductor Literature Distribution Center 1-800-441-2447 or +1-303-675-2140 Fax: +1-303-675-2150 LDCForFreescaleSemiconductor@hibbertgroup.com Document Number: AN4305 Rev. 0, August 2011 Information in this document is provided solely to enable system and software implementers to use Freescale Semiconductors products. There are no express or implied copyright licenses granted hereunder to design or fabricate any integrated circuits or integrated circuits based on the information in this document. Freescale Semiconductor reserves the right to make changes without further notice to any products herein. Freescale Semiconductor makes no warranty, representation, or guarantee regarding the suitability of its products for any particular purpose, nor does Freescale Semiconductor assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any liability, including without limitation consequential or incidental damages. "Typical" parameters that may be provided in Freescale Semiconductor data sheets and/or specifications can and do vary in different applications and actual performance may vary over time. All operating parameters, including "Typicals", must be validated for each customer application by customer's technical experts. Freescale Semiconductor does not convey any license under its patent rights nor the rights of others. Freescale Semiconductor products are not designed, intended, or authorized for use as components in systems intended for surgical implant into the body, or other applications intended to support or sustain life, or for any other application in which failure of the Freescale Semiconductor product could create a situation where personal injury or death may occur. Should Buyer purchase or use Freescale Semiconductor products for any such unintended or unauthorized application, Buyer shall indemnify Freescale Semiconductor and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claims alleges that Freescale Semiconductor was negligent regarding the design or manufacture of the part. RoHS-compliant and/or Pb-free versions of Freescale products have the functionality and electrical characteristics as their non-RoHS-complaint and/or non-Pb-free counterparts. For further information, see http://www.freescale.com or contact your Freescale sales representative. For information on Freescale's Environmental Products program, go to http://www.freescale.com/epp. Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2011 Freescale Semiconductor, Inc.