Fall ResetAmazon USFall reset deals: check better picks before checkoutAmazon US: today's deals, useful picks and quick comparisons.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowFall ResetAmazon USWork and home upgrades are worth comparing todayAmazon US: today's deals, useful picks and quick comparisons.See Picks×
Skip to content
Blog

PIR Sensor Arduino Alarm: Wiring, Code, and Troubleshooting

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Build a local motion alarm by connecting a PIR module’s output to an Arduino digital input and a small buzzer to an output pin. The example below uses an Uno-compatible 5 V board, a typical HC-SR501-style active-high module, and a non-blocking timer so the buzzer sounds for a set period. PIR modules are not electrically identical: verify whether yours signals motion with HIGH or LOW before choosing the code.

This is a useful learning or room-occupancy project, not a certified security system. A PIR detects changes in infrared energy as objects move through its field of view; it cannot identify a person or guarantee that an intruder will be detected.

Parts and tools

  • Arduino Uno, Uno R3-compatible board, or another board whose input voltage matches the PIR output.
  • Three-pin PIR module, such as an HC-SR501-style module.
  • Small 5 V-compatible buzzer, breadboard, jumper wires, and USB cable or suitable supply.
  • Optional: LED and approximately 220 Ω resistor. The sketch below uses the board’s built-in LED.
  • For a larger siren or other load: a suitably rated transistor or MOSFET driver, and a separate supply if needed. A relay coil also needs a flyback diode unless the driver module already includes protection.

A small buzzer may be suitable for direct GPIO control, depending on its electrical requirements. Check the buzzer’s voltage and current against the board’s limits. Do not power a siren, motor, lamp, or relay coil directly from an Arduino pin.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What a PIR sensor does—and does not do

PIR means passive infrared. Unlike an active IR distance sensor, a PIR does not shine infrared light. Its sensing element responds to changes in infrared radiation across zones shaped by the module’s lens. A person or animal moving against a different-temperature background can create a trigger.

#1 Best Overall
WWZMDiB 5 Pcs PIR Sensor Compatible with HC-SR501 PIR Motion Module for Arduino Raspberry Pi STM32 (Comes with 2 Dedicated Cases)
  • WWZMDiB 5 Pcs PIR Sensor: When a human body enters the sensing range, the temperature difference between the body and the background causes a voltage change in the pyroelectric device. After amplification and comparison, the voltage signal is output.
  • Voltage:DC 4.5-20V
  • Detection Angle: <110 ° cone angle Lens size
  • Detection range: 3-7 meters (10-23 feet)(adjustable)
  • Two triggering modes: H: The output signal is maintained as long as a person is present. L: Triggered once with each change.

That makes a PIR a motion detector, not a dependable presence detector: someone who remains still may stop producing triggers. Motion across the sensor’s view is often easier to detect than motion directly toward it. Lens shape, mounting, distance, temperature contrast, and the particular module all affect coverage; there is no single range or field of view that applies to every PIR. For a general explanation of module variation, see Adafruit’s PIR guide.

Identify the module before wiring the code

Read the board labels and documentation rather than assuming that every module has the same pin order. Find VCC, GND, and OUT or SIG; wire colors and physical pin order are not universal.

  • Active-high output: the output reads HIGH during motion. Many HC-SR501-style examples use this behavior.
  • Active-low output: the output reads LOW during motion. SparkFun’s documented PIR, for example, has an open-collector alarm output; it needs a pull-up so the inactive state does not float.

Adafruit documents a HIGH-on-trigger convention for its example module, while SparkFun documents an active-low, open-collector design. Those differences are why a sketch for one PIR should not be assumed to work unchanged with another. See the respective Adafruit wiring guide and SparkFun hookup guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

HC-SR501-style boards commonly have a sensitivity adjustment, a delay adjustment, and an H/L retrigger jumper. Typically, sensitivity changes approximate range, the delay control changes how long the output stays asserted, and H retriggers or extends the active period while motion continues while L tends toward a single timing interval. These labels and behaviors are not universal; test your board.

Wire the basic build

Disconnect USB power while making connections. For a typical active-high HC-SR501-style module and an Uno-compatible 5 V board:

