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

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 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. 

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

View solution in original post

0 Likes
34 Replies
Anonymous
Not applicable

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

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
Anonymous
Not applicable

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Some older application notes on the topic -

   

 

   

    

   

          

   

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

   

 

   

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

   

 

   

Regards, Dana.

Anonymous
Not applicable

 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.

0 Likes
Anonymous
Not applicable

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.    
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Anonymous
Not applicable

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 Sure, I have attached my whole project below.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 Sure, I have attached my whole project below.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Dana's point 3

   

Of course you may use registers to change component behaveour. I suggest to start doing that to optimize an already error-free running project. When something goes wrong it can turn out to be VERY difficult laying handsd on the error if there are multiple potential sources for misbehaveour.

   

 

   

To rule a project (in opposite to "The project rules me") I cling to the words "divide et impera"

   

Dividing the project into small parts, providing testcases for each of such a "module" together and finally integrate all the functionalities.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Note, you do some register writes that are redundant. That is when you set the

   

component proerties, they come into effect when program starts up and stay that

   

way until a reg write or applicable API is used. So if you set gain to 48, and never

   

change it, just set in properties and drop using equivalent reg write/API.

   

 

   

Note, there is one case where you would want to issue a no change write to a reg

   

or use of API, thats in high rel continuous computing type designs, where a dropped

   

bit is taken care of by fault tolerance design.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 I am really confused now, but I'll start all over.

1) First, I will create a project to read the output of an analog signal going into the ADC user module and display the digital value on the LCD.

   

2) Implement the analog output signal from the PIR sensor and run that signal into the ADC port on the psoc eval board. Display the digital value on the LCD.

   

3) Create a separate project to implement the timer interrupt and ISR for the LED.

   

Now how do I comebine this? Considering I have both the ADC and the Timer projects working, Do i just use the ADC project as the main project then implement the Timer LED projecting into the IF statement?

   

example:

if (ADC data is available)

   

{trigger the TIMER LED project

   

counter timer

   

check again to see if data is available

   

exit

   

timer count runs out

   

turn off LED

   

 

   

 

   

is this project simple like this?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

"

   

1) First, I will create a project to read the output of an analog signal going into the ADC user module and display the digital value on the LCD.

   

"

   

 

   

Let us start with that until it runs to your satisfaction.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Step 2 is easy, just remove the connection from the potentiometer you used to test point no. 1 and connect it to your PIR sensor. You may change the connection internally with a mux or change the wires. The former makes it easy to check in between (during integration phase) when something goes amiss.

   

 

   

"

   

3) Create a separate project to implement the timer interrupt and ISR for the LED.

   

"

   

 

   

No need to use a separate project. You may comment the initialization and the access to your ADC and use the same project. This will make the later integration much easier. Check and test the ISR-part and the associated LED. Then go to the next step in your list.

0 Likes
Anonymous
Not applicable

      http://pewa.panasonic.com/assets/pcsd/catalog/papirs-ekmc-catalog.pdf

   

Here's the data sheet for the PIR sensor

0 Likes
Anonymous
Not applicable

 Bob,  Here is what I put together so far. I am using port_0_1 for analog input from the PIR sensor.

   

I am not sure if my interconnect chip wiring is correct in the PSoC in the analog section

   

---I have Port 0_1 set for analog input that goes into the MUX_0. then from there, the ADC is taking the data passed through the PGA into the ADC. but from here it goes to a comparator, which i set to TRUE. I am stuck here, so i thought that there needs to be an output somewhere from the ADC connected to the port. I am lost here.

   

1) I need ADC output to go into a specific port.

   

2) The port then runs to LED and runs through the program to see if data is available.

   

3) If data is available, LED turns ON.

   

 

   

I have also attached my recent project. What I have here is the LED timer interrupt ( I Commented out the ADC project)

   

Port 0_1 is connected to an LED. LED initially turns ON when program initiated.

   

After loop runs out, timer interrupt stops the LED and turns OFF.

   

 

   

Now I want to Combine the ADC program and the LEDTimer program.

   

How do i do this? 

   

 

   

 

   

NOTE: The ADC and the LEDTimer programs use the same ports, so i should switch one of them right?

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Some thoughts -

   

 

   

1) Keep in mind you can open multiple Designer sessions. So open one

   

with A/D project, make that your master project, then open another with timer

   

