0% found this document useful (0 votes)
2K views1 page

HW 416

This code sets up an Arduino circuit to detect motion using a PIR sensor connected to pin 2. When motion is detected, it turns on a relay connected to pin 3 and prints "Motion detected!" to the serial monitor. When no motion is detected, it turns off the relay and prints "Motion ended!" to indicate the sensor is inactive again.

Uploaded by

Osama Yaseen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views1 page

HW 416

This code sets up an Arduino circuit to detect motion using a PIR sensor connected to pin 2. When motion is detected, it turns on a relay connected to pin 3 and prints "Motion detected!" to the serial monitor. When no motion is detected, it turns off the relay and prints "Motion ended!" to indicate the sensor is inactive again.

Uploaded by

Osama Yaseen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

int sensorPin = 2; // Set up a PIR sensor pin

int pirState = LOW;


int val = 0;
int relayPin = 3; //Set up a Relay pin

void setup() {
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
val = digitalRead(sensorPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(relayPin, HIGH); // turn Relay ON
delay(150);

if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(relayPin, LOW); // turn Relay OFF
delay(150);
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}

You might also like