Rank #2
2-Pack HC-SR501 PIR Motion Sensor Module with Adjustable Sensitivity & Delay, 5V DC, for Arduino Compatible, Green PCB‌
  • Detects human motion up to 7 meters away with 110° coverage using a built-in Fresnel lens for enhanced accuracy and range
  • Adjustable sensitivity and delay time via onboard potentiometers—customize response for indoor lighting, security alarms, or automated systems
  • Low-power design consumes under 65µA in standby mode, perfect for battery-operated IoT devices and energy-efficient installations
  • Compatible with Arduino, Raspberry Pi, and 5V logic systems—directly connects to digital pins with no external circuitry required
  • Robust green PCB with stable output and wide operating voltage (3.6V–30V DC), suitable for both prototyping and permanent installations
Component connection Arduino connection
PIR VCC 5V, if the module documentation permits it
PIR GND GND
PIR OUT or SIG Digital pin 2
Small buzzer positive lead Digital pin 8
Small buzzer negative lead GND

Check the exact PIR’s supply requirement and output voltage. Do not assume that all modules accept 5 V or that every Arduino-family board has 5 V-tolerant inputs. Some boards use 3.3 V GPIO. The UNO R4 Minima and UNO R4 WiFi are 5 V Uno-class options; see the official UNO R4 Minima and UNO R4 WiFi specifications. Their capabilities do not make 5 V wiring safe for every other Arduino-compatible board.

Test the PIR signal first

Upload this diagnostic sketch before adding alarm behavior. Open Serial Monitor at 115200 baud, then allow the sensor to settle before testing:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const byte PIR_PIN = 2;

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
}

void loop() {
  Serial.println(digitalRead(PIR_PIN));
  delay(100);
}

For a typical active-high module, the reading should usually be 0 when idle and 1 during motion. An active-low module may show the reverse. For an open-collector output, the inactive state needs a pull-up; use pinMode(PIR_PIN, INPUT_PULLUP) when the module’s documentation calls for one. Do not add INPUT_PULLUP indiscriminately to a module that already drives a strong HIGH/LOW signal.

After power-up, keep people and moving heat sources out of the sensor’s view while it establishes a baseline. The settling time varies by module and conditions. SparkFun describes an initial still-room period and about 15 seconds of motion-free settling for its example sensor; treat that as guidance for that product, not a universal PIR specification. Once settled, walk across the field of view and note which serial value corresponds to motion.

Upload the alarm sketch

This version is set for an active-high sensor. It sounds the buzzer for five seconds after a new motion transition, turns on the built-in LED at the same time, and uses millis() rather than a long blocking delay.

Rank #3
HiLetgo 3pcs HC-SR501 PIR Infrared Sensor Human Body Infrared Motion Module for Arduino Raspberry Pi
  • Operating voltage range: DC 4.5-20V
  • Quiescent Current: <50uA Trigger: L can not be repeated trigger/H can be repeated trigger(Default repeated trigger)
  • Delay time: 5-200S(adjustable) the range is (0.xx second to tens of second)
  • Board Dimensions: 32mm*24mm
  • Angle Sensor: <100 ° cone angle Lens size sensor:Diameter:23mm(Default)
const byte PIR_PIN    = 2;
const byte BUZZER_PIN = 8;
const byte LED_PIN    = LED_BUILTIN;

const unsigned long STARTUP_SETTLE_MS = 15000UL;
const unsigned long ALARM_DURATION_MS = 5000UL;

bool alarmActive = false;
bool previousMotion = false;
unsigned long bootTime = 0;
unsigned long alarmStartedAt = 0;

void setup() {
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);

  noTone(BUZZER_PIN);
  digitalWrite(LED_PIN, LOW);

  Serial.begin(115200);
  bootTime = millis();

  Serial.println("PIR alarm starting.");
  Serial.println("Keep the sensor still during warm-up.");
}

void loop() {
  unsigned long now = millis();

  // Ignore triggers during the initial settling period.
  if (now - bootTime < STARTUP_SETTLE_MS) {
    noTone(BUZZER_PIN);
    digitalWrite(LED_PIN, LOW);
    return;
  }

  // Typical HC-SR501-style active-high module.
  // For active-low, change HIGH to LOW and see the pull-up note below.
  bool motionDetected = digitalRead(PIR_PIN) == HIGH;

  // Start once on the transition from idle to motion.
  if (motionDetected && !previousMotion) {
    alarmActive = true;
    alarmStartedAt = now;
    Serial.println("Motion detected: alarm ON");
  }

  previousMotion = motionDetected;

  if (alarmActive) {
    tone(BUZZER_PIN, 2200);
    digitalWrite(LED_PIN, HIGH);

    if (now - alarmStartedAt >= ALARM_DURATION_MS) {
      alarmActive = false;
      noTone(BUZZER_PIN);
      digitalWrite(LED_PIN, LOW);
      Serial.println("Alarm OFF");
    }
  } else {
    noTone(BUZZER_PIN);
    digitalWrite(LED_PIN, LOW);
  }
}

