Raspberry Pi Pico · PIR Sensor

Raspberry Pi Pico PIR Sensor Simulator: Wiring, Code and a Live Circuit

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

Three wires: VCC to VBUS (5 V from USB), GND, and OUT to GP15 (physical pin 20). No library is needed.

  • 3.3 V logic
  • No library
  • OUT on GP15 (physical pin 20)
  • Powered from 5 V, 3.3 V output
Raspberry Pi Pico · 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.

Wiring the PIR Sensor to the Raspberry Pi Pico

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

PIR Sensor pinRaspberry Pi Pico pinWhy
VCCVBUS (5 V from USB)The HC-SR501 takes 4.5 to 20 V and regulates it down to 3.3 V on board.
OUTGP15 (physical pin 20)A 3.3 V high while it sees motion, then low.
GNDGNDCommon ground.

The module needs VBUS (5 V from USB) for power, but its output is 3.3 V, so OUT connects straight to the Raspberry Pi Pico with no divider. That makes the PIR one of the few 5 V modules that is a perfect match for a 3.3 V board.

Pins to leave alone on the Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the PIR Sensor goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

The code

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 Raspberry Pi Pico GP15 (physical pin 20)
const int PIR_PIN = 15;   // OUT, GP15 (physical pin 20)
int lastState = LOW;

void setup() {
  Serial.begin(115200);
  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(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.

Common mistakes with the PIR Sensor on the Raspberry Pi Pico

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.

Powering it from 3V3

Its regulator wants 4.5 V or more; at 3.3 V it triggers at random or not at all. Power from VBUS (5 V from USB); the output is already 3.3 V.

Questions and answers

Can I simulate a PIR sensor with a Raspberry Pi Pico?

Yes. The circuit at the top of this page is a Raspberry Pi Pico with a HC-SR501 PIR sensor, running in your browser on a simulated RP2040. 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 PIR output safe for the Raspberry Pi Pico?

Yes. The HC-SR501 is powered from 5 V but its output pin swings 0 to 3.3 V, exactly the board’s level.

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.