Night Light Sensor widget

Over the last few months I have been putting some effort into prototyping my first printed circuit board. For me the process went better than I could have expected.

For me it really started when I built my first day/night light. I used a light dependant resistor to determine if it was dark and if it was to then turn on an LED light.

I then went on to add a movement sensor. From there I built a multi channel lighting controller. With very small thin low voltage wires running all over the place, hot glued in spots to the walls and floors. I can’t tell you how nice it is to have automated lighting. It seems like such a simple thing to go over and turn on a light switch, but believe me after having an automated system, If I go somewhere else to another house or hotel I realize how luxurious and satisfying it is not to have to turn the switches on and off in order as I move from room to room.

So what started out as a simple test of coding and sensors and logic, has seen me refine the design iteration by iteration. Not really in terms of capability but more in the way the parts come together and the way it is used. In terms of the hardware I have gone from breadboard, to prototyping board and soldered parts, to a fully custom printed circuit board (PCB). In the way of usability I have gone from very messy blobs of electronics laying on the floor, to an encased multi channel box, but with wires everywhere, then to a single board per light solution, with onboard adjustable dials.

After hearing and watching Dave Jones from the (http://www.eevblog.com) talk about PCB layout and the possibility of getting my boards made for under $20 for 10 delivered to my door, I really want to have a go at it. Now the first time I tried using the program without watching some tutorials I wasn’t really able to achieve much. But then I decided to use Youtube to my advantage. Yes its not just for cute cat videos. rpcelectronics have a seriously good quick set of videos to follow along to with your own free copy of eagle cad.

Itead Studios is where you can get your self some great 2 layer coloured PCB’s. Just send them your exported files from eagle cad and they will send you your boards in about 4 weeks or so.

So what’s next:-

A change to the on/off jumper to allow for AC.
A new revision of the board, possibly smaller with some smaller surface mounted parts.
Some changes to labelling.
A reset button.
ICSP (In Circuit Serial Programming).
Clean up the code and make possible improvements.

And of course I am posting my code. I am not a programmer by any means.

/*
Day night IR sensor shield 11/08/12 Trobb

DEBUG VERSION!!!!

*/

#include <avr/wdt.h>

#define ldr A0 // LDR light sensor
#define ir A1 // IR movement sensor pin
#define timerPot A2 // Pin the timer potentiometer is connected to

#define watchdogPin 3 // Uncomment this line to have a pin blink to let us know the code is running

#define led 3 // Output for LED

#define gain 10 // the amount of gain to add to the timerPot 10 is a normal value
int luxVal = 500; // Value to trigger the low light condition
float timerValue; // A variable to hold the value of the delay on time
float timerDelay;

 

void setup() {

// initialize the IO.

// pinMode(watchdogPin, OUTPUT); // Uncomment this line to have a different pin blink to let us know the code is running

pinMode(led, OUTPUT);
pinMode(ir, INPUT);
pinMode(ldr, INPUT);
digitalWrite(led, LOW); // start with LED off

Serial.begin(9600);

wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

// Uncomment this section to have a pin blink to let us know the code is running
// blink some morse code OK

digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(1500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(250);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);

 

Serial.println(“Setup complete”);
}

void loop() {

wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

// Lets print some debugging values to the console

Serial.print(“Current ldr reading is “);
Serial.println(analogRead(ldr));
Serial.print(“Current sensor value “);
Serial.println(digitalRead(ir));
Serial.print(“Current timer value is “);
Serial.println(analogRead(timerPot));

wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

 

// Now check for darkness or daylight and movement

// so long as its dark

while(analogRead(ldr)<=luxVal){

Serial.println(“Its dark”);

// and movement is detected
if(digitalRead(ir) == 1){
Serial.println(“Movement detected”);
digitalWrite(led, HIGH); // turn led on
Serial.println(“LED is ON”);

// now leave light on for a predetermined time

timerValue = analogRead(timerPot);
timerDelay = timerValue * (timerValue / gain); // times the value by itself divided by gain

Serial.println(timerDelay);

float i = timerDelay; // set timer variable
while(i >=0){
i –;
Serial.print(“Timing out “);
Serial.println(i);

wdt_reset(); //make sure we reset watchdog timer within the loop to prevent endless resetting
}
}

else{
Serial.println(“No movement”); // no movement detected so turn led off
digitalWrite(led, LOW); // turn led off
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}

}
// not dark enough yet

Serial.println(“still not dark enough”);

digitalWrite(led, LOW); // turn led off
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}

LED Night Sensor Widget Assembly Notes.

Toby Robb 2013

This board can be fed from either AC or DC 7-35 Volts. 7-9 volts DC is ideal as it generates less heat in the regulator.

With heatsinking the regulator can probably safely deliver up to 750mA.

The output at the terminals is 5 Volts. Calculate your LED load resistor accordingly.

With a 3 Watt led everything runs quite hot especially the load resistor, the regulator and the LED itself. So care must
be taken when siting the board to avoid anything catching fire. You have been warned!

A 1 Watt led is ideal for this board.

There are a couple of mistakes in the board at the moment.

The 2 Power capacitors are marked incorrectly on the board. They need to swap positions.
The larger electrolytic should be on the DC regulated side.

The LDR fixed resistor should be a solid link.

Choose your load resistor to suit your LED. Keep the current below 750mA, or even lower if you can.

The values printed on the board for the brightness and delay potentiometers are marked
incorrectly on the board. They need to swap positions.
Note: The jumper will probably not cut the power if your using AC in. If you are using DC
the screw on the left should be positive and the jumper will work.

Known bugs: If you face the light back to into the LDR you may get oscillations.
The light will switch on and off a few times as it aproaches darkness. This needs a proper
fix in software.

 

 Here below is a possible revision to the code I have been working on.
/*
Day night IR sensor shield 11/08/12 Trobb

Please enjoy my dodgy code!

I am not a programmer by any means!

Feel free to improve on it!

Check out http://www.tobyrobb.com

Written with Arduino IDE V.1.0.1

DEBUG VERSION!!!!

*/

#include <avr/wdt.h>

#define ldr A0 // LDR light sensor
#define ir A1 // IR movement sensor pin
#define timerPot A2 // Pin the timer potentiometer is connected to

#define watchdogPin 3 // Uncomment this line to have a pin blink to let us know the code is running

#define led 3 // Output for LED

#define gain 10 // the amount of gain to add to the timerPot 10 is a normal value
int luxVal = 150; // Value to trigger the low light condition
float timerValue; // A variable to hold the value of the delay on time
float timerDelay;
int dark = 0;
long previousMillis = 0; // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to check the LDR (milliseconds)

 

void setup() {

// initialize the IO.

// pinMode(watchdogPin, OUTPUT); // Uncomment this line to have a different pin blink to let us know the code is running

pinMode(led, OUTPUT);
pinMode(ir, INPUT);
pinMode(ldr, INPUT);
digitalWrite(led, LOW); // start with LED off

Serial.begin(9600);

wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

// Uncomment this section to have a pin blink to let us know the code is running
// blink some morse code OK

digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(1500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(250);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);

 

Serial.println(“Setup complete”);
}

void loop() {

wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

// Lets print some debugging values to the console

Serial.print(“Current ldr reading is “);
Serial.println(analogRead(ldr));
Serial.print(“Current sensor value “);
Serial.println(digitalRead(ir));
Serial.print(“Current timer value is “);
Serial.println(analogRead(timerPot));
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

// Now check for darkness or daylight and movement

checkLdr();

// so long as its dark

while(dark <=0){

Serial.println(“Its dark”);

// and movement is detected
if(digitalRead(ir) == 1){
Serial.println(“Movement detected”);
digitalWrite(led, HIGH); // turn led on
Serial.println(“LED is ON”);

// now leave light on for a predetermined time

timerValue = analogRead(timerPot);
timerDelay = timerValue * (timerValue / gain); // times the value by itself divided by gain

Serial.println(timerDelay);

float i = timerDelay; // set timer variable
while(i >=0){
i –;
Serial.print(“Timing out “);
Serial.println(i);
checkLdr();
if(dark<=0){
return;
}
wdt_reset(); //make sure we reset watchdog timer within the loop to prevent endless resetting
}
}

else{
Serial.println(“No movement”); // no movement detected so turn led off
digitalWrite(led, LOW); // turn led off
checkLdr();
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}

}
// not dark enough yet

Serial.println(“still not dark enough”);

digitalWrite(led, LOW); // turn led off
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}

 

void checkLdr(){

// check to see if it’s time to read the LDR;

// difference between the current time and last time

unsigned long currentMillis = millis();

if(currentMillis – previousMillis > interval) {
// save the last time you checked the LDR
previousMillis = currentMillis;

Serial.println(“Checking LDR”);

if(analogRead(ldr)>=luxVal){

dark++;

}
if(analogRead(ldr)<=luxVal){

dark–;

}
if(dark>=5){
dark = 5;

}
if(dark <=-5){
dark = -5;
}
}
Serial.print(“Current dark reading is “);
Serial.println(dark);
}

No Comments Yet.

Leave a comment