Skip to content

PIR Motion Counter

Project 26 • Beginner


Overview

Build a motion event counter using a PIR sensor and an OLED display.

This project demonstrates digital input counting, I2C display communication, and data persistence.

The final result counts and displays each motion event detected by the PIR sensor.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
PIR motion sensor
PIR motion sensor
SH1106 OLED display
SH1106 OLED display
Breadboard
Breadboard
Jumper wires
Jumper wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
PIR VCC 3.3V Physical pin 36 Check module label
PIR GND GND Physical pin 38 —
PIR OUT GPIO 1 GPIO 1 / physical pin 2 3.3V logic only
OLED VCC 3.3V Physical pin 36 Use a 3.3V-compatible module
OLED GND GND Physical pin 38 —
OLED SDA GPIO 8 GPIO 8 / physical pin 11 I2C data line
OLED SCL GPIO 9 GPIO 9 / physical pin 12 I2C clock line

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

step1

Step 2: Connect Power Rails

Connect the 3.3V and GND rails to the breadboard power rails. This provides power for the PIR sensor and OLED display.

step2

Step 3: Place the PIR Sensor

Place the PIR sensor on the breadboard with the white dome facing the area where you want to detect motion. Identify the VCC, OUT, and GND pins before wiring.

step3

Step 4: Wire the PIR Sensor

Connect the PIR VCC to 3.3V, PIR GND to GND, and PIR OUT to GPIO 1 on the Pico.

step4

Step 5: Place the OLED Display

Insert the OLED display onto the breadboard. Make sure the pins are in separate rows and the display faces upward for visibility.

step5

Step 6: Wire the OLED Display

Connect the OLED VCC to 3.3V, OLED GND to GND, OLED SDA to GPIO 8, and OLED SCL to GPIO 9.

step6


Wiring Check

Pico 2W is placed correctly across the breadboard center gap
PIR sensor faces the motion detection area
PIR VCC connects to 3.3V
PIR GND connects to GND
PIR OUT connects to GPIO 1
OLED VCC connects to 3.3V
OLED GND connects to GND
OLED SDA connects to GPIO 8
OLED SCL connects to GPIO 9
No loose jumper wires


Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

PIR sensor test (motion detection)

Check motion detection first.

from machine import Pin
import time

pir = Pin(1, Pin.IN)

print('Wait 10 seconds for PIR warm-up')
time.sleep(10)

while True:
    print('Motion' if pir.value() else 'No motion')
    time.sleep(0.5)

Expected test result: The Shell reports Motion when movement is detected after warm-up.

OLED I2C scanner test

Check that the OLED is detected on the I2C bus.

from machine import Pin, SoftI2C

i2c = SoftI2C(sda=Pin(8), scl=Pin(9))
devices = i2c.scan()
print('I2C devices found:', [hex(addr) for addr in devices])

Expected test result: The Shell reports one or more I2C addresses, typically 0x3c for the SH1106 OLED.

OLED text test

Check that the OLED displays text correctly.

from machine import Pin, I2C
import sh1106
import time

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
display = sh1106.SH1106_I2C(128, 64, i2c)
display.fill(0)
display.text('Motion OLED OK', 8, 28, 1)
display.show()

Expected test result: "Motion OLED OK"


Full Project Code

Upload and run this code after the individual tests work correctly.

from machine import Pin, I2C
import sh1106
import time

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
display = sh1106.SH1106_I2C(128, 64, i2c)
pir = Pin(1, Pin.IN)

motion_count = 0
last_motion = 0

print('Motion counter ready')
print('Wait 10 seconds for PIR warm-up')
time.sleep(10)

while True:
    motion = pir.value()

    if motion == 1 and last_motion == 0:
        motion_count += 1
        print('Motion detected! Count:', motion_count)

    display.fill(0)
    display.text('Motion Counter', 10, 8, 1)
    display.text('Count: {}'.format(motion_count), 18, 30, 1)
    display.text('MOTION!' if motion else 'Waiting...', 18, 50, 1)
    display.show()

    last_motion = motion
    time.sleep(0.2)

How the Code Works

Code Section What It Does Why It Matters
PIR input Configures GPIO 1 as input for motion detection Reads motion events from the sensor
motion_count Stores the total count of motion events Persists across loop iterations
Edge detection Detects rising edge from the PIR sensor Counts only one event per motion trigger
OLED update Shows the current count on the display Provides visual feedback of the count

Expected Result

After waiting for the PIR warm-up time, each motion event in front of the sensor increments the counter. The OLED display shows the running total, and the Shell prints each detection.


Troubleshooting

Problem Possible Cause Solution
Count does not change PIR not warmed up or wrong pins Wait 10 seconds and check GPIO 1 wiring
OLED shows nothing Wrong I2C pins or no library Confirm SDA/GPIO 8 and SCL/GPIO 9, install sh1106 library
Multiple counts per motion No debounce logic Add delay after counting to avoid multiple triggers
OLED shows strange characters Wrong I2C address Run I2C scanner to find correct address