Detecting Motion-Integrating Passive Infrared Detectors
This code is similar to the pushbutton. That’s because the sensor acts like a switch when motion is detected. Different kinds of PIR sensors are available, and you should check the information for the one you have connected. Some sensors, such as the Parallax, have a jumper that determines how the output behaves when motion is detected. In one mode, the output remains HIGH while motion is detected, or it can be set so that the output goes HIGH briefly and then LOW when triggered. The example sketch in this recipe’s Solution will work in either mode. Other sensors may go LOW on detecting motion. If your sensor’s output
pin goes LOW when motion is detected, change the line that checks the input value so that the LED is turned on when LOW:
if (val == LOW) // motion when the input is LOW
PIR sensors come in a variety of styles and are sensitive over different distances and angles. Careful choice and positioning can make them respond to movement in part of a room, rather than all of it.Use a motion sensor such as a Passive Infrared (PIR) sensor to change values on a digital pin when someone moves nearby. Sensors such as the SparkFun PIR Motion Sensor (SEN-08630) and the Parallax PIR
Sensor (555-28027) can be easily connected to Arduino pins.
The following sketch will light the LED on Arduino pin 13 when the sensor detects motion:
/* PIR sketch a Passive Infrared motion sensor connected to pin 2 lights the LED on pin 13 */
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for the PIR sensor)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if motion detected
delay(500);
digitalWrite(ledPin, LOW); // turn LED off
}
}
This code is similar to the pushbutton. That’s because the sensor acts like a switch when motion is detected. Different kinds of PIR sensors are available, and you should check the information for the one you have connected. Some sensors, such as the Parallax, have a jumper that determines how the output behaves when motion is detected. In one mode, the output remains HIGH while motion is detected, or it can be set so that the output goes HIGH briefly and then LOW when triggered. The example sketch in this recipe’s Solution will work in either mode. Other sensors may go LOW on detecting motion. If your sensor’s output
pin goes LOW when motion is detected, change the line that checks the input value so that the LED is turned on when LOW:
if (val == LOW) // motion when the input is LOW
PIR sensors come in a variety of styles and are sensitive over different distances and angles. Careful choice and positioning can make them respond to movement in part of a room, rather than all of it.Use a motion sensor such as a Passive Infrared (PIR) sensor to change values on a digital pin when someone moves nearby. Sensors such as the SparkFun PIR Motion Sensor (SEN-08630) and the Parallax PIR
Sensor (555-28027) can be easily connected to Arduino pins.
The following sketch will light the LED on Arduino pin 13 when the sensor detects motion:
/* PIR sketch a Passive Infrared motion sensor connected to pin 2 lights the LED on pin 13 */
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for the PIR sensor)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if motion detected
delay(500);
digitalWrite(ledPin, LOW); // turn LED off
}
}
No comments:
Post a Comment