CODE: Arduino Controlled Motion Sensor

Arduino Controlled Motion Sensor:

Parallax Sensor Code:

/*

Arduino Controlled Passive Infrared Motion Sensor

LemonSlice7 – Instructables.com

*/

int calibrationTime = 30;

boolean sensorActive = false;

boolean previousSensorState = false;

int pirPin = 4;    //the digital pin connected to the PIR sensor’s output

int ledPin = 5;

 

// setup phase

void setup(){

Serial.begin(9600);

pinMode(pirPin, INPUT);

pinMode(ledPin, OUTPUT);

digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate

Serial.println(“Sensor Calibration in Progress”);

Serial.println(“———————————————”);

for(int i = 0; i < calibrationTime; i++){

Serial.print(“.”);

digitalWrite(ledPin, HIGH);

delay(250);

digitalWrite(ledPin, LOW);

delay(250);

digitalWrite(ledPin, HIGH);

delay(250);

digitalWrite(ledPin, LOW);

delay(250);

}

Serial.println(“”);

Serial.println(“Sensor Calibration Completed”);

Serial.println(“Sensor Reading Active”);

delay(50);

sensorActive = false;

previousSensorState = false;

}

// loop sequence

void loop()

{

// takes the pin value and saves it to the sensorActive boolean value

if(digitalRead(pirPin) == HIGH)

{

sensorActive = true;

digitalWrite(ledPin, HIGH);

}

if(digitalRead(pirPin) == LOW)

{

sensorActive = false;

digitalWrite(ledPin, LOW);

}

// performs action if the state of the sensor changes

// since this is a loop, here is now it works:

// if the sensor pin goes HIGH (on) after it being LOW (off), the sensorActive value changes from the previousSensorState value.

// it then turns on the LED. when the pin goes LOW (off) it will do the same thing but opposite values.

// it also prints status to serial. it will print the time of triggering by providing the number of seconds that have passed since the program started.

if(sensorActive != previousSensorState)

{

if(sensorActive == true)

{

previousSensorState = sensorActive;

Serial.println(“—-“);

Serial.print(“Motion Detected At: “);

Serial.print(millis()/1000);

Serial.println(” Seconds”);

delay(50);

}

if(sensorActive == false)

{

previousSensorState = sensorActive;

Serial.println(“—-“);

Serial.print(“Motion Stopped At: “);

Serial.print(millis()/1000);

Serial.println(” Seconds”);

delay(50);

}

}

}

 

PIR Sensor Code V2:

/*

Arduino Liquid Crystal Circuit

*LCD RS pin to pin

*LCD R/W pin to Ground

*LCD Enable pin to pin

*LCD D4 pin to pin

*LCD D5 pin to pin

*LCD D6 pin to pin

*LCD D7 pin to pin

*Backlight Circuit

[5v Supply -> 330Ω resistor -> Backlight Positive Pin]

[Backlight Ground Pin -> NPN Collector]

[NPN Emitter -> Ground]

[NPN Base to pin]

*10kΩ potentiometer wiper to VO pin 0 of LCD

LiquidCrystal lcd(RSpin, enablePin, D4pin, D5pin, D6pin, D7pin);

*/

//include the Arduino LiquidCrystal Library

#include <LiquidCrystal.h>

//initializes library with these interface pins

boolean sensorActive = false;

boolean previousSensorState = false;

int pirPin = 3;

int RSpin = 4;

int enablePin = 5;

int D4pin = 6;

int D5pin = 7;

int D6pin = 8;

int D7pin = 9;

int lightLCD = 10;

int serialVal1 = 0;

int unsigned long lastMillis = 0;

LiquidCrystal lcd(RSpin, enablePin, D4pin, D5pin, D6pin, D7pin);

void initializeLCD()

{

digitalWrite(lightLCD,HIGH);

lcd.setCursor(0, 0);

lcd.print(“  Arduino LCD Test  “);

lcd.setCursor(0, 2);

lcd.print(“   Please Wait…   “);

delay(1000);

cycleLCD();

lcd.clear();

}

void initializePIR()

{

lcd.setCursor(0,3);

lcd.setCursor(0, 0);

lcd.print(“Initializing”);

lcd.setCursor(0,1);

lcd.print(“PIR Sensor Module”);

lcd.setCursor(0,3);

for (int i=0; i < 20; i++)

{

lcd.write(0);

delay(1500); // default 1500ms

}

delay(1000);

lcd.clear();

}

void createCharLCD()

{

byte block[8] = {

B11111,

B11111,

B11111,

B11111,

B11111,

B11111,

B11111,

B11111,

};

lcd.createChar(0, block);

}

void lcdReset()

{

lcd.clear();

lcd.setCursor(0, 0);

lcd.print(“Awaiting Input      “);

}

void cycleLCD()

{

lcd.setCursor(0,0);

for (int i=0; i <= 80; i = i + 1)

{

lcd.write(0);

delay(10);

}

lcd.setCursor(0,0);

for (int i=0; i <= 80; i = i + 1)

{

lcd.print(” “);

delay(10);

}

}

void setup()

{

Serial.begin(9600);

pinMode(lightLCD, OUTPUT);

lcd.begin(20, 4); //setup LCD as a 20×4 layout

pinMode(pirPin, INPUT);

createCharLCD();

initializeLCD();

initializePIR();

lastMillis = millis();

}

void loop()

{

lcd.setCursor(0, 0);

lcd.print(“Arduino PIR Sensor”);

if(digitalRead(pirPin) == HIGH)

{

sensorActive = true;

}

if(digitalRead(pirPin) == LOW)

{

sensorActive = false;

}

// performs action if the state of the sensor changes

// since this is a loop, here is now it works:

// if the sensor pin goes HIGH (on) after it being LOW (off), the sensorActive value changes from the previousSensorState value.

// it then turns on the LED. when the pin goes LOW (off) it will do the same thing but opposite values.

// it also prints status to serial. it will print the time of triggering by providing the number of seconds that have passed since th

// program started.

if(sensorActive != previousSensorState)

{

if(sensorActive == true)

{

previousSensorState = sensorActive;

lcd.setCursor(0, 2);

lcd.print(“Motion Detected At: “);

lcd.setCursor(0, 3);

lcd.print(millis()/1000);

lcd.print(” Seconds”);

Serial.print(“Motion Detected At: “);

Serial.print(millis()/1000);

Serial.println(” Seconds”);

delay(50);

}

if(sensorActive == false)

{

previousSensorState = sensorActive;

lcd.setCursor(0, 2);

lcd.print(“Motion Stopped At:  “);

lcd.setCursor(0, 3);

lcd.print(millis()/1000);

lcd.print(” Seconds”);

Serial.print(“Motion Stopped At: “);

Serial.print(millis()/1000);

Serial.println(” Seconds”);

delay(50);

}

}

}

-END CODE-

Reference Source:

http://arduinotronics.blogspot.com/2013/01/motion-sensors-ssrs.html

http://www.instructables.com/id/DIY-Arduino-Motion-Sensor-Lighting-Control/

http://arduinotronics.blogspot.com/2012/03/light-sensing-with-cds-ldr.html
http://www.instructables.com/id/Arduino-Motion-Sensor-Control-Led-Light/?ALLSTEPS
http://www.instructables.com/id/Arduino-Controlled-Motion-Sensor/