Go Down

Topic: Need code for PIR sensor to make a led blink 3 times  (Read 65 times) previous topic - next topic

Phelpster26

Can some one please help me I don't know much about coding I'm very new to it an I'm trying to make a PIR sensor blink a led 3 times when the motion sensor is triggered

JimboZA

Welcome to ArduinoLand....

Best tutorial I've seen for PIR is this one at adafruit. I think it's a good tutorial, because it covers the idea of detecting "new" motion as opposed to "existing"  motion.

You'll see that code turns on a LED whenever the PIR is active, ie whenever there is motion. But if you look closely you'll see it only prints the message "Motion detected" when the PIR has only just activated (as distinct from being still activated since the last time through loop()).

So have a look at that and think about where and how to do your blink.... hint: you want to blink on "new" motion not "existing" motion.
meArm build blog:     http://jimbozamearm.blogspot.com/

Please don't PM for technical advice. Firstly my advice is sometimes wrong, and second, in the forum you get a broader, more timely, range of responses.

Phelpster26

thank you very much that code was helpful and the hint as well this is what i got

    /*
     * PIR sensor tester
     */
     
    int ledPin = 13;                // choose the pin for the LED
    int inputPin = 2;               // choose the input pin (for PIR sensor)
    int pirState = LOW;             // we start, assuming no motion detected
    int val = 0;                    // variable for reading the pin status
     
    void setup() {
      pinMode(ledPin, OUTPUT);      // declare LED as output
      pinMode(inputPin, INPUT);     // declare sensor as input
     
      Serial.begin(9600);
    }
     
    void loop(){
      val = digitalRead(inputPin);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
        digitalWrite(ledPin, HIGH);  // turn LED ON
        delay(1000);
        digitalWrite(ledPin, LOW);
        delay(1000);
        if (pirState == LOW) {
          // we have just turned on
          Serial.println("Motion detected!");
          // We only want to print on the output change, not state
          pirState = HIGH;
        }
      } else {
        digitalWrite(ledPin, LOW); // turn LED OFF
        if (pirState == HIGH){
          // we have just turned of
          Serial.println("Motion ended!");
          // We only want to print on the output change, not state
          pirState = LOW;
        }
      }
    }


but i can only make it blink 4 to 6 time i thought putting   

digitalWrite(ledPin, HIGH);  // turn LED ON
        delay(1000);
        digitalWrite(ledPin, LOW);
        delay(1000);

3 time would work but didn't only putting it once made it blink close to what i wanted can i get another hint please or did i even get the first hint correct?

Phelpster26

wait i got the first hint but i can't figure out how to make the last blink not stay on so long meaning the first 2 are on off on off an the last if on for like 5 seconds then off

    /*
     * PIR sensor tester
     */
     
    int ledPin = 13;                // choose the pin for the LED
    int inputPin = 2;               // choose the input pin (for PIR sensor)
    int pirState = LOW;             // we start, assuming no motion detected
    int val = 0;                    // variable for reading the pin status
     
    void setup() {
      pinMode(ledPin, OUTPUT);      // declare LED as output
      pinMode(inputPin, INPUT);     // declare sensor as input
     
      Serial.begin(9600);
    }
     
    void loop(){
      val = digitalRead(inputPin);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
        digitalWrite(ledPin, HIGH);  // turn LED ON
       
     
        if (pirState == LOW) {
          // we have just turned on
          Serial.println("Motion detected!");
          // We only want to print on the output change, not state
          pirState = HIGH;
           digitalWrite(ledPin, HIGH); 
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
         digitalWrite(ledPin, HIGH); 
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
        }
      } else {
        digitalWrite(ledPin, LOW); // turn LED OFF
        if (pirState == HIGH){
          // we have just turned of
          Serial.println("Motion ended!");
          // We only want to print on the output change, not state
          pirState = LOW;
        }
      }
    }

JimboZA

Hey well done on getting that hint that you need to be a bit lower in the code where the "motion detected" message was.... that's exactly what I was getting at.

I think the new problem is that the existing code uses this line:

Code: [Select]
if (val == HIGH) {            // check if the input is HIGH
        digitalWrite(ledPin, HIGH);  // turn LED ON  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        if (pirState == LOW) {


.... to put the LED on as long as the PIR is active, so it stays on after you're done blinking. The PIR probably takes that 5s or so to reset wheb motion has stopped. Probably just zap that line out?

Couple of other things:
  • Have a look at for loops to see how to compress repeated lines
  • Look at BlinkWithoutDelay for future use
  • Select your code and hit the </> icon above the  :)  to put code in a box when you post.
meArm build blog:     http://jimbozamearm.blogspot.com/

Please don't PM for technical advice. Firstly my advice is sometimes wrong, and second, in the forum you get a broader, more timely, range of responses.

ScottC

Code: [Select]
/*
     * PIR sensor tester
     */
    
    int ledPin = 13;                // choose the pin for the LED
    int inputPin = 2;               // choose the input pin (for PIR sensor)
    int pirState = LOW;             // we start, assuming no motion detected
    int val = 0;                    // variable for reading the pin status
    
    void setup() {
      pinMode(ledPin, OUTPUT);      // declare LED as output
      pinMode(inputPin, INPUT);     // declare sensor as input
    
      Serial.begin(9600);
    }
    
    void loop(){
      val = digitalRead(inputPin);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
            //This line was deleted and moved further down--------------------****
      
        if (pirState == LOW) {
          // we have just turned on
          Serial.println("Motion detected!");
          // We only want to print on the output change, not state
          pirState = HIGH;
           digitalWrite(ledPin, HIGH);  
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
        digitalWrite(ledPin, HIGH);  
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
        digitalWrite(ledPin, HIGH);  
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
        }
      } else {
        digitalWrite(ledPin, LOW); // turn LED OFF
        if (pirState == HIGH){
          // we have just turned of
          Serial.println("Motion ended!");
          // We only want to print on the output change, not state
          pirState = LOW;
        }
      }
    }

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy