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
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
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
Reading
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.