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++;*/
}

Monday, July 9, 2012

LDR - Light sensor

For a coming project I'll need to know if a room has the lights on or off.
That's when a Light Dependent Resistor (LDR), or photocell comes in handy.

As usual, Ladyada provides an excellent tutorial on understanding and using electronics: Ladyada's 101 on LDR

I also found useful info here.

For the LDR that I have, I'm using this Datasheet. I'm not sure if this is the correct datasheet, but for this component I think it should be close enough.

The quantity of light can be measured in Lux, Lumen or Candela depending on several factors.
Here's some more info.

In this case, here's a table of some real world approximate Lux values, in order to understand the values we need to read:

Light source LUX
---------------------------
Moonlight 0.1
60W Bulb at 1m 50
1W Bulb at 0.1m 100
Fluorescent Lighting 500
Bright Sunlight 30,000

The resistance of a LDR can vary from +/-400 Ohm (for Bright Sunlight) to +/- 2 MOhm (in the Dark), depending also on the make of the component.

Here are some of the values I measured (from bright to dark):