Although we have covered this before, it's a good idea to familiarize (or re-familiarize) ourselves with the programming of the HC-SR04:
- Open up Thonny from
Application Menu
|Programming
|Thonny Python IDE
. - Click
New
to create a new file. - Type the following:
from gpiozero import DistanceSensor from time import sleep distance_sensor = DistanceSensor(echo=18, trigger=17) while True: print('Distance: ', distance_sensor.distance*100) sleep(1)
- Save the file as
distance-sensor-test.py
. - Run the code.
- Place your hand in front of the distance sensor. You should see the following in the shell (depending on how far your hand is from the distance sensor):
Distance: 5.05452024001
- As you move your hand closer to, or farther away from, the distance sensor, the value will change. This code is pretty self explanatory. The
distance_sensor = DistanceSensor(echo=18, trigger=17)
line sets up adistance_sensor
object of class typeDistanceSensor
, with the appropriate...