Shop OBEX P1 Docs P2 Docs Learn Events
PIR Motion Sensor with Piezo Buzzer and LEDs — Parallax Forums

PIR Motion Sensor with Piezo Buzzer and LEDs

I am trying to program a PIR motion sensor that will sense motion and turn on a piezo buzzer and LED light that are all connected on a Parallax Propeller. The code is written in C and used on Simple IDE. The code works to where the motion is sensed and turns on the buzzer and LED but I can't turn it off unless I unplug the circuit. I was thinking of adding a push button that will turn it off and reset it to the previous state. Any advice?

Comments

  • The problem is there is no logic to turn things off. You get stuck in loops that don't re-read the PIR sensor so nothing can get turned off. Here is a rewrite of your code:
    /**
     * @brief Description of Project Here
     * @author 
     * @date Month Day, Year
     * @version 1.0
     * 
    */
    
    #include "simpletools.h"
    #include "stdbool.h"
    
    #define LED 2
    #define BUZZER 4
    #define PIR 5
    
    // ------ Global Variables and Objects ------
    int pir;
    bool volatile alert;
    
    // ------ Function Declarations ------
    void led2(void *);
    
    // ------ Main Program ------
    int main()
    {
    
      alert = false;
      cog_run(led2, 128);
    
      // Wait 20 to 30 seconds for  PIR to calibrate or"warm up"
      print("Wait about 5 seconds for PIR to calibrate...........");
      print("\r");
      pause(5000);
      term_cmd(CLS);
      // Once the PIR detects movement, it will send a HIGH or  a value of 1 out from its OUTPUT pin
      while (1)
      {
        pir = input(PIR);
        if (pir == 1)
        {
          alert = true;
          print("Intruder Alert!");
          print("\r");
        }
        else
        {
          if (alert)
          {
            print("All Clear!");
            print("\r");
            alert = false;
          }      
        }      
        pause(1000);
        term_cmd(CLS);
      }
    
    }
    
    // ------ Functions ------
    void led2(void *par)
    {
      while (1)
      {
        if (alert)
        {
          freqout(BUZZER, 500, 1000);
          high(LED);
          pause(500);
          low(LED);
        }
        else
        {
          freqout(BUZZER, 0, 1000);
        }      
        pause(500);
      }
    }
    

    It compiles but I don't have your setup created to try it out.

    Mike
  • Ok so the code works to where the pir sensor will sense movement and turn on led/buzzer. But I was trying to go for more of an alarm system type where if it senses something then it will keep the alarm on until an action is done to turn it off. So I do have a push button where if pressed it should reset it to the first state(nothing turned on). But the code I have for that isn't doing the trick. If you don't mind taking a look at it and tell me what I am missing? I appreciate it.
  • Uzi,

    Since the code that "iseries" provided was closer to what I think you want, I added the appropriate changes to it to implement a push-button to reset(turn-off) an active alarm. The code below compiles, but I haven't tested it on any hardware. Take a look at it and give it a try. Let us know how it goes...
    /**
     * @brief Description of Project Here
     * @author 
     * @date Month Day, Year
     * @version 1.0
     * 
    */
    
    #include "simpletools.h"
    #include "stdbool.h"
    
    #define LED 2
    #define BUZZER 4
    #define PIR 5
    #define PUSHBUTTON 8
    
    // ------ Global Variables and Objects ------
    int pir;
    bool volatile alert;
    bool volatile button_pressed;
    
    // ------ Function Declarations ------
    void led2(void *);
    
    // ------ Main Program ------
    int main()
    {
      alert = false;
      button_pressed = false;
      cog_run(led2, 128);
    
      // Wait 20 to 30 seconds for  PIR to calibrate or"warm up"
      print("Wait about 5 seconds for PIR to calibrate...........");
      print("\r");
      pause(5000);
      term_cmd(CLS);
      // Once the PIR detects movement, it will send a HIGH or  a value of 1 out from its OUTPUT pin
      while (true)
      {
        pir = input(PIR);
        if (pir == 1)
        {
          alert = true;
          print("Intruder Alert!");
          print("\r");
        }
        else
        {
          if (button_pressed)
          {
            print("All Clear!");
            print("\r");
            button_pressed = false;
          }      
        }      
        pause(1000);
        term_cmd(CLS);
      }
    }
    
    // ------ Functions ------
    void led2(void *par)
    {
      while (1)
      {
        if (alert) 
        {
          freqout(BUZZER, 500, 1000);
          high(LED);
          pause(500);
          low(LED);
          pause(500);
          if ( input(PUSHBUTTON) == 1 )
          {
            button_pressed = true;
            alert = false;
            freqout(BUZZER, 0, 1000);
          }
        }    
        else
        {
          freqout(BUZZER, 0, 1000);
          low(LED);
        }          
        pause(100);
      }
    }
    
  • Wow it worked! Thanks so much! I am new to using simple IDE and haven't heard of the stdbool.h but it does the trick.
  • One more thing. The piezo buzzer is set at frequency zero but that still has some noise. Like when it is at reset state it makes a clicking noise.
  • Ops my bad, I thought the freqout function continued to run in the background and it doesn't.

    Just remove the freqout(BUZZER, 0, 1000);

    Mike
  • uzi wrote: »
    Wow it worked! Thanks so much! I am new to using simple IDE and haven't heard of the stdbool.h but it does the trick.

    Glad to hear it worked...
Sign In or Register to comment.