Arduino PIR Motion Sensor

arduino pir motion sensor

An Arduino PIR motion sensor needs power, ground and one digital signal wire. Wire it, wait out the warm-up, tune the sensitivity, delay and jumper on the board, then drive a buzzer from the output to make an alarm.

This build is one of the arduino robotics projects on the site. It suits beginners, because the HC-SR501 module does the signal processing and your sketch only reads a pin.

Parts and prerequisites

  • Arduino Uno and Arduino IDE. You need the board, a USB cable and the Arduino IDE installed so you can upload and watch the Serial Monitor.
  • HC-SR501 PIR module. It has a dome lens on top, plus sensitivity and delay potentiometers and a mode jumper on the board.
  • Buzzer and jumper wires. The buzzer only needs a spare digital pin and a ground connection.
  • A still room for testing. The sensor needs a short period without movement after power-up before its readings settle.
Attribute Value Planning use
Supply voltage 4.5 V to 20 V DC for the module; at least 5 V, working up to 12 V in the SparkFun guide The Uno’s 5V pin is a safe, convenient supply.
Detection range Adjustable, up to 7 m (about 3 m to 7 m) Set it just beyond the area you want to watch.
Detection angle Approximately 120 degrees Aim the dome at the area to cover; the cone is wide.
Delay time 5 seconds to 5 minutes Short for testing and alarms, longer for lighting-style uses.
Warm-up About 15 seconds of motion-free time Keep the room still after power-up.
Output level High 3.3 V, low 0 V Treat it as a plain digital signal on one input pin.
Operating temperature -20 °C to +80 °C Comfortable for normal indoor use.

Figures are from the Osoyoo PIR module specification and the SparkFun PIR hookup guide.

How a PIR sensor detects motion

A PIR sensor reacts to changes in infrared radiation, not to a steady level of it. SparkFun explains that when the radiation pattern changes, the PIR interprets that change as movement.

A blue trimmer like this is the style of adjuster used for the range and delay settings; this stock photo shows a generic board, not a PIR module.

The sensing element is split into two halves that are wired so that they cancel each other out, so a static scene produces no signal. The element sits behind a multifaceted Fresnel lens, which widens the area it watches. For this module the detection angle is approximately 120 degrees, according to the same Osoyoo specification.

When motion is sensed, the output goes high, as the ElectronicWings Uno interfacing guide describes, and it falls low again when there is no motion. Your sketch only has to read that level.

Wiring, tuning and alarm steps

Step 1: Wire the sensor to the Arduino Uno

Find the power, output and ground pins from the markings on the module. Connect power to the Uno’s 5V pin, ground to GND and the output to a free digital pin. SparkFun states the sensor should be powered with at least 5 V but can work with voltages as high as 12 V, so the Uno’s supply is enough.

Check your board’s output type before writing code. The Osoyoo specification lists the module output as 3.3 V high and 0 V low, and the Uno example reads it with digitalRead, treating high as motion. SparkFun’s board is different: its alarm output is open-collector and active low, so it should be pulled high to 5 V. Its example reads the signal on pin 2 with INPUT_PULLUP, which inverts the logic. Use plain INPUT with high-active boards and INPUT_PULLUP with open-collector ones.

The robot chassis carries several small sensor modules; one has a blue potentiometer, and a metal ball caster sits at the front.

Step 2: Upload a test sketch in the Arduino IDE

Start with a sketch that only reports what the sensor sees, so you can tune it before adding the buzzer.

  1. Declare the pin. Store the PIR input pin number in a constant.
  2. Configure it in setup. Call pinMode on that pin with INPUT or INPUT_PULLUP, depending on your board, and start the serial port.
  3. Read it in loop. Call digitalRead on the pin and print a clear message such as “motion” or “idle” each time the state changes.

Printing only on changes keeps the Serial Monitor readable and makes the delay behaviour easy to see.

Step 3: Wait out the warm-up

The sensor triggers at start-up because it has not yet formed a picture of the room. SparkFun says it requires approximately 15 seconds of motion-free activity to do so. Anything that moves in view during that time, including you, is read as a change.

The ElectronicWings example handles this with a power-on warm-up delay in setup, which allows for the motion-free settling time the sensor needs to form a snapshot of the room. Add a similar delay to your sketch and stay out of the sensor’s view until it ends.

Step 4: Adjust sensitivity and delay

The module has two potentiometers. The sensitivity one sets the detection range, which spans about 3 m to 7 m. The delay one sets how long the output stays high after motion, anywhere from 5 seconds to 5 minutes, per the Osoyoo module description.

Turn each potentiometer in small increments and test after every change. For bench testing, set the delay short so the serial output returns to idle quickly, and set the range just beyond the area you want to watch. A shorter range limits the space the sensor responds to, which helps when something outside the target area keeps triggering it.

Step 5: Choose the retrigger mode with the jumper

The jumper selects between repeatable and non-repeating triggering. In repeatable mode, each detected motion resets the time delay, so the output stays high while someone keeps moving in view. That suits an alarm that should sound for as long as there is activity. The other jumper position is the non-repeating choice. Move the jumper, repeat a short walk-through and watch the serial output to confirm which behaviour you have.

Step 6: Add the buzzer alarm

Connect the buzzer’s positive lead to a second free digital pin and its negative lead to GND. SparkFun notes that once a microcontroller senses movement, it can trigger a buzzer.

In setup, set the buzzer pin as an output. In loop, make the buzzer pin high whenever the PIR reading indicates motion and low otherwise. Use the inverted test if your board is active low. If your buzzer needs a tone signal, generate it with the tone function instead of a steady high level.

Soldering the buzzer leads to short wires gives a firmer joint than pushing bare leads into a breadboard; this is a stock image of the process.

Because the buzzer follows the PIR output, it sounds for the whole delay period. Keep the delay short while you build, then lengthen it only if you want a longer alarm.

Loose jumper wires gather around a board like this one; trace each wire back to its pin before applying power.

Where to take it next

Once the alarm works, a Uno with a PIR sensor is a handy trigger for other builds. An arduino rotary encoder can add a panel control for settings such as alarm behaviour, and motion detection can start a routine on an arduino robotic arm. The module’s rated operating temperature is -20 °C to +80 °C, so ordinary indoor locations are well within its range.

Frequently asked questions

How long will the buzzer keep sounding after someone leaves?

It sounds for as long as the PIR output stays high, which is the delay you set on the delay potentiometer, from 5 seconds to 5 minutes. In repeatable mode, every new movement restarts that timer.

Why does the sensor miss motion straight after an alarm?

Once the delay completes, the output goes low for approximately 5 seconds and cannot detect during that time. Wait that long before testing again.

Can I power the module from a supply higher than 5 V?

The module’s working voltage is 4.5 V to 20 V DC, but SparkFun says it can also work with higher voltages. For an Uno build, the 5V pin is the simplest supply.