How to use PIR sensor with Arduino for Object movement
#arduino
const int MOTION_SENSOR_PIN = 2; // Arduino pin connected to the OUTPUT pin of motion sensor
const int BUZZER_PIN = 13; // Arduino pin connected to Buzzer's pin
int motionStateCurrent = LOW; // current state of motion sensor's pin
int motionStatePrevious = LOW; // previous state of motion sensor's pin
void setup() {
Serial.begin(9600); // initialize serial
pinMode(MOTION_SENSOR_PIN, INPUT); // set Arduino pin to input mode
pinMode(BUZZER_PIN, OUTPUT); // set Arduino pin to output mode
}
void loop() {
motionStatePrevious = motionStateCurrent; // store old state
motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state
if (motionStatePrevious == LOW && motionStateCurrent == HIGH) {
// pin state change: LOW to HIGH
Serial.println("Motion detected!");
digitalWrite(BUZZER_PIN, HIGH); // turn on
}
else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) {
// pin state change: HIGH to LOW
Serial.println("Motion stopped!");
digitalWrite(BUZZER_PIN, LOW); // turn off
}
}
Watch video How to use PIR sensor with Arduino for Object Movement- online without registration, duration hours minute second in high quality. This video was added by user Preethi J 13 April 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 22 once and liked it lik people.