Motion sensor with Alarm

Aim

To learn how to use a Proximity sensor to detect motion using Arduino UNO.

Components required for the experiment

Arduino UNO Board, PIR sensor and Buzzer.

What is a PIR sensor and where is it used?

PIR stands for "Passive Infrared". A PIR sensor is an electronic device that can detect motion by measuring changes in the infrared (heat) radiation within its range. It contains a pyroelectric sensor which generates an electrical signal when exposed to infrared radiation, and an electronic circuit that processes the signal and triggers an output when motion is detected.

PIR sensors are commonly used for security purposes in homes, offices, and other buildings. They are also used in automatic lighting systems, where the sensor detects motion turns on the lights, and then turns them off after a set period when no motion is detected. PIR sensors are also used in some outdoor lighting systems, where they can help conserve energy by turning on the lights only when motion is detected. Additionally, PIR sensors can be used in robotics, for obstacle detection, and in some industrial applications for monitoring equipment and processes.

 

 

 

Circuit Diagram

Make the circuit diagram with the exact pin-to-pin connection as shown below to practice. This will help you to complete the experiment successfully.

 

Code

const int PIR = 7;
const int buzzer = 13;

void setup(){
   pinMode(13, OUTPUT);
   pinMode(7, INPUT);
   Serial.begin(9600);
}

void loop(){
    if(digitalRead(7) == HIGH){
       Serial.println("Motion Detected");
       digitalWrite(13, HIGH);
       delay(2000);
   }else{
       Serial.println("No Motion Detected");
       digitalWrite(13, LOW);
   }
}

Code Explained

This code is written for a PIR (Passive Infrared) sensor and a buzzer. The PIR sensor is connected to pin 7 of the Arduino board and the buzzer is connected to pin 13.

Here is the line-by-line explanation of the code:

int PIR = 7;
int buzzer = 13;

These are the macro definitions to assign the PINs to variables for the PIR sensor and buzzer.

void setup(){
   pinMode(13, OUTPUT);
   pinMode(7, INPUT);
   Serial.begin(9600);
}

In the setup function, the pin modes of PIR sensor and buzzer are defined as input and output, respectively. We also initialise the serial communication at 9600 baud rate.

void loop(){
    if(digitalRead(7) == HIGH){
       Serial.println("Motion Detected");
       digitalWrite(13, HIGH);
       delay(2000);
   }else{
       Serial.println("No Motion Detected");
       digitalWrite(13, LOW);
   }
}

The If-Else condition checks whether the PIR sensor detected any motion. If PIR is HIGH, the buzzer is tuned ON indicating there is some motion and on the serial monitor, "Motion Detected" text is printed. Otherwise, the buzzer will be turned OFF to indicate the sensor did not detect any motion.  This cycle of checking if the PIR detects any motion happens after every 1 second.

Output and Serial Monitor

  1. Press the start simulation button in the interface.

  2. Position the mouse cursor over the PIR sensor.

  3. Click on the PIR sensor to enable its functionality.

  4. Once activated, a hand icon with a range will appear.

  5. Move the hand within the range to observe the simulator displaying "Motion Detected."

  6. If the hand moves beyond the range, the simulator will indicate "No motion detected."

  7. Whenever there is movement detected by the sensor within the range, the buzzer will produce a buzzing sound.

 

 

Conclusion

The alarm is activated whenever motion is detected.

Assignment

Change the code to reverse the condition. i.e. When there is no motion detected by the sensor, the buzzer should be turned ON and when motion is detected, the buzzer should turn OFF.