Go Down

Topic: Under The Bed lighting - need help with code and diagram (Read 283 times) previous topic - next topic

dducic

Hi all,

I was trying to create an LED under-the-bed lighting system for my parents. Here's the description:

"When someone puts their feet on the ground from the bed, it will activate a motion sensor that turns on the lights. I would like to have the lights stay on for at least 5 minutes. I would like to have this system connected to a power source and plugged in (not battery operated). Additionally, I would prefer to have LED strip lights on both sides of the bed (2 strips, I assume), as well as a motion sensor on each side of the bed (again, I assume this means 2 sensors) so that my Mom and Dad could activate their respective motion sensors when they put their feet on the ground."

I had a guy provide me schematics, a parts list, and the code, (all of which is attached) but I think there are a couple of glitches. First, the switch doesn't do what it's supposed to do (turn lights on when the IR sensor trips it, and turn lights off after 5 minutes), and I think there's something wrong with the wiring diagram close to the power source. The way it's wired, the LEDs never turn off, so I think they have to be wired at a different location than the schematic suggests. Either way, I'm hoping someone can help me understand where the glitches are and can help me out. I'd love to provide this gift to my elderly parents so they don't lose their balance when they get up in the middle of the night.

Many thanks!
~David

PaulRB

Most forum members generally read the forum on phones & tablets, so can't view a zip file. Plus there's the danger of viruses...

I will do you a favour and post your files according to forum guidelines.

Here's the code:
Code: [Select]

#define pirPin   2    // connect senosr 1 pin to arduino digital pin 2
#define pirPin2  3    // connect senosr 2 pin to arduino digital pin 3
#define relayPin  12  // attach relay pin to arduino digital pin 12

// variables..........
int timePeriod = 300;  // set light active time period in seconds.     it set 5 minute
int mode = 0;
//......................

// variable for motion detection.........................
int calibrationTime = 3;  
long unsigned int lowIn;
long unsigned int lowIn2;        
long unsigned int pause = 1000;  
boolean lockLow = true;
boolean lockLow2 = true;
boolean takeLowTime;
boolean takeLowTime2;  

long unsigned int activeTime = 0;
long unsigned int timer = 0;
//.......................................................


void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT_PULLUP);  // set pir pin 1 input
  pinMode(pirPin2, INPUT_PULLUP); // set pir pin 2 input
  pinMode(relayPin, OUTPUT);      // set relay pin output
  digitalWrite(relayPin,LOW);     // set relay pin Low (deactive)
  digitalWrite(pirPin, HIGH);    
  digitalWrite(pirPin2, HIGH);

  //give the sensor some time to calibrate...............
  Serial.print("calibrating sensors ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSORS ACTIVE");
//........................................................

// set pir pins to interrupt mode......
   attachInterrupt(digitalPinToInterrupt(pirPin), functionPir01, RISING);   // if pir sensor 1 go low, run "functionPir01"
   attachInterrupt(digitalPinToInterrupt(pirPin2), functionPir02, RISING);  // if pir sensor 2 go low, run "functionPir02"
  }


void functionPir01 () {
// if pir pin 1 go active.....
 if ( timer > 1000) {
       if(lockLow){  
          lockLow = false;          
         Serial.println("---");
         Serial.println("Sensor 01 motion detected at ");
         delay(100);
         if ( mode == 0 ) {
            digitalWrite(relayPin, HIGH); // set relay high. ( active )
            activeTime = timer;
            mode = 1;
          }
         delay(50);
         }        
         takeLowTime = true;
 }      
}

void functionPir02 () {
// if pir pin 1 go active.....
  if ( timer > 1000) {
       if(lockLow2){  
          lockLow2 = false;        
         Serial.println("---");
         Serial.println("Sensor 02 motion detected at ");
         delay(100);
         if ( mode == 0 ) {
            digitalWrite(relayPin, HIGH); // set relay high. ( active )
            activeTime = timer;
            mode = 1;
          }
         delay(50);
         }        
         takeLowTime2 = true;
  }    
}



void loop(){
  
  timer = millis(); // timer
// pir sensor function............................................
     if(digitalRead(pirPin) == HIGH){        
       if(takeLowTime){
        lowIn = millis();          
        takeLowTime = false;      
        }
      
       if(!lockLow && millis() - lowIn > pause){  
           lockLow = true;                        
           delay(50);
           }
       }

     if(digitalRead(pirPin2) == HIGH){              
       if(takeLowTime2){
        lowIn2 = millis();          
        takeLowTime2 = false;      
        }
       if(!lockLow2 && millis() - lowIn2 > pause){  
           lockLow2 = true;                        
           delay(50);
           }
       }  
//..................................................................


// for relay controller............................
       if ( mode == 1 ) {
          if ( timer > ( (timePeriod * 1000) + activeTime )) { // lights active time check
            digitalWrite(relayPin, LOW); //lights off
            mode = 0;
            Serial.println(" set led low ");
          }
       }
  }






And here are the diagrams:


And the parts list:

Quote
Parts list…………………….

1. Arduino Uno Board - 1
2. PIR Motion Sensors - 2
3.  Relay module - 1
4. Led Strip  - 2
5. Power Supply      - 1
6. Jumper wires
7. Power jack

