That video made by Lori Pfahler helped out so immensely.
Here is a short demonstration script for how to get the distances for all three sensors using AML-S905X-CC with HC-SR04 (5v echo output).
from datetime import datetime
from time import sleep
from gpiod import Chip, LINE_REQ_DIR_OUT, LINE_REQ_DIR_IN
forward_ultrasonic_sensor = {
"trigger": {"chip":"1","offset":91},
"echo": {"chip":"1","offset":95}
}
left_ultrasonic_sensor = {
"trigger": {"chip":"1","offset":92},
"echo": {"chip":"1","offset":81}
}
right_ultrasonic_sensor = {
"trigger": {"chip":"0","offset":6},
"echo": {"chip":"1","offset":82}
}
def get_distance(sensor):
trigger_chip = Chip(sensor["trigger"]["chip"])
trigger_lines = trigger_chip.get_lines([sensor["trigger"]["offset"]])
trigger_lines.request(consumer="",type=LINE_REQ_DIR_OUT)
echo_chip = Chip(sensor["echo"]["chip"])
echo_lines = echo_chip.get_lines([sensor["echo"]["offset"]])
echo_lines.request(consumer="", type=LINE_REQ_DIR_IN)
# Pull pin down to zero volts.
trigger_lines.set_values([0])
sleep(0.000002)
# Set pin high for just 10 microseconds to initiate the trigger
trigger_lines.set_values([1])
sleep(0.00001)
trigger_lines.set_values([0])
while echo_lines.get_values()[0] == 0:
continue
start_time = datetime.now()
while echo_lines.get_values()[0] == 1:
continue
end_time = datetime.now()
duration_in_seconds = (end_time - start_time).total_seconds()
distance_in_cm = (duration_in_seconds * 34444) / 2
return distance_in_cm
forward_distance = get_distance(forward_ultrasonic_sensor)
left_distance = get_distance(left_ultrasonic_sensor)
right_distance = get_distance(right_ultrasonic_sensor)
print(str(forward_distance))
print(str(left_distance))
print(str(right_distance))
Example output below. The left sensor has the most distance shown since that part of my desk is clear and there’s a good distance until the wall. the other two sensors are pretty crowded with toolboxes and such all around.
python3 distance_measurement_demo.py
14.070374000000001
68.147454
14.345926
The code provided above can be written better. For example you could trigger all three sensors at once via the gpiod library, and you could get all three values from all three sensors at once by getting the lines for all three at once via the gpiod library. Though the intent here is to give a basic example, not final code.
Here is the basic circuit which includes the sensors and the voltage dividers. Note: You do not need the voltage dividers if your sensor echo pin outputs 3.3v. Mine output 5v so I included the voltage dividers to bring voltage down to 3.4v
As mentioned in the guide, you’d use this command to get the chip and line for each pin number.
lgpio info PIN
So for example to get pin 8, you’d use this command. I put the output just below the command, it shows the chip and line.
lgpio info 8
Chip Line sysfs Name Pad Ref Desc
1 91 492 GPIOX_12 A6 UART_A_TX UART_TX_A