ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Raspberry Pi & IoT Engineering• 6 min read

Measuring Distance with Ultrasonic Sensors (HC-SR04)

Measure physical proximity and distance using ultrasonic sound wave time-of-flight with DistanceSensor.

Calculating Distance from Speed of Sound

The HC-SR04 emits high-frequency 40kHz sound pulses and listens for the echo. With gpiozero: from gpiozero import DistanceSensor; sensor = DistanceSensor(echo=24, trigger=23); print(sensor.distance * 100, "cm").

ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which class in gpiozero handles HC-SR04 ultrasonic soundwave proximity measurements?