Ultrasonic sensors can give you an accurate distance measurement. The transmitting side of the sensor sends out an ultrasonic pulse, which will bounce back off the target, to be caught by the receiving side of the transmitter. Knowing that the pulse travels at the speed of sound, we can determine how far away an object is by capturing the time between sending and receiving.
You can use your Raspberry Pi Zero to understand the ultrasonic sensor's data and report to you how far away an object is. Let's take a look!
For this recipe, we will use the following components
One 1 KOhm resistor
One 2 KOhm resistor
One tactile pushbutton switch
The Elego HC-SR04 Ultrasonic sensor (though any four-pin ultrasonic sensor should work)