Interfacing an ultrasonic sensor with the RPZ
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!
Getting ready
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)
How to do it...
- The following diagram shows how to connect the switch, resistors, and sensor to the Raspberry Pi Zero:
- The Python code for setting up this recipe is quite simple. Enter the following code as...