and want wires,  soldering iron
Use diagram for connect devices.  


If you have any question problem contact me first.
Project is done give me a 5 stars best feedback.
Thank you.
Ardutech.

kenwood120s

I see that Ardutech (presumably the designer) asks that he be contacted first if it doesn't work: what did he say about your glitches?

I wouldn't have gone for an interrupt-driven solution: adafruit have a great tutorial on detecting motion with a pir.

PaulRB

My initial thoughts:

1. You paid actual money to some guy for this? Maybe I should change career!
2. No Arduino needed. The PIR sensors can trigger the relay themselves. You can adjust their sensitivity and the on-time with a small screwdriver. The Arduino is adding practically nothing to this circuit.
3. The code is amateurish.
4. The relay module has 2 relays but only one is used? Why? Could have switched the two strips separately. Or saved a little money by using a single relay module.

dducic

Appreciate the thoughts, PaulRB. I'm a woodworker and wanted to learn more about electronics, so I thought I'd get my feet wet with a simple project like this. Yes, I paid a couple of bucks but since I didn't know any better I figured I'd give it a go. Be gentle on a noob like me. :)

I'm a "learn by doing" kind of guy so if you have any suggestions about how to change the code and/or wiring schematics I'd very much appreciate it.

PaulRB

Is it one of your goals to start learning Arduino? If so, we need to find something for the Arduino to actually do. Right now, like I said, it has no purpose because you could connect the sensors to the relays and it would work just as well without the Arduino. If you want to get the project installed and working quickly, that's what I suggest.

So you could do that and then spend time making it better and including the Arduino.

You could, for example, get rid of the clicking relays, and have the led strips fade smoothly on and off over a couple of seconds, and just on the side of the bed where there is movement.

wvmarle

When reading it, I immediately thought about using capacitive sensors: a strip of aluminimum foil or so under the carpet, so the system reacts to someone touching the ground with their feet, not if you just hang your hand over the edge (which is what I often do). Also a bedsheet can't cover the PIR. It may even be possible to adjust sensitivity that it really needs a big foot on the ground before triggering.
You'd definitely need a microcontroller to do something like that.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

INTP

You don't need an Arduino.
There are PIR sensors that connect 12v wall wart to LED strip directly with adjustable on time. $2-3 bucks on ebay.

dducic

Ok, great - thanks to everyone for your input. So, being a noob, can someone provide some guidance as to *how* to do this? I could absolutely use some help understanding how to put this all together so it actually works...

wvmarle

Ok, great - thanks to everyone for your input. So, being a noob, can someone provide some guidance as to *how* to do this? I could absolutely use some help understanding how to put this all together so it actually works...
It's quite literally as described above - do some searches on Amazon or ebay or so.

Take 12V wall wart, connect on one end of PIR sensor, connect LED strip on the other side, and done. You may have to adjust the sensitivity a bit. That's it.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

INTP



If you can't figure out how to connect this, I think you should find a different hobby  :smiley-confuse:

dducic

Thank you.

INTP, I was actually asking about how to connect everything with the equipment I have, which is listed above, and that's a little more involved than just plugging one thing in another with the equipment you showed (which is different from mine). I was looking for opportunities to learn a little about electronics and how they are all wired together.

I think I have enough to make it all work - thanks everyone.

6v6gt


I'd simply say, if you have bought all this stuff already and have got to the point of testing it, then continue.
You have certainly got the basis of a solution. You will also have a learning experience.

I'd suggest starting with a very simple sketch to begin to understand the behaviour of the passive IR sensors you have purchased so you can set these optimally for your project. It has 3 controls ( a jumper and 2 potentiometers) and seems to take 1 minute to stabilise after power on.

Here is a detailed specification for the PIRs: https://www.upmakerspace.com/sites/default/files/datasheets/31227sc.pdf

Use the wiring diagram you have and simply omit the second PIR and LED.

Code: [Select]

#define pirPin   2    // connect sensor 1 pin to arduino digital pin 2
#define relayPin  12  // attach relay pin to arduino digital pin 12


void setup() {
  Serial.begin(9600);
  Serial.println("Starting...");
  pinMode(pirPin, INPUT_PULLUP);  // set pir pin 1 input
  pinMode(relayPin, OUTPUT);      // set relay pin output
}

void loop() {
  if ( digitalRead( pirPin == HIGH ) ) {
    digitalWrite( relayPin, HIGH ) ;
    Serial.println("Relay on");
  }
  else {
    digitalWrite( relayPin, LOW ) ;
    Serial.println("Relay off");
  }
  delay(1000) ; // 1 second. not nice but OK for a simple sketch.
}


Once you have got something working, you can start adding features eg the fading that has already been suggested, suppression of output during PIR stabilisation time, an LDR so that the light output can be supressed during daylight etc. etc. In parallel, you can start testing/using bits of the original sketch.

dducic

Thank you, 6v6gt - much appreciated. I will take your advice and give it a go. I'm a kinesthetic learner (learn by doing) so I will definitely follow your suggested approach.

Thanks for the encouragement!

Go Up