Loading...
 

Arduino M0 Pro PIR Motion Sensor Example

PIR Motion Sensor Example

PIR Motion is a sensor which allows you to detect moving objects in a room until to a distance of 7 meters and an angle of 110 Degree.
The sensor has a digital output that it is set to LOW, when it detects a movement the signal changes from LOW to HIGH.
Often there is one or plus potentiometer to adjust the sensibility of sensor.
In this example we will use the Pir Motion to command the switch on of a led when the sensor detects a movement.
Image
NOTE:
The Arduino board M0 Pro operates on 3.3V. The maximum voltage that the I/O pins can tolerate is 3.3V. The application of a voltage exceeding 3.3V may damage the board.

Hardware Required

  • Arduino Board
  • PIR Motion Sensor
  • Wires
  • Breadboard
  • 1 resistor of 220 Ω
  • 1 Led

Circuit

Image
The PIR Motion Sensor has three pins: VCC, OUTPUT, Ground.
Connect the Ground pin to the GND of board, the Vcc to pin 3.3V of the board and the OUTPUT to the digital pin 2 of board.
Also connect the anode of the LED (usually the longer pin) in series to a resistor of 220 Ω and connect it to pin 13 of board.
Finally connect the cathode of led to GND of board.

Code

Initially the led is switched off. If the sensor detects a movement, the led goes on and it will remain so for three second, after the led goes off.
Now we'll explain the sketch in detail.
In the first part of sketch we define the variables used.
In the setup function we define the led pin as output and we set it to LOW.
In the loop block we read the detected value from the sensor and it is placed in the pir variable, after we check this value if it is 1 we'll use the
digitalWrite to switch on the LED for three second after we will use again the digitalWrite to switch off it.

/*PIR Motion Example*/
int sensor=2;
int led=13; 
int pir;
void setup()
{
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);//led off
}
void loop()
{
  pir= digitalRead(sensor);//Reads the Sensor
  if (pir==1){
     digitalWrite(led,HIGH);//led on
     delay(3000);
     digitalWrite(led,LOW);//led off
    }
    delay(100);
}