Stairs ledstrip with pir sensors keeps running, please help

Hello everyone,

For a while i have been working on al ledstrip on my staircase with an arduino uno and 2 pir sensors (hc501).
However i am stuck and i am completely out of ideas on how to solve this problem.

I connected the ledstrip to the arduino (there is a resistor at the beginning of the strip)
The two sensors are connected the the 12v power supply and the arduino inputs.
The power supply ground is connected to the arduino ground.

This is the problem:
When i switch on the power the led flow nicely down the stairs, and after 10 second turn off.
But then turn on again, and off, and so on. Even when i remove the sensors from the arduino pins it just keeps going.

I tried several codes i found on the internet and they all do this. Today i bought a new arduino board because the only thing i could think of that it must be broken, but still the same thing happens. I must have done something wrong but cannot figure out what and its driving me crazy, i would really appreciate your help.

trap.ino (11.8 KB)

Please attach code using code tags. Many helpers will not fill up their computers with code from OPs and will not help You.
Attach a diagram as well.

i've been using this code. removed the funcion bit because it was to many words

// DIGITAL MOTION SENSING STAIR LIGHTS
// Imran O Mumtaz
// Summer 2013
// Sources: ADAFRUIT LIBRARY, ARDUINO EXAMPLE CODE, ARDUINO FORUMS + (USER: JamesHayek) for some of the cool effects!
// Nested If statement suggested by Jessica Milne (CS 375)


// This system utilizes an Atmel Atmega 328p MCU along with 2 PIR (Pyroelectric infrared) Sensors (HC-SR501) and a Digital RGB LED strip (WS2811).
// One sensor placed at the bottom of the stairs (or where ever) and one at the top. Each sensor triggers a different animation (walking up or walking down) along with a shut down warning animation.
// Just modify the function calls in the void loop() and the actual functins towards the bottom area to change the animations, color, etc. colorWipe for example. 
// The four parameters it takes are the RGB pixels followed by the speed at which you wish to run the animations. 0, 0, 0 would mean the LEDS are off and 255, 255, 255 means that they are all white/full brightness.




// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip = Adafruit_NeoPixel(380, 3, NEO_GRB + NEO_KHZ800);


int motionPin = 1;                                                                                 // PIR Input pin 8, UPSTAIRS
int motionPin2 = 7;                                                                                // PIR 2 Input pin 7, DOWNSTAIRS
int senseMotion = 0;                                                                               // variable to hold current state of motion detector
int senseMotion2 = 0;                                                                              // 2nd variable for 2nd sensor




void setup() {
  
  pinMode(motionPin, INPUT);                                                                      //PIRs declared as inputs
  pinMode(motionPin2, INPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  //rainbow (30000);
  //rainbowCycle (30000);
  //strip.show();
}

void loop() {
  
    senseMotion = digitalRead (motionPin);                                                        //set variables equal to what our sensors are reading (on or off)
    senseMotion2 = digitalRead (motionPin2);
    
      if (senseMotion == HIGH || senseMotion2 == HIGH) {                                          // Testing both sensors at same time, if high, go to apropriate line.
          
          if (senseMotion2 == HIGH) {                                                               // If Upstairs motion sensor is triggered...
          colorWipe (strip.Color (50, 50, 50), 1);                                             // White/blue color sweep going up stairs (Towards the arrow on the strip)
          strip.show();                                                                           // This being enabled or not, doesnt seem to make a difference. NVM it looks like its more responsive when trying to activate the PIRs one after another
          delay (10000);                                                                          // Wait so someone can get to the bottom or top of stairs, will remove this when 2nd sensor is implemented with an interrupt
          //colorChase (strip.Color(255, 25, 25), 5);                                             // Color chase animations, red, green, blue indicating that the light are about to shut down
          //colorChase2(strip.Color(25, 255, 25), 5);
          //colorChase (strip.Color(25, 25, 255), 5);
          //scanner(200, 0, 255, 10);
          colorWipe (strip.Color (0, 0, 0), 5);                                                   // This is needed for turn everything off, clear all pixels. More efficient with this here
          strip.show();
                                }                        
        
      if (senseMotion == HIGH) {                                                                 // If motion sensor 2 (Downstairs) if high go to next line
          colorWipe2 (strip.Color(50, 50, 50), 1);
          strip.show();
          delay(10000);
          //colorChase (strip.Color(255, 25, 25), 15);
          //colorChase2 (strip.Color(25, 255, 25), 5);
          //colorChase (strip.Color(25, 25, 255), 5);
          //scanner(200, 0, 255, 10);
          colorWipe2 (strip.Color(0, 0, 0), 5);
          strip.show();
                                 }  
                                                      }
      else {                                                                                    // no motion = no lights or animate til off
          digitalWrite (motionPin, LOW);                                                        //turning motion pins low, doesnt seem to make a difference either.
          digitalWrite (motionPin2, LOW);
          //colorWipe (strip.Color(0, 0, 0), 30);
          strip.show();
           }

            }


  
}

