Wiring guide

How to wire a PIR motion sensor to an Arduino Mega

To wire a PIR motion sensor to an Arduino Mega, connect VCC to 5V, OUT to pin 2 and GND to GND. The module’s output is a 3.3 V high, which the Arduino Mega reads as HIGH, so nothing else is needed. The sketch below uses no library beyond what the board includes. It prints Motion detected whenever the sensor’s output goes high.

  • 5 V logic
  • Library: none
  • 3 wires
Arduino Mega · PIR motion sensorlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

PIR motion sensor to Arduino Mega wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

PIR motion sensor pinArduino Mega pinWire colorWhy
VCC5VredThe HC-SR501 takes 4.5 to 20 V and regulates it down to 3.3 V on board.
OUTpin 2amberA 3.3 V high while it sees motion, then low.
GNDGNDblueCommon ground.
Voltage

Power and logic levels for PIR motion sensor and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The PIR motion sensor takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

The output is a 3.3 V high, which the Arduino Mega reads as HIGH: its threshold is half of 5 V. Nothing else is needed.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: none, the sketch reads OUT with digitalRead(). This is the sketch the circuit above runs.

PIR sensor on Arduino Mega pin 2
const int PIR_PIN = 2;   // OUT, pin 2
int lastState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(PIR_PIN, INPUT);
  Serial.println("Watching for motion");
}

void loop() {
  int state = digitalRead(PIR_PIN);
  if (state != lastState) {
    lastState = state;
    Serial.println(state == HIGH ? "Motion detected" : "Motion ended");
  }
  delay(50);
}
Output

What you should see

Open the serial monitor at 9600 baud. The sketch prints Watching for motion once; each time the sensor sees movement, a Motion detected line follows, and Motion ended when it settles. In the editor, click the sensor to trigger it.

Real PIR modules need about a minute to settle after power-up and then hold their output high for a few seconds per trigger.

The serial monitor prints this, recorded from the circuit above with motion triggered after 300 ms:

Watching for motion
Motion detected
Pitfalls

Common mistakes wiring a PIR motion sensor to an Arduino Mega

Not waiting for it to settle

A real HC-SR501 needs up to a minute after power-up to learn the room, and gives false triggers until then. Ignore it for the first 30 to 60 seconds.

The hold time knob

The orange trimmer next to the sensitivity one sets how long OUT stays high, from about 3 seconds to several minutes. A sensor that seems stuck on is usually turned all the way up. The simulated part has a hold property for the same thing.

Taking the serial or I2C pins

Pins 0 and 1 are the USB serial port, 14 to 19 are Serial1 to Serial3 and 20 and 21 are I2C. the PIR output works on any of them until the sketch needs that port, so start on pin 2 and up.

Build your own PIR motion sensor project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.