pb pbaumgarten.com
Courses Python Reference IR sensor
Login
Course
Python Reference
Topic
MicroPython
Python Reference

IR sensor

IR Proximity Sensor

Wiring:

IR Sensor Pin Connect To
VCC 3.3V
GND GND
OUT GPIO 12 (or any GPIO)

Logic: Active-low — output goes 0 (LOW) when an object is detected.

Some modules have a sensitivity potentiometer (small screw). Turn it to adjust detection range.

Code:

from machine import Pin
import time

ir = Pin(12, Pin.IN)

while True:
    if ir.value() == 0:   # Object detected
        print("Object detected!")
    else:
        print("Clear")
    time.sleep(0.1)

Did you know? IR sensors work by emitting infrared light and detecting its reflection. They're used in automatic hand sanitiser dispensers, TV remote controls, and robot obstacle detection.