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.
Pins
| Pin | What it is | Type |
|---|---|---|
| VCC | Supply 3.3–5 V | power |
| GND | Ground | ground |
| OUT | Digital out: HIGH when an object is close | io |
| Setting | Type | Range | Default |
|---|---|---|---|
thresholdCm | number | 2–30 | 20 |
In circuit.py:
ObstacleSensor()— Obstacle-avoidance sensor — $3.50- Pins: 3
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:
from wiring import *
pi = Pi()
obstacle = ObstacleSensor()
pi.V3_3 >> obstacle.vcc
obstacle.gnd >> pi.GND
obstacle.out >> pi.GPIO17On 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
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
OUTsafe 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
distanceCmfeed andthresholdCmprop model the comparator exactly:blockedis 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
- GPIO and the 40-pin header — digital inputs and pulls.
- The line sensor is the same optical pair aimed down instead of forward.
Further reading
- KY-032 obstacle-avoidance module — trimmer and wiring.
- LM393 comparator datasheet — the comparator inside the module.