wiring diagram?

Use INPUT_PULLUP here (not INPUT):

pinMode(motionPin, INPUT); //PIRs declared as inputs
pinMode(motionPin2, INPUT);

this is wrong - remove it:

digitalWrite (motionPin, LOW); //turning motion pins low, doesnt seem to make a difference either.
digitalWrite (motionPin2, LOW);

I didn't read any further.

Its like this but i wired the sensors to the power supply + and -
Also there is a resistor at the beginning of the ledstrip

zwieblum:
Use INPUT_PULLUP here (not INPUT):

pinMode(motionPin, INPUT); //PIRs declared as inputs
pinMode(motionPin2, INPUT);

this is wrong - remove it:

digitalWrite (motionPin, LOW); //turning motion pins low, doesnt seem to make a difference either.
digitalWrite (motionPin2, LOW);

I didn't read any further.

I tried this but does not make a difference.

Even when i remove the wires from the sensors to the arduino it still keeps going like someone is there.

vincentvega_nl:
Its like this but…..

Draw the actual wiring using pen and paper showing pins, Vcc, GND etc. Those coloured paintings aren't precise enough.

For ex. I see no Vcc supplied to the controller.

your sensors are not connected to what your codes says it should be.

int motionPin = 1;
int motionPin2 = 7;

wolframore:
your sensors are not connected to what your codes says it should be.

int motionPin = 1;
int motionPin2 = 7;

Yes i changed it while trying to find the problem, I thought maybe the input was malfunctioning so I tried a different one.

then show the correct code and connections in your diagram

I tried...

This is how to insert images to your post.

Are you sure you're reading the PIR sensors correctly? If not, write a simple, small sketch that does just that. Print the result to the Serial interface (note: pins 0 and 1 are Serial, don't use them for your sensors or LEDs).

PIR sensors have a timeout; if your timeout is too long they may still be active when you think they should not be. There's usually a small pot on the sensor board to change this.

Thank you wvmarle, I haven't looked at the sensors. And haven't tried the pot to adjust the timeout.

However, when I power on the whole thing without the sensors connected the led should remain off right? Without pins 1 and 7 connected the strip still turns on and off and repeats that. Is that normal?

Your image shows you use USB power.

As said, pin 1 is part of the Serial interface. Any Serial.print() statements will make your sketch believe the sensor is triggered. Use another pin (obviously other than 0, 13, A6 or A7).

Alright i've changed it to pin number 5
Still the same thing unfortunately.

Yes, i use an old phone charger to power the arduino, is that okay?

My code is now this

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip = Adafruit_NeoPixel(380, 3, NEO_GRB + NEO_KHZ800);


int motionPin = 5;                                                                                 // PIR Input pin 5, UPSTAIRS
int motionPin2 = 7;                                                                                // PIR 2 Input pin 7, DOWNSTAIRS
int senseMotion = 0;                                                                               // variable to hold current state of motion detector
int senseMotion2 = 0;                                                                              // 2nd variable for 2nd sensor




