Sunday, January 13, 2013

Motion Sensors & SSR's

A Motion Sensor is a good way to save energy, by turning lights on and off based on the presence of a warm body.  Walk into a room, the lights come on, walk out, and they go off. We can build in a delay on the off cycle, and a manual override with a switch giving us motion - off - on modes.

Now also on Instructables!

First we start with the PIR Motion Sensor. This connects to a digital input, which we monitor for the presence of a warm body.


int ledPin = 13;
int switchPin = 2;
int value = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(ledPin, LOW);
}

void loop() {
value = digitalRead(switchPin);
if (HIGH == value) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
} }

This turns on the LED on the Arduino when motion is detected. Now let's add the SSR. This allows us to control 120vac devices with no chance of damaging the Arduino (optically isolated) and reduced chance of getting zapped (not much harder than wiring a light switch). I suggest starting with a corded desk or floor lamp.



IMPORTANT!

Unplug from wall before doing this step!

Separate the two wires going to the lamp, and only cut one of them (preferably the HOT, not the Neutral (wide blade), if your plug is polarized - Larry).
Mike recommends you cut open a short extension cord instead of your lamp cord, so that the project is "portable".

Strip the insulation from the two ends you have from the one line, and insert them under the SSR screws labeled 1 & 2.
Connect screw 4 on the SSR to ground on the Arduino, and screw 3 to pin 13 on the Arduino.
Plug the lamp back into the wall, and power up the Arduino.

Your lamp should light when motion is detected, and go out after a short period of time after you leave the focus area.

Now lets add a manual override switch. You will want a SPDT switch with center off. You will be connecting the center pin to +5vdc on the arduino. The outer two pins will connect to Arduino pins 11 & 12, with a 10k resistor to ground on each.

Now our sketch looks like this:


int inPin1 = 11;   // switch connected to digital pin 11
int inPin2 = 12;   // switch connected to digital pin 12

int ssrPin = 13;
int pirPin = 2;
int motionDetect= 0;
int manualSwitch = 0;
int motionSwitch = 0;

void setup() {
pinMode(ssrPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
digitalWrite(ssrPin, LOW);
}

void loop() {

motionSwitch = digitalRead (inPin1);
manualSwitch = digitalRead (inPin2);

if (motionSwitch == HIGH) // Motion Mode
{
  motionDetect = digitalRead(pirPin);
  if (motionDetect == HIGH) {
     digitalWrite(ssrPin, HIGH);
     delay (180000); //Optional 3 minute delayed off

}
else if (manualSwitch == HIGH) // Manual On
{
  digitalWrite(ssrPin, HIGH)
}
else // Manual Off

{
  digitalWrite(ssrPin, LOW)
}
}