Thursday, July 12, 2012

Project: NiteLite (step 1)

So, finally something about this project!

The idea has been slightly changed since I installed a LED tape on my kitchen counter, but there are other places in the house where to use this project.

In order to start slow and getting things done, I decided to build an Arduino prototype first. Then I'll try bit by bit to alter the configuration to avoid using an IC (if possible).

This project is the use of a motion sensor and an LDR to light up some LEDs when I pass by the sensors at night.

I already wrote about the PIR Motion Sensor and the LDR on previous posts.
Now it's time to use them together.

The PIR Motion Sensor needs 6 to 12V to work correctly because it has an in-built 5V regulator. As I intend to use it with an Arduino or with an external regulator for the whole circuit, I decided to bypass that internal regulator.
I read on Sparkfun's forums that some people had no problems from doing that simple bypass, and it looks safe enough, so I managed to it with some clumsy soldering:


I connected the sensors like on the previous posts and added a couple of pots to control LDR sensitivity and on time for the LED. Looks a mess, but it's quite simple.


Here are the schematics for a better understanding:


And of course, all this needs some Arduino code:

void setup() {
  pinMode(2, INPUT);
  pinMode(5, OUTPUT);
  
  Serial.begin(9600);
}

//int sample = 0;

void loop() {
  // LDR value: the bigger the value, the darker the room
  // mapping the LDR input to correspond to a valid PWM value
  int m = map(analogRead(A1), 0, 1023, 0, 255);

  // PIR value: when motion is detected turns LOW, else HIGH
  int v = digitalRead(2);
  
  // Sensitivity pot
  int p = map(analogRead(A0), 0, 1023, 0, 255);
  
  // Time pot
  int t = map(analogRead(A2), 0, 1023, 1000, 10000);
  
  if( m > p && v == LOW )
  {
    digitalWrite(5, HIGH);
    delay(t);
  }
  else
    digitalWrite(5, LOW);

  // Writes to serial only 1 in 5 turns
  /*if(sample % 5 == 0)
  {  
    Serial.print("LDR: ");
    Serial.print(m);
    Serial.print(" Sense: ");
    Serial.print(p);
    Serial.print(" PIR: ");
    Serial.print(v);
    Serial.print(" Time: ");
    Serial.println(t);
  }*/
  
  delay(50);
  
  // Avoid overflow
  /*if(sample == 10000)
    sample = 0;
    
  sample++;*/
}

No comments:

Post a Comment