Arduino Leonardo 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.

Hardware Required

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


The PIR Motion Sensor has three pins: VCC, OUTPUT, Ground.
Connect the Ground pin to the GND of board, the Vcc to pin 5V 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.




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()
  digitalWrite(led,LOW);//led off
void loop()
  pir= digitalRead(sensor);//Reads the Sensor
  if (pir==1){
     digitalWrite(led,HIGH);//led on
     digitalWrite(led,LOW);//led off

This is how the PIR Motion Example is shown on the new Arduino Studio. Check it out!