1 2 3 Previous Next 34 Replies Latest reply on Dec 13, 2013 11:59 PM by bale.jiskit

    PIR motion sensor to trigger LED timer countdown (timer User module)

    tanman.yan

       Hello there, I have a project where I want the PIR motion sensor to detect motion, take the output of the PIR motion sensor and send it into psoc, triggering LED to turn on, and then after turning on LED, a timer user module will countdown and reach a specified time, therefore turning off the LED. Help as soon as possible would be much appreciated! I have attached my "thought-to-work" PSoC 1 CY3210 kit  software/firmware below. 

        • 1. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
          tanman.yan

           Also, i want to be able to display the time (in seconds) or at least try to display it on the LCD. 

          • 3. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
            tanman.yan

             Please help anyone? There is an error when I combine the ADC user module output to trigger the LED and timer countdown.

            • 4. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
              user_1377889

              This is an easy one:

                 

              The modules are named LCD_1 and LED_1, so the APIs are named LED_1_On() and LED_1_Off. By the way: The polarity of the LEDs on the Evaluation Kit is "Active Low", not Active High (as far as I remember)

                 

              The same is for the LCD: The API is named LCD_1_Init().

                 

               

                 

              Bob

              • 5. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                user_14586677

                Some older application notes on the topic -

                   

                 

                   

                    

                   

                          

                   

                http://www.cypress.com/?rID=360    IP-360

                   

                 

                   

                http://www.cypress.com/?docID=20141    AN2414

                   

                 

                   

                Regards, Dana.

                1 of 1 people found this helpful
                • 6. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                  tanman.yan

                   Thank you! so I will just follow the application notes given. But it does not have any C coding to follow for the PSoC 1. Is there any way I can double check if I am on the right route with my already made PSoC Project? OR is it completely wrong? Your help would be much appreciated! Thank you.

                  • 7. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                    tanman.yan

                    Here's kind of the flow chart: (is this the correct procedure?)

                       

                    1) Initiate all User Modules and PIR sensor

                       
                          
                    • Analog-->PGA-->ADC-->LED-->LCD--> DAC
                    •     
                    • But how do I make the data that goes into the port for the ADC output on a different port to trigger the LED? I guess that is my biggest prolem here.
                    •    
                       

                    2) Software

                       
                          
                    • Check to see if data is available from PIR sensor
                    •    
                       
                          
                    1. #include <m8c.h> 
                    2.     
                    3. #include "PSoCAPI.h" 
                    4.     
                    5. #include "stdlib.h"
                    6.     
                    7. #include "string.h"
                    8.     
                    9.  
                    10.     
                    11.  
                    12.     
                    13. //~~~~~ global variables ~~~~~
                    14.     
                    15. int stop = 0; // flag for spin loop
                    16.     
                    17. int tick = 0; // variable to keep track of # of times terminal count
                    18.     
                    19. // is reached on the LEDTimer8 UM
                    20.     
                    21. int MAXTICK = 100; // number of count cycles for 10 seconds
                    22.     
                    23.  
                    24.     
                    25. void main(void) 
                    26.     
                    27.     
                    28.  unsigned char bADCData; 
                    29.     
                    30.  
                    31.     
                    32.  /* Enable Interrupt for ADC */ 
                    33.     
                    34.  M8C_EnableGInt ; 
                    35.     
                    36.  
                    37.     
                    38.  /* Initialize all User Modules */ 
                    39.     
                    40.  LCD_1_Start(); 
                    41.     
                    42.  PGA_1_Start(PGA_1_HIGHPOWER); 
                    43.     
                    44.  ADCINC_1_Start(ADCINC_1_HIGHPOWER); 
                    45.     
                    46.  
                    47.     
                    48.  /* Run ADC continuously */ 
                    49.     
                    50.  ADCINC_1_GetSamples(0); 
                    51.     
                    52.  
                    53.     
                    54.  while(1) /* Loop forever */ 
                    55.     
                    56.  { 
                    57.     
                    58.  if(ADCINC_1_fIsDataAvailable() != 0) 
                    59.     
                    60.  /* If data is valid display on LCD */ 
                    61.     
                    62.  { 
                    63.     
                    64.  /* Read ADC Result */ 
                    65.     
                    66.  bADCData = ADCINC_1_bGetData(); 
                    67.     
                    68.  led();
                    69.     
                    70.  /* Display on LCD */ 
                    71.     
                    72.  LCD_1_Position(1,5); 
                    73.     
                    74.  LCD_1_PrHexByte(bADCData); 
                    75.     
                    76.  
                    77.     
                    78.  /* Clear ADC flag for next reading */ 
                    79.     
                    80.  ADCINC_1_fClearFlag(); 
                    81.     
                    82.  } 
                    83.     
                    84. }
                    85.     
                    86. }
                    87.     
                    88.  
                    89.     
                    90.  
                    91.     
                    92. void led()
                    93.     
                    94. {
                    95.     
                    96. char ASCII_String[7]; // for itoa
                    97.     
                    98. int i = 0; // count index for spin loop
                    99.     
                    100.     
                    101. InitLCD();
                    102.     
                    103. LEDTimer8_Start();
                    104.     
                    105.  
                    106.     
                    107. // M8C_EnableGInt ; // enable Global Interrupts
                    108.     
                    109. LEDTimer8_EnableInt(); // enable Timer8 interrupts
                    110.     
                    111.     
                    112. while (!stop)
                    113.     
                    114. {
                    115.     
                    116. PRT0DR |= 0x02; // turn ON LED at P0_1
                    117.     
                    118. LCD_1_Position(0,6);
                    119.     
                    120. LCD_1_PrString(itoa(ASCII_String,i/0x1CA,10));
                    121.     
                    122. i++;
                    123.     
                    124. } // end spin loop
                    125.     
                    126.     
                    127. // concluding code - will not run until max time has been reached 
                    128.     
                    129. PRT0DR &= ~0x02; // Turn OFF LED at P0_1
                    130.     
                    131. LCD_1_Position(0,0);
                    132.     
                    133. LCD_1_PrCString("Max Time Reached");
                    134.     
                    135. LCD_1_Position(1,0);
                    136.     
                    137. LCD_1_PrCString("Lights Turn OFF");
                    138.     
                    139. }
                    140.     
                    141.  
                    142.     
                    143. void Timer_Interrupt(void ) //Timer ISR
                    144.     
                    145. {
                    146.     
                    147. tick++;
                    148.     
                    149.     
                    150. if (tick == MAXTICK)
                    151.     
                    152. { LEDTimer8_Stop();
                    153.     
                    154. stop = 1; // change spin loop flag
                    155.     
                    156. }
                    157.     
                    158.     
                    159. } // end Timer_Interrupt
                    160.     
                    161.  
                    162.     
                    163. void InitLCD(void )
                    164.     
                    165. { LCD_1_Start();
                    166.     
                    167. LCD_1_Position(0,0);
                    168.     
                    169. LCD_1_PrCString("Time:"); 
                    170.     
                    171. LCD_1_Position(0,9);
                    172.     
                    173. LCD_1_PrCString("seconds");
                    174.     
                    175. }// end InitLCD
                    176.    
                    • 8. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                      user_1377889

                      You are setting Port0 pin 1, but your LED is connected to Port0 pin 2. You still do not use the LED_1_xxx() APIs.

                         

                       

                         

                      Bob

                      • 9. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                        tanman.yan

                        How about we try this, I used the old PSoC sample code that was provided above in the application note. Which ports and pins do I plug in for the PIR and then which port_pin do I use for input to the LED? Let's try if anyone of you are able to help. So far all i have is port0_pin7 serving as the input for from the PIR output pin.

                        • 10. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                          user_1377889

                          In your uploaded project you are using Port0 pin 1 (not seven) as input to the PGA.

                             

                          Can you please upload a current project state for us to check?

                             

                           

                             

                          Bob

                          • 11. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                            tanman.yan

                             Sure, I have attached my whole project below.

                            • 12. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                              tanman.yan

                               Sure, I have attached my whole project below.

                              • 13. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                                user_1377889

                                I cannot see where you

                                   

                                -Have an input mux and set that to connect your PGAs to your desired inputs

                                   

                                -Have an LED-component to safely(!!!) switch your LED on and off (You are using a read-modify-write when accessing the port directly)

                                   

                                 

                                   

                                Some useful hints:

                                   

                                Port0 is best for analog I/O, use port1 (or two) for digital I/O. Leave port2 free for using the LCD on the development kit

                                   

                                Use usermodules when they are availlabe for your purpose, do not "create" your own muxes, LED-usermodules can be used for other purposes as well, for instance to switch your relay-driver

                                   

                                While designing: Readability rules effectiveness, use bit setting/testing sparingly better use boolean TRUE or FALSE at the cost of additional 7 bits.

                                   

                                Avoid re-configuring usermodules by changing register values if not ABSOLUTELY necessary, any hardware design change may spoil that!

                                   

                                Your included .h-files are not listed in the workspace explorer. Left-click on Source or Header Files and add the file(s)

                                   

                                 

                                   

                                You do not exactly tell, what is working and what does not work. Getting some AD values and Switching some port bits are easy stuff but your project is a bit more complex. I would suggest you to make an exxample project for getting the ADC-values and display on LCD. This might help you to control the PIR values.

                                   

                                 

                                   

                                Bob

                                • 14. Re: PIR motion sensor to trigger LED timer countdown (timer User module)
                                  user_14586677

                                  Some observations –

                                     

                                   

                                     

                                  1)      1) The LEDs on the kit are active high to turn on. As Bob suggested use LED

                                     

                                  components to control LEDs, they make life easy, instantiate shadow registers,

                                     

                                  and have very little code/ram burden.

                                     

                                   

                                     

                                  2)      2) I see your PGA, GainStage1, GainStage2, are respectively connected to

                                     

                                  P0_1 and P0_4. P0_1 is not set to Analog Input, which it should be. Po_4

                                     

                                  is connected to AnalogOutBuff2, the DAC, is that what you wanted ?

                                     

                                   

                                     

                                  3)      3) You are fine on doing register writes to configure and control PSOC1, but

                                     

                                  you have to read carefully the register TRM to make sure you understand

                                     

                                  all side effects and constraints when you do. That is a strength of the architecture,

                                     

                                  the ability to exercise great control over the HW.

                                     

                                   

                                     

                                  4)      4) A link to the PIR datasheet would be helpful.

                                     

                                   

                                     

                                  5)      5) It would help if you could post an electrical schematic, not absolutely necessary

                                     

                                  but helpful.

                                     

                                   

                                     

                                  6)      6) I do not see where the timer ISR code is, or that you have used a C ISR to

                                     

                                  handle it (whiuch would require you mod the boot.tpl file or the tool generated

                                     

                                  ISR placeholder for code) ?

                                     

                                   

                                     

                                  Regards, Dana.

                                  1 2 3 Previous Next