PyRasp

Obstacle-avoidance sensor

The obstacle-avoidance module costs $3.50 and is a one-bit proximity switch. An infrared LED and receiver point forward; an object in front reflects the IR back, and an LM393 comparator compares that reflection with the trimmer. OUT is high when something is within the trip distance and low when the path is clear. It is the sensor behind countless "robot stops before the wall" projects.

Obstacle-avoidance sensor

Pins

PinWhat it isType
VCCSupply 3.3–5 Vpower
GNDGroundground
OUTDigital out: HIGH when an object is closeio
SettingTypeRangeDefault
thresholdCmnumber2–3020

In circuit.py:

thresholdCm is the trimmer's trip distance, 2–30 cm. The feed's distanceCm is what the sensor sees; the comparator turns it into blocked and OUT.

Wiring it in circuit.py

Power it from 3.3 V and read the output like a button:

circuit.py
from wiring import *

pi = Pi()

obstacle = ObstacleSensor()

pi.V3_3 >> obstacle.vcc
obstacle.gnd >> pi.GND
obstacle.out >> pi.GPIO17

On a Pico, use pico.P36 (3V3), pico.GND and pico.GP16. On the KS3017 shield, V goes to the 3.3 V selection; the signal header is S.

Driving it in main.py

main.py
from gpiozero import DigitalInputDevice
from time import sleep

obstacle = DigitalInputDevice(17)

while True:
    print("blocked" if obstacle.value else "clear")
    sleep(0.05)

The output is already 3.3 V on the classroom module. A bare 5 V module needs a divider before a Pi or Pico GPIO. It is not a distance sensor: OUT gives close/far, not centimetres. For a distance, use the HC-SR04.

Electrical notes

  • Supply 3.3–5 V; the 3.3 V connection keeps OUT safe for a GPIO.
  • Range is about 2–30 cm, set by the trimmer. Black or transparent objects reflect poorly and may be missed.
  • Bright sunlight and strong ambient IR can reduce the range. Shade the receiver or use a modulated sensor for outdoor robots.
  • The comparator output is active high on these classroom modules. Some modules are active low; check with a meter before trusting the logic in your code.
  • Add hysteresis in software if the output chatters at the trip point: stop below 15 cm, resume above 25 cm.

Simulator vs real hardware

  • The distanceCm feed and thresholdCm prop model the comparator exactly: blocked is high at or below the threshold.
  • The simulator does not model object colour, ambient IR or the receiver's angle; those are the real-world calibration issues.
  • A stopped motor is a software decision in the problem; the module itself only reports blocked.

Going deeper

Further reading