PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Sensors

People counter

MediumBudget $7Python · gpiozero

An HC-SR04 (TRIG GPIO23, ECHO GPIO24 through a divider, or the 3.3 V-safe module) looks across a doorway at the wall, 120 cm away. Someone in the doorway reads under 60 cm. Count each person once — when the reading drops under 60 cm — and print the new total:

Count: 1

The test sends people through at 1 s, 3 s, 4 s (close behind the second, with half a second of clear doorway between), 7 s, and one who stops in the doorway from 10 s to 13 s (still one person). Read at least every 0.1 s — gpiozero's default 9-reading smoothing is too slow here, so pass queue_len=1.

More in Sensors