example/whatever, and view the two screens side by side, cutting and pasting

   

between the two projects. Best done on dual monitor, or one hi res monitor

   

using windows shortcut keys to open 2 windows side by side. Select one window

   

on taskbar, then hold down ctrl key and click other window and select how you

   

want the two windows to appear.

   

 

   

2) The PIR has option of digital or analog out per below. If you are using A/D for PIR

   

it might make more sense to use just digital out and timer, saves code and resources

   

for other uses.

   

 

   

Anonymous
Not applicable

 So, I can get rid of the ADC and just keep the PGA user module and the Timer User module?

   

1) IF that is true, then I can send the output from the sensor as a digital signal, run it through Port 0_5.

   

2) If detect motion, execute LED timer program and TUrn on LED and loop counter

   

as simple as that?

0 Likes
Anonymous
Not applicable

 Is there anyway I can get a walk through? or chat via online to make this quicker? Thank you.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

If you look at the diagram for the digital PIR output case you would -

   

 

   

1) Delay on power up of PIR the "Circuit Stability Time"

   

2) Feed the sensor output to PGA then to a comparator, so you develop a logic

   

level signal to drive the enable of the timer. Route

   

3) Timer clock, since you are trying to detect relatively slow motion, use say 10 - 50 mS.

   

4) Width of timer has to reflect some minimal length in time of motion. Here I am guessing,

   

but lets use 50 mS clock, and detect up to 1 minute of motion, So Timer is 60 secs / .05 secs =

   

1200 ticks, or a 16 bit timer.

   

5) When comparator out goes high timer starts, and you use ISR on this edge. You can

   

use a pin to generate ISR off comparator out, or use internal buffer, place it, and enable

   

it as an ISR. Or use comparatopr ISR. Set a flag, exit. In main test flag, if true, tunr on led

   

or whatever you want for detection of motion. Could also test timer value that a minimum

   

amount of motion has been detected.

   

6) If timer rolls over,  60 secs (or whatever you decide) set its Tc out to interrupt and you have

   

a false motion ? A motion that lasts too long, not sure how you want to handle.

   

6) Using another placed buffer (or use comparator ISR) ISR when motion is lost (comparator

   

out falls) and use that ISR to set a flag, which in turn resets timer count (timer is now stopped

   

because enable is false, low from comparator).

   

 

   

Sort of an overview. I do not knbow what criteria you want to detect motion, and when motion

   

is considere illegitimate, you have to modify code to take care of that and above settings.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

In 3 above route Comparator out to timer enable.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 awesome. I got the project to work using the application notes you gave me.

   

How do I set a time limit for how long the LED turns on? say, 5 seconds.

   

1) PIR detects motion waving hand. LED turns on.

   

2) If motion detected again, reset timer to 5 seconds.

   

3) Else let timer run out to 5 seconds, turn OFF led.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When detecting a move, stop a 5 second timer, load the period register anew and start the timer again, set LED on. This will ensure a contineous signal as long as there is a movement seen.

   

When timer expires (interrupt-routine) stop timer and clear LED

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

As an aside, looking at digital out of PIR, looks like open drain PMOS to Vdd

   

rail, so you want a light pulldown to ground. Compute that off fact at Vdd - .5

   

it can source 100 uA max. Since it pulls to Vdd - .5, if you are running it at same

   

Vdd as PSOC, just interface that to enable of Timer. In other words eliminate PGA

   

and comparator, no need for them.

   

 

   

Also not knowing if that output is well conditioned, when motion results in noise

   

dominated output, you might want to debounce it like a mechanical contact. In

   

this case you might want to gate the enable to the timer with the debounce detect

   

routine you write.

   

 

   

Attached some debounce ref material.

   

 

   

Regards, Dana.

   

 

   

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

In fact if you look at notes below and use of one shot that indicates

   

PIR not well conditioned for noise, so I would advise using a debounce

   

routine.

   

 

   

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

One error in my posting, I forgot PSOC 1 Timers do not have HW enable.

   

So just use pin polling or isr to enable Timer via API write.

   

 

   

To effect it as a gated timer use your debounce code to qualify gate API

   

write.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Hey,,tanmanie

   

As per your last depicted post I think 5 sec of delay would be ok for detecting a particular move and I guess there would be a contineous signal as long as there is a movement seen and it would work like a motion sensor security lighting.

0 Likes