Home ยท Module 2 ยท Sensors

Sensors: how robots perceive

Each sensor below is a live simulation of a real component you'll use in this course. Drag, click, and watch the readings change.

๐Ÿ“ก HC-SR04 Ultrasonic Distance Sensor

Emits a 40 kHz pulse, measures echo time. distance = (echo_time ร— 343 m/s) / 2. Drag the obstacle to change distance.

Reading

Distanceโ€” cm
Echo timeโ€” ยตs
Statusโ€”

Wiring (your Pi car)

VCC  โ†’ 5V
GND  โ†’ GND
TRIG โ†’ GPIO 23
ECHO โ†’ GPIO 24 (via voltage divider)

Python snippet

import RPi.GPIO as GPIO, time
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0: t0=time.time()
while GPIO.input(ECHO)==1: t1=time.time()
dist_cm = (t1-t0) * 17150

๐Ÿ”ด IR Line-Follower Array

3 downward-facing IR sensors detect black vs. white. Drag the line to see how the array reports its position.

Sensor outputs

Leftโ€”
Centerโ€”
Rightโ€”
Decisionโ€”

Decision rule

if center: go straight
elif left:  turn left
elif right: turn right
else:       stop / search

A real line-follower extends this with a PID on the error between the line center and the sensor midpoint.

๐Ÿ”„ Rotary Encoder

A slotted disc + photo-interrupter counts ticks as the wheel turns. Click "Spin" or use the speed slider.

Controls

60

Reading

Ticks (total)0
Revolutions0.00
RPM (estimate)0
Distance @ r=3cm0.0 cm

Sensor fusion preview โ€” Real robots combine multiple sensors because each one is noisy or limited. Your Pi car will use the ultrasonic to detect obstacles, but it could add IR to follow a line and encoders to know how far it has traveled. A Kalman filter combines all three into one clean estimate of the robot's state.