Board and part

Arduino Mega PIR Sensor Simulator: Wiring, Code and a Live Circuit

This is an HC-SR501 PIR motion sensor wired to an Arduino Mega 2560, running live: the serial monitor says when motion starts and when it ends. In the editor, press the sensor to walk past it.

  • 5 V logic
  • No library
  • OUT on pin 2
  • Powered from 5 V, 3.3 V output
Arduino Mega · PIR 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.
Connections

Wiring the PIR Sensor to the Arduino Mega

Three wires: VCC to 5V, GND, and OUT to pin 2. No library is needed.

Every connection in the circuit above, with the board’s own pin names.

PIR Sensor pinArduino Mega pinWhy
VCC5VThe HC-SR501 takes 4.5 to 20 V and regulates it down to 3.3 V on board.
OUTpin 2A 3.3 V high while it sees motion, then low.
GNDGNDCommon ground.

Power

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 on the Arduino Mega

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.

On a real bench

The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The PIR Sensor uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

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);
}

The PIR module does all the sensing itself; the board only reads one digital pin. OUT goes high when the infrared pattern in front of the lens changes, which is what a warm body moving does, and stays high for the hold time.

The sketch prints only changes, not every reading, by remembering the last state. That is the pattern for any sensor that holds a level: act on the edge, not the level.

Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.

Pitfalls

Common mistakes with the PIR Sensor on the 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.

FAQ

Questions and answers

Can I simulate a PIR sensor with an Arduino Mega 2560?

Yes. The circuit at the top of this page is an Arduino Mega 2560 with a HC-SR501 PIR sensor, running in your browser on a simulated ATmega2560. Press the sensor on the drawing for motion; its hold and block times behave like the module’s. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

Is the 3.3 V output enough for a 5 V board?

Yes. The Arduino Mega reads anything above 2.5 V as HIGH, and 3.3 V is comfortably above that.

What do the jumper positions H and L do?

H is repeat trigger: OUT stays high as long as motion keeps happening. L is single trigger: OUT goes low after the hold time even if motion continues, then can fire again. The part’s retrigger property chooses between them.

Can I use an interrupt for the PIR?

Yes, OUT is a clean digital edge with no bounce, so attachInterrupt() on RISING works well. Polling every 50 ms as here is just as good for a person walking past.

Build your own PIR Sensor project

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