Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

IR obstacle sensor with Raspberry Pi Pico W — MicroPython wiring and code

Detect objects with an FC-51 style IR obstacle sensor on a Pico W (GP17, 3.3 V) in MicroPython, count passes and light the on-board LED.

MicroPythonVerified — micropython (mpy-cross v1.29.0), 3 Nov 2026Compile-only

Community draft — report a mistake

The FC-51 style IR obstacle sensor is an IR LED and photodiode with a comparator: it pulls OUT low when something reflects enough light back. Powered from 3V3, it connects straight to any Pico W GPIO.

The script lights the on-board LED while something is in front of the sensor and counts each new obstacle — the start of a visitor counter or a no-touch switch.

Wiring

Raspberry Pi Pico WOnly the pins used are shown · 3.3 V logicIR obstacle avoidance sensor (FC-51)GND3V3GP17VCCGNDOUT
Every connection
Raspberry Pi Pico WIR obstacle avoidance sensor (FC-51)WireNotes
3V3VCCred3V3 is pin 36. OUT is pulled up to VCC, so 3.3 V keeps it safe for the Pico.
GNDGNDblack
GP17OUTyellowGP17 is physical pin 22.

3.3–5 V part, 3.3 V board. OUT is pulled up to VCC, so power the module from 3.3 V on ESP32 and Pico (it works fine there) and from 5 V on Uno.

Board quirks

  • OUT is LOW when something is detected — the code checks for 0, not 1.
  • Range is about 2–30 cm depending on the pot and the surface: white paper reflects far, black cloth hardly at all. Bright sunlight blinds it.
  • It counts edges, not readings, so a hand held in front of it counts once — handy for a simple people or object counter.

Code

main.pyMicroPython
# IR obstacle sensor (FC-51 style) on a Raspberry Pi Pico W (MicroPython).
# Wiring: VCC -> 3V3 (pin 36), GND -> GND, OUT -> GP17 (pin 22).
# OUT is LOW when something is in front of the sensor. Set the range with the blue pot.
from machine import Pin
import time

sensor = Pin(17, Pin.IN)
led = Pin("LED", Pin.OUT)
count = 0
was_blocked = False

print("IR obstacle sensor on GP17 - wave a hand 2-10 cm in front of it")
while True:
    blocked = sensor.value() == 0
    led.value(blocked)
    if blocked and not was_blocked:
        count += 1
        print("Obstacle! count =", count)
    elif was_blocked and not blocked:
        print("Clear")
    was_blocked = blocked
    time.sleep_ms(20)

Expected serial output

REPL / Thonny shellOutput
IR obstacle sensor on GP17 - wave a hand 2-10 cm in front of it
Obstacle! count = 1
Clear
Obstacle! count = 2
Clear

Common errors

Always “Obstacle!”

The pot is turned to maximum sensitivity, or the sensor sees the table. Turn the pot anticlockwise until the module’s second LED goes out.

Never detects anything

Check OUT is on GP17 (physical pin 22). Outdoors, sunlight swamps the IR — test indoors first.

↑↓ to move · Enter to open