void setup() {
  
  pinMode(motionPin, INPUT_PULLUP);                                                                      //PIRs declared as inputs
  pinMode(motionPin2, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  //rainbow (30000);
  //rainbowCycle (30000);
  //strip.show();
}

void loop() {
  
    senseMotion = digitalRead (motionPin);                                                        //set variables equal to what our sensors are reading (on or off)
    senseMotion2 = digitalRead (motionPin2);
    
      if (senseMotion == HIGH || senseMotion2 == HIGH) {                                          // Testing both sensors at same time, if high, go to apropriate line.
          
          if (senseMotion2 == HIGH) {                                                               // If Upstairs motion sensor is triggered...
          colorWipe (strip.Color (50, 50, 50), 1);                                             // White/blue color sweep going up stairs (Towards the arrow on the strip)
          strip.show();                                                                           // This being enabled or not, doesnt seem to make a difference. NVM it looks like its more responsive when trying to activate the PIRs one after another
          delay (10000);                                                                          // Wait so someone can get to the bottom or top of stairs, will remove this when 2nd sensor is implemented with an interrupt
          //colorChase (strip.Color(255, 25, 25), 5);                                             // Color chase animations, red, green, blue indicating that the light are about to shut down
          //colorChase2(strip.Color(25, 255, 25), 5);
          //colorChase (strip.Color(25, 25, 255), 5);
          //scanner(200, 0, 255, 10);
          colorWipe (strip.Color (0, 0, 0), 5);                                                   // This is needed for turn everything off, clear all pixels. More efficient with this here
          strip.show();
                                }                        
        
      if (senseMotion == HIGH) {                                                                 // If motion sensor 2 (Downstairs) if high go to next line
          colorWipe2 (strip.Color(50, 50, 50), 1);
          strip.show();
          delay(10000);
          //colorChase (strip.Color(255, 25, 25), 15);
          //colorChase2 (strip.Color(25, 255, 25), 5);
          //colorChase (strip.Color(25, 25, 255), 5);
          //scanner(200, 0, 255, 10);
          colorWipe2 (strip.Color(0, 0, 0), 5);
          strip.show();
                                 }  
                                                      }
      else {                                                                                    // no motion = no lights or animate til off
         
          //colorWipe (strip.Color(0, 0, 0), 30);
          strip.show();
           }

            }





//Function time!


// LED animation fuction, Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
                                              }
                                          }
                                                                                                                        
                                          
// LED animate the other direction                                         
                                         
void colorWipe2(uint32_t c, uint8_t wait) {
  for(uint16_t i=379; i<strip.numPixels(); i--) {                          //aantal leds invullen -1
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
                                              }
                                          }

Before posting code, please format it properly. The autoformat function of the IDE (press -T) can help you out. Makes it more readable.

Old phone charger: if that's 5V and connected to the 5V pin, that's OK. But your schematic shows USB power.

Are those sensors active high or active low?

This is what I see you have:

     if ( either PIR is HIGH )
      {
         // do a color thing with 10 sec delay in the middle

         if ( top of stairs PIR is HIGH) 
         {
           // do a color thing with 10 sec delay in the middle
         }                               }  
        else // the bottom of stairs PIR might be HIGH and still get here
        {                  
          //    show the leds as last set
        }

      }

Maybe this is more clear and definite?

byte motion, prevmotion;

................. and down in void loop()

    motion = digitalRead(motionPin);  
    motion += 2 * digitalRead (motionPin2);  // motion is both reads combined

    switch( motion )
    {
      case  0  :
      if ( motion != prevMotion )
      {
        // turn the lights off
      }
      break;

      case  1  :
      // turn the lights on, run colors one way
      break;

      case  2  :
      // turn the lights on, run colors the other way
      break;

      case  3  :  // because there could be more than 1 person triggering sensors
      // turn the lights on, run colors some other way
      break;
    }

    prevMotion = motion;

I use the phone charger and a USB cable to power the arduino, not to the pin but to the USB port on the board, is that wrong?

Sorry about the code, I don't use forums often so I'm learning.

Goforsmoke, I am new to the whole arduino code writing so I'm trying to learn. This code I found on the internet and has a lot of stuff i don't need but don't know how to rewrite, so I don't understand what you mean, sorry..
if you have any suggestions on changing something could you write what I have to put in the code, and what to remove?

Thank you for your help so far

The code you lifted doesn't really do what the comments say and it's not tight at all, it's more like English than Logic.

I don't know how to explain it to you. It's mushy logic and that makes it hard to follow.