The 15-second startup ignore period is an example, not a guarantee that every module is calibrated by then. Adjust it to the module’s documentation and observed behavior. The timer is non-blocking: the loop can continue checking inputs and handling other work while the alarm is active. Arduino’s official language reference documents functions used here, including pinMode(), digitalRead(), and millis(); see also the Tone reference for tone() and noTone().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an active-low, open-collector PIR

Use the pull-up if the sensor documentation specifies an open-collector output, and change the motion test to LOW:

pinMode(PIR_PIN, INPUT_PULLUP);
bool motionDetected = digitalRead(PIR_PIN) == LOW;

Keep the rest of the sketch unchanged. If the serial test never settles to a clear idle value, recheck the output type, pull-up requirement, ground connection, and module documentation before changing other logic.

Choose the alarm behavior you actually want

  • One alarm per motion transition: the supplied sketch triggers on the change from idle to motion. It avoids repeated starts during one continuous active signal.
  • Buzzer on while motion remains active: simpler, but a retriggering PIR or a long module delay can keep the sound on for much longer than expected.
  • Fixed-duration alarm: the supplied code sounds for five seconds regardless of how long the sensor stays active. Change ALARM_DURATION_MS to suit the demonstration. A new motion transition can start another alarm.

The PIR’s own output timing and retrigger setting affect what counts as a new transition. If you need a cooldown, a latched alarm, or an alarm that resets only after a button press, add that as explicit state rather than relying on the sensor’s delay knob.

Add an arm/disarm switch

For a simple switch, connect one terminal to digital pin 4 and the other to GND, then configure the input with the built-in pull-up:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
HiLetgo 3pcs AM312 Mini Pyroelectric PIR Human Sensor Module PIR Infrared IR Sensor Body Manual Motion Infrared IR Detector
  • Working voltage: DC 2.7-12V.
  • AM312 Human Sensing Module: Based on passive body infrared technology digital intelligent automatic control products, high sensitivity, reliability, widely used in various types of automatic induction electrical equipment.
  • Low power consumption and small size for easy embedded installation.
  • Sensing range: ≤100 degree cone angle, 3-5 meters; (depending on the specific lens)
const byte ARM_PIN = 4;

void setup() {
  pinMode(ARM_PIN, INPUT_PULLUP);
}

With this wiring, the input reads HIGH when the switch is open and LOW when it is closed. Read the state with bool armed = digitalRead(ARM_PIN) == LOW;, and only start an alarm on a motion transition when armed is true. Decide explicitly what disarming should do if an alarm is already active—for example, turn off the buzzer and LED immediately.

Driving a larger siren or relay

For a load that exceeds the GPIO pin’s current or voltage limits, use a properly rated NPN transistor or logic-level N-channel MOSFET as a driver. A relay coil needs a flyback diode across the coil unless the module incorporates one. A separate supply may be necessary; for a non-isolated driver, connect its ground to Arduino GND so the control signal has a shared reference. Choose parts and wiring for the actual load, not simply the Arduino pin.

A relay module does not make mains wiring safe. Mains voltage is outside the scope of this beginner build; use properly rated equipment and qualified installation rather than treating a breadboard alarm as a mains controller.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting

The alarm triggers constantly

  • Wait through the sensor’s settling period with the field of view clear.
  • Reduce sensitivity and aim away from windows, direct sunlight, heaters, radiators, hot electronics, and changing HVAC airflow.
  • Keep the module stable; movement of the sensor itself changes the scene it sees.
  • Check VCC, GND, and OUT connections and confirm that the selected HIGH/LOW condition matches the serial test.
  • If the output is open-collector, verify the required pull-up. A floating output can produce unreliable readings.

Nothing happens when someone walks past

Confirm supply and ground polarity, the signal pin number, and that the uploaded sketch is running. Check that warm-up has finished, walk across the sensor’s view, and confirm the expected serial value at 115200 baud. Then check buzzer polarity and whether the buzzer is appropriate for direct GPIO drive. If serial output changes but there is no sound, troubleshoot the buzzer circuit separately from the sensor.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The buzzer seems stuck on

