Friday, August 9, 2013

PIR Sensor IO

Datalogging the output from a PIR sensor. The output of the PIR is connected to an Arduino digital input. The PIR output is digital with no PWM for distance. On or Off is as good as it gets.
A separate +12V supply is connected to the PIR as the +5V from the Arduino is not enough for the PIR output to be stable.
PIR hooked into an Arduino via a Protoshield
Data saved to microSD card and charted in Excel

The Arduino code to monitor the PIR and fade in an LED if motion is detected.

int LED = 11;
int sensor = 7;
int n = 0;
int light = 0;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(sensor, INPUT);
  digitalWrite(LED, LOW);
  delay(1000);
}

void loop()
{
  int sens = digitalRead(sensor);
  if(sens == LOW)
  {
    for(n=0; n<255; n++)
    {
      analogWrite(LED, n);
      delay(10);
    }
    light = 1;
  }
  if (light == 1)
  {
    if (sens == HIGH)
    {
      for(n=255; n>-1; n--)
      {
        analogWrite(LED, n);
        delay(10);
      }
      light = 0;
    }
  }
}

No comments:

Post a Comment