Stairs LED Project

A few week ago i finished a project for my staircase.

A single simple ledstrip with a Arduino Nano with two PIR HC-SR501 sensors and LDR sensor. The LDR is not connected directly on the PIR itself but on the print board. That way i can choose the value for the light.

Everything is working fine but sometime when someone enters the stairs the ledstrip turn on but then stays on in stet of turning off after 30 seconds. I don't use the regulators on the PIR but in de Arduino code. This happens maybe once of twice a day. When it happens and the PIR detect something again the light will go off after the 30 seconds again and then working fine again.

I found some codes and created my own new code.

But my skills with Arduino a very limmited so hoping someone can check my code and know where is goes wrong sometimes or if there a errors in the code what create this fault.

I have the feeling it have something to do with the LDR. When i walk up the stairs it turns on but when i pass the second PIR the led is still on a few seconds later it turn off and on again. So it look like it see the movement, remember it and switch back on again. But if the LDR then have lower of higher value it sometime get lost, i think.

/*
 * PIR sensor fade effect
 */
int ledDelay = 30000;              //time to wait untill light switch off
int ledPin = 3;                   // choose the pin for the LED-Strip

int sensorPin = A0;               // select the input pin for ldr
int sensorValue = 0;              // variable to store the value coming from the sensor

int TOP_SENSOR = 2;               // the number of the PIR_Top pin
int BOTTOM_SENSOR = 4;            // the number of the PIR_Bottom pin
const int ACTIVE = HIGH;

int pirState = LOW;               // we start, assuming no motion detected
int topSensorState = 0;           // variable for reading the top PIR sensor
int bottomSensorState = 0;        // variable for reading the bottom PIR sensor
 
void setup() {
  pinMode(ledPin, OUTPUT);       // declare LED as output
  pinMode(TOP_SENSOR, INPUT);    // declare sensor as input
  pinMode(BOTTOM_SENSOR, INPUT); // declare sensor as input
  Serial.begin(9600);            //sets serial port for communication
    
}
 
void loop(){
   // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  
  topSensorState = digitalRead(TOP_SENSOR);  // read input value
  bottomSensorState = digitalRead(BOTTOM_SENSOR);  // read input value
   if ((sensorValue > 700 && topSensorState == ACTIVE) or (sensorValue > 700 && bottomSensorState == ACTIVE)) { // check if the input is HIGH
     for(int fadeValue = 0 ; fadeValue <= 30; fadeValue +=5) {    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);       
    // wait for 30 milliseconds to see the dimming effect   
    delay(60);  // turn LED ON
    }
    delay(ledDelay); //time to wait untill light switch off
    for(int fadeValue = 30 ; fadeValue >= 0; fadeValue -=5) {    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);       
    // wait for 30 milliseconds to see the dimming effect   
    delay(60);
    }   
    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 {
   
    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;
    }
  }
}

Hi,
Karma for using code tags.
OPs circuit;


How are you powering the project?
Is the LDR away from the LED strip?

You may need to put some Serial.print statements in your code to serially monitor your code.

I noticed you are looking for the PIR to be at a LOW,
May I suggest to look for the PIR going from HIGH to LOW, to trigger your LED part of the code.

Thanks.. Tom.. :slight_smile:

Your printing to the serial monitor is a bit weird. But can you show what the serial looks like when it does the "weird thing"?

And two things:

    for(int fadeValue = 30 ; fadeValue >= 0; fadeValue -=5) {    // sets the value (range from 0 to 255):
      analogWrite(ledPin, fadeValue);       
      // wait for 30 milliseconds to see the dimming effect   
      delay(60);
    }

a) There is one thing more terrible than no comments and that's wrong comments :wink: Aka, don't repeat yourself in comments because you WILL forget to change one of the two :wink:

b) It works out here but that will give you trouble if you don't start at a multiply of 5. For example, if you start at 29 it will do down to -1. analogWrite() has NO contrains on its input and just uses the last 8 bits. So -1 becomes 255. Like I said, works out here because you really end at 0 and you would have noticed a 255 of brightness :stuck_out_tongue:

Your code would not cause that issue.

Why is there a resistor between your “PWM” output pin 3 and the FET, What exact FET is it? and how are actually powering the Arduino and LED strip?

Thanks for all the replay's. I am a beginner so hope I understand everting.

TomGeorge:
Hi,
Karma for using code tags.
OPs circuit;


How are you powering the project?
Is the LDR away from the LED strip?

You may need to put some Serial.print statements in your code to serially monitor your code.

I noticed you are looking for the PIR to be at a LOW,
May I suggest to look for the PIR going from HIGH to LOW, to trigger your LED part of the code.

Thanks.. Tom.. :slight_smile:

Powering the project is done by a LED power 12v adapter. It gives power to the ledstrip and Arduino.
LDR is in the PIR box build in on top of the box. With wires it goes to the Arduino about 1 mtr further.

