Hi,
I am trying to create a home automation system based on the Ultrasonic sensor HC-SR04. However, no matter what I do, the ECHO pin (GPIO24) just doesn't open, once the TRIG (GPIO23) fires the ultrasonic 10 microsecond pulse.
I am providing the code below and also attaching the current connection images.
The code below only works if I manually open GPIO24 in the first while loop to calculate the pulse_start and close it to calculate the pulse_end.
I would appreciate some help in this.
Code
import lgpio as GPIO
import time
# Set pins
TRIG = 23 # Associate pin 23 to TRIG
ECHO = 24 # Associate pin 24 to ECHO
# Open the GPIO chip and set the GPIO direction
h = GPIO.gpiochip_open(0)
GPIO.gpio_claim_output(h, TRIG)
GPIO.gpio_claim_input(h, ECHO)
def get_distance():
# Set TRIG LOW
GPIO.gpio_write(h, TRIG, 0)
time.sleep(2)
# Send 10us pulse to TRIG
GPIO.gpio_write(h, TRIG, 1)
time.sleep(0.00001)
GPIO.gpio_write(h, TRIG, 0)
# Start recording the time when the wave is sent
while GPIO.gpio_read(h, ECHO) == 0:
GPIO.gpio_write(h, ECHO, 1) #the only way the code works for ECHO Open
pulse_start = time.time()
# Record time of arrival
while GPIO.gpio_read(h, ECHO) == 1:
GPIO.gpio_write(h, ECHO, 0) #the only way the code works for ECHO Close
pulse_end = time.time()
# Calculate the difference in times
pulse_duration = pulse_end - pulse_start
# Multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
# Main program
if __name__ == '__main__':
try:
while True:
dist = get_distance()
print("Measured Distance = {:.2f} cm".format(dist))
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.gpiochip_close(h)
Warm Regards,
Sajal
I am trying to create a home automation system based on the Ultrasonic sensor HC-SR04. However, no matter what I do, the ECHO pin (GPIO24) just doesn't open, once the TRIG (GPIO23) fires the ultrasonic 10 microsecond pulse.
I am providing the code below and also attaching the current connection images.
The code below only works if I manually open GPIO24 in the first while loop to calculate the pulse_start and close it to calculate the pulse_end.
I would appreciate some help in this.
Code
import lgpio as GPIO
import time
# Set pins
TRIG = 23 # Associate pin 23 to TRIG
ECHO = 24 # Associate pin 24 to ECHO
# Open the GPIO chip and set the GPIO direction
h = GPIO.gpiochip_open(0)
GPIO.gpio_claim_output(h, TRIG)
GPIO.gpio_claim_input(h, ECHO)
def get_distance():
# Set TRIG LOW
GPIO.gpio_write(h, TRIG, 0)
time.sleep(2)
# Send 10us pulse to TRIG
GPIO.gpio_write(h, TRIG, 1)
time.sleep(0.00001)
GPIO.gpio_write(h, TRIG, 0)
# Start recording the time when the wave is sent
while GPIO.gpio_read(h, ECHO) == 0:
GPIO.gpio_write(h, ECHO, 1) #the only way the code works for ECHO Open
pulse_start = time.time()
# Record time of arrival
while GPIO.gpio_read(h, ECHO) == 1:
GPIO.gpio_write(h, ECHO, 0) #the only way the code works for ECHO Close
pulse_end = time.time()
# Calculate the difference in times
pulse_duration = pulse_end - pulse_start
# Multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
# Main program
if __name__ == '__main__':
try:
while True:
dist = get_distance()
print("Measured Distance = {:.2f} cm".format(dist))
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.gpiochip_close(h)
Warm Regards,
Sajal
Statistics: Posted by csajal — Tue Jun 03, 2025 4:23 am