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 W | IR obstacle avoidance sensor (FC-51) | Wire | Notes |
|---|---|---|---|
| 3V3 | VCC | red | 3V3 is pin 36. OUT is pulled up to VCC, so 3.3 V keeps it safe for the Pico. |
| GND | GND | black | |
| GP17 | OUT | yellow | GP17 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
# 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)# 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
IR obstacle sensor on GP17 - wave a hand 2-10 cm in front of it
Obstacle! count = 1
Clear
Obstacle! count = 2
ClearIR 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.