Arduino / Beginner

Read a PIR sensor before connecting a motor

Three labelled connections, a downloadable UNO sketch and a timing experiment that separates sensor output from software hold time.

By Harry AvraamidesUpdated 22 November 202630 min activity

Before you start
Classic Arduino UNO R3; a documented 5 V-powered PIR module with an UNO-compatible digital output; data cable and three jumpers.

You will leave with
A motion indicator you can observe independently of the pencil-case servo.

When a motion-triggered project seems complicated, remove the moving part from the first experiment. Read the sensor, light the built-in LED and print a short message. You can then tell a sensor problem from a servo problem.

The reference module here is the documented Joy-IT SEN-HC-SR501, powered at 5 V with a digital output specified at 3 V. HC-SR501-looking modules can differ. Check your particular pin labels, supply range and output compatibility; the drawing below deliberately avoids promising a universal physical pin order.

Recognise the part before wiring it. These two views show the lens and adjustment side of an example module. Match the labels and supplier documentation for your own board; this is not a universal pinout.

Front of a PIR sensor module dominated by a white faceted Fresnel lens.
Component reference photograph

The faceted white cover is the Fresnel lens. It shapes the detection zones; it is not an ultrasonic distance sensor.

Photo: Luiz Augusto Souza de Mesquita · CC0 1.0. Wikimedia preview; no further edits. Image credits and scope.
Rear of a PIR module with two orange adjustment controls, a yellow mode jumper and a blue circuit board.
Component reference photograph

The reverse side shows two adjustment controls and a trigger-mode jumper. This HW-416A/HC-SR501-style example is not the exact Joy-IT unit whose specifications are referenced above.

Photo: Luiz Augusto Souza de Mesquita · CC0 1.0. Wikimedia preview; transparent margins cropped in the page layout. Image credits and scope.

These photographs are by another maker, not photographs of Harry’s project or evidence of a Workbench hardware test. Use the functional wiring drawing below for the connection plan.

1. Make only three connections

PIR VCC to UNO 5V, PIR OUT to D2, PIR ground to UNO GND. The built-in LED needs no additional wiring.
Read the terminal labels on your module before plugging in a wire. Save diagram ↓

Disconnect USB while wiring. Connect VCC to 5V, OUT/DATA to D2 and GND to GND, after verifying those functions in the module documentation. Set D2 as INPUT, not OUTPUT. The UNO's built-in LED is our entire output for this experiment. No servo supply, lid or soldering is needed at this stage.

2. Upload the indicator

Open the downloaded sketch in the Arduino IDE; save it in a folder with the same name if prompted. Select Arduino Uno, choose its port and upload. Open Serial Monitor at 9600 baud. You should first see WARMING, then READY after the configured settling period.

Download pir-motion-indicator.ino ↓

Read the complete sketch
// Workbench — PIR motion indicator, Arduino UNO R3.
// D2 = documented PIR module OUT; use the built-in LED.
// Reference design: see the tutorial for wiring and physical checks.
const byte PIR_PIN = 2;
const unsigned long WARMUP_MS = 60000UL;
const unsigned long HOLD_MS = 2000UL;
unsigned long startedAt = 0;
unsigned long lastHighAt = 0;
bool warmingUp = true;
bool active = false;

void setup() {
  pinMode(PIR_PIN, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.begin(9600);
  startedAt = millis();
  Serial.println("WARMING: LED off, allow sensor to settle");
}

void loop() {
  const unsigned long now = millis();
  if (warmingUp) {
    if (now - startedAt < WARMUP_MS) return;
    warmingUp = false; // One startup phase; does not restart at millis rollover.
    Serial.println("READY: observing PIR output");
  }

  const bool wasActive = active;
  if (digitalRead(PIR_PIN) == HIGH) {
    lastHighAt = now;
    active = true;
  } else if (active && now - lastHighAt >= HOLD_MS) {
    active = false;
  }
  digitalWrite(LED_BUILTIN, active ? HIGH : LOW);
  if (active != wasActive) Serial.println(active ? "ACTIVE" : "QUIET");
  // LOW is not proof that a person or object has left the area.
}

3. Understand the two different timers

The module has its own output hold time. Turning a module's trimmer can change how long its OUT stays HIGH. The sketch adds a software hold. Our LED remains active until two seconds have passed since the last observed HIGH sample. Changing HOLD_MS does not change the module itself.

Warm-up is a separate one-time phase. The default 60 seconds is an observation window, not proof that every sensor has stabilised. Keep the scene still, watch for unexpected transitions, and adjust based on your module. Adafruit describes typical settling and retrigger behaviour in its PIR test procedure. In repeat-trigger mode, continuing movement can extend the module's HIGH output.

The variable warmingUp becomes false once. That avoids restarting the warm-up when millis eventually wraps. The subtraction now - lastHighAt compares elapsed unsigned time. Keep the short intervals shown here, rather than turning this into a multi-month scheduling system.

4. Predict the output before testing

Example timeline after the warm-up phase
TimeSensor observationExpected LED
0 msLOW; no event yetOff
100 msHIGHOn; lastHighAt becomes 100
1,000 msLOWStill on; less than 2,000 ms elapsed
2,100 msLOWOff; the hold has elapsed
Any time while activeAnother HIGH sampleOn; the hold starts again from that sample

Try the same decisions in the interactive timing lab. That lab demonstrates code logic; it does not simulate infrared optics, noise or the module's trimmers.

5. Run a repeatable observation

  1. Start with the module stationary and wait for READY.
  2. Move across its field of view once. Record when ACTIVE appears.
  3. Stop moving and time when QUIET appears. Do not assume it equals two seconds: the module can still be holding HIGH.
  4. Repeat three times without changing a setting.
  5. Change only the module's trigger mode or one trimmer; record the new results separately.

Download the observation log. It starts blank because no measurements have been taken for this publication.

6. Diagnose one symptom at a time

If nothing appears in Serial Monitor, check the port and baud rate before rewiring. If READY appears but no events follow, check D2 and module ground with power off. If output stays active, observe the module hold time, retrigger setting and surroundings before changing the code. If it activates after every power-up, allow settling and check whether the board is resetting.

A PIR responds to changes in infrared patterns. It is not a hand-distance sensor, an obstacle detector or a reliable occupancy detector. LOW cannot establish that a pencil-case lid is clear to close. Keep this tutorial as an indicator until a separate mechanical safety and control plan exists.

Reference exercise · Read the evidence and checking record.

Your privacy, your choice.

This site currently loads no advertising or optional analytics scripts. Hosting includes aggregate traffic statistics and essential site-delivery functions. Optional services are off by default; changing these preferences does not activate them.

EssentialSite delivery and your saved privacy choices.Always on

Read the privacy notice