The sketch may be written to sound while the PIR output remains active, the module may be retriggering, or its delay potentiometer may be set long. Verify that the intended fixed-duration sketch is running and that the input is not floating or noisy. The supplied timer uses unsigned elapsed-time subtraction (now - alarmStartedAt), which is the appropriate pattern for timing with millis().

Best Value
DIYmall 5 Pack HC-SR501 Pir Motion IR Sensor Body Module Infrared for Arduino
  • Using Potentiometer 105, output timing is from 0.5S to 200S
  • Widely used in:Security Products,human body sensors toys,human body sensor lighting industrial automation and control, etc
  • NOTE: On this retrigger jumper is a solder jumper, and you need solder it by yourself
  • Pls note that there is no IR emitter in this module, the principle of PIR sensor is to detect the infrared radiation emitted by the human body, it only have a IR sensor (cell)
  • Package Included: 5 X HC-SR501 PIR Infared Sensor

The output seems inverted

This often reflects the module’s design rather than a fault. Use HIGH for motion on an active-high module, LOW for motion on an active-low module, and enable a pull-up when required by an open-collector output. Do not reverse power and ground to try to correct signal polarity.

Pets or other heat sources trigger it

A PIR reacts to thermal movement, not human identity. A pet may trigger it depending on size, distance, temperature contrast, and the lens’s field of view. A generic PIR project cannot promise pet immunity. If that is a requirement, use purpose-designed security equipment or combine sensors with a carefully designed detection strategy.

Extensions and when to choose another sensor

Once the local alarm works, the same input can support multiple PIR zones, a display, event logging, or a wireless notification system. Keep each added task non-blocking so it does not prevent the controller from reading the sensor or responding to an arm switch. For wireless connectivity, the UNO R4 WiFi includes an ESP32-S3 module, but remote alerts add network, credential, and connectivity-failure considerations; they are unnecessary for a local buzzer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a different sensor when the requirement is different: a magnetic reed switch detects a door or window opening; a break-beam detects crossing a defined line; an ultrasonic sensor measures distance in a suitable layout; radar may detect motion through some nonmetallic materials but can bring interference and false-trigger trade-offs. A camera can support richer classification, with privacy and processing costs. None of these options makes a basic DIY build a substitute for a properly designed commercial alarm when premises security matters.

Security limits

This project provides a local warning for learning or automation. It has no inherent tamper detection, monitoring service, backup battery, certified intrusion detection, guaranteed false-alarm resistance, or reliable human identification. A local buzzer can be silenced by disconnecting or damaging the hardware. Do not rely on it as the sole security or life-safety system for a building.

Quick Recap

Bestseller No. 1
WWZMDiB 5 Pcs PIR Sensor Compatible with HC-SR501 PIR Motion Module for Arduino Raspberry Pi STM32 (Comes with 2 Dedicated Cases)
WWZMDiB 5 Pcs PIR Sensor Compatible with HC-SR501 PIR Motion Module for Arduino Raspberry Pi STM32 (Comes with 2 Dedicated Cases)
Voltage:DC 4.5-20V; Detection Angle: <110 ° cone angle Lens size; Detection range: 3-7 meters (10-23 feet)(adjustable)
$8.99
Bestseller No. 3
HiLetgo 3pcs HC-SR501 PIR Infrared Sensor Human Body Infrared Motion Module for Arduino Raspberry Pi
HiLetgo 3pcs HC-SR501 PIR Infrared Sensor Human Body Infrared Motion Module for Arduino Raspberry Pi
Operating voltage range: DC 4.5-20V; Delay time: 5-200S(adjustable) the range is (0.xx second to tens of second)
$8.49
Bestseller No. 4
HiLetgo 3pcs AM312 Mini Pyroelectric PIR Human Sensor Module PIR Infrared IR Sensor Body Manual Motion Infrared IR Detector
HiLetgo 3pcs AM312 Mini Pyroelectric PIR Human Sensor Module PIR Infrared IR Sensor Body Manual Motion Infrared IR Detector
Working voltage: DC 2.7-12V.; Low power consumption and small size for easy embedded installation.
$8.69
Bestseller No. 5
DIYmall 5 Pack HC-SR501 Pir Motion IR Sensor Body Module Infrared for Arduino
DIYmall 5 Pack HC-SR501 Pir Motion IR Sensor Body Module Infrared for Arduino
Using Potentiometer 105, output timing is from 0.5S to 200S; NOTE: On this retrigger jumper is a solder jumper, and you need solder it by yourself
$9.49

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.