Checking PIR is something i will need to check how that works.

septillion:
Your printing to the serial monitor is a bit weird. But can you show what the serial looks like when it does the "weird thing"?

And two things:

    for(int fadeValue = 30 ; fadeValue >= 0; fadeValue -=5) {    // sets the value (range from 0 to 255):

analogWrite(ledPin, fadeValue);     
      // wait for 30 milliseconds to see the dimming effect 
      delay(60);
    }




a) There is one thing more terrible than no comments and that's wrong comments ;) Aka, don't repeat yourself in comments because you WILL forget to change one of the two ;)

b) It works out here but that will give you trouble if you don't start at a multiply of 5. For example, if you start at 29 it will do down to -1. analogWrite() has NO contrains on its input and just uses the last 8 bits. So -1 becomes 255. Like I said, works out here because you really end at 0 and you would have noticed a 255 of brightness :p

It's code i found and thought it is not a problem. Problem is that i can only see the output from the Serial monitor when my laptop is connected. Arduino is in a cabinet near the frontdoor and strais and now room for it. So i can't let the laptop connected a long time and i just happens random so it needs to stay for a long time. That's a problem.

Thanks for the explenation for analogWrite. So best way is to use 5/10/15 etc.

Slumpert:
Your code would not cause that issue.

Why is there a resistor between your “PWM” output pin 3 and the FET, What exact FET is it? and how are actually powering the Arduino and LED strip?

I used the project as a start: https://www.instructables.com/id/Motion-Activated-Automatic-LED-Stair-Lighting-With/
And only added a second PIR to it.

I wanted to use his code but it is missing a second PIR and no option to add a timer for the light. He use the timer on the PIR itself but i don't want that. I asked if he could help me to add a Timer and second PIR but that was difficult for him at the moment.

So i only uses his schematic and with some cute / paste i created a almost working code.

Most stair projects are with WS2812 of other digital led strips. But i don't need RGB or effect for the stairs. Just On/Off with fade effect is more than enough. So i was glad i found his project but sadly to difficult for my lake of skills with code.

mikeoo:
It's code i found and thought it is not a problem.

I don't see a problem right away, but like I said, it's structure is a bit weird. But, the moment you took the code and edited it, it became your code :wink:

mikeoo:
That's a problem.

Let me rephrase that. That's your problem. I don't see anything obvious so the most logical thing to do is at least use the debug info. You could try to make a simple test setup on your bench and start testing it there. But if it doesn't happen there then yeah, you have to debug the real thing. Which indeed can be a pain sometimes.

Another option would be to try and make new and more structured software which is easier to debug. May I suggest FadeLed for the output? And for example Bounce2 to make state change simple (yeah, no real debounce needed but it keeps it clean).

mikeoo:
Thanks for all the replay's. I am a beginner so hope I understand everting.

Powering the project is done by a LED power 12v adapter. It gives power to the ledstrip and Arduino.
LDR is in the PIR box build in on top of the box. With wires it goes to the Arduino about 1 mtr further.

Checking PIR is something i will need to check how that works.

It's code i found and thought it is not a problem. Problem is that i can only see the output from the Serial monitor when my laptop is connected. Arduino is in a cabinet near the frontdoor and strais and now room for it. So i can't let the laptop connected a long time and i just happens random so it needs to stay for a long time. That's a problem.

Thanks for the explenation for analogWrite. So best way is to use 5/10/15 etc.

I used the project as a start: https://www.instructables.com/id/Motion-Activated-Automatic-LED-Stair-Lighting-With/
And only added a second PIR to it.

I wanted to use his code but it is missing a second PIR and no option to add a timer for the light. He use the timer on the PIR itself but i don't want that. I asked if he could help me to add a Timer and second PIR but that was difficult for him at the moment.

So i only uses his schematic and with some cute / paste i created a almost working code.

Most stair projects are with WS2812 of other digital led strips. But i don't need RGB or effect for the stairs. Just On/Off with fade effect is more than enough. So i was glad i found his project but sadly to difficult for my lake of skills with code.

septillion:
I don't see a problem right away, but like I said, it's structure is a bit weird. But, the moment you took the code and edited it, it became your code :wink:
Let me rephrase that. That's your problem. I don't see anything obvious so the most logical thing to do is at least use the debug info. You could try to make a simple test setup on your bench and start testing it there. But if it doesn't happen there then yeah, you have to debug the real thing. Which indeed can be a pain sometimes.

Another option would be to try and make new and more structured software which is easier to debug. May I suggest FadeLed for the output? And for example Bounce2 to make state change simple (yeah, no real debounce needed but it keeps it clean).

Differently my problem and that is no problem, glad you helped.

FadeLed is a nice option i think so i will soon create the project on the bench and start trying to test and try to add FadeLed.

Let you know when i create and test with it.