You should use code tags as described here to publish python code. viewtopic.php?t=84477
Did a test here on a PI5 to verify the gpio behavior. Connected TRIG=GPIO23 and ECHO=GPIO24 with a 1k resistor (just in case of errors, prevents shorts). Then simplified the code you provided and write a 1 to TRIG gpio, read the ECHO and check if its 1. Then write a 0 to TRIG and check ECHO for 0. Works perfect.
Recommendation: run this test on your machine. Then check your breadboard connections, contacts are sometimes bad. Perhaps wires are broken. Especially check the ground connection of your voltage divider.
Test code:
Did a test here on a PI5 to verify the gpio behavior. Connected TRIG=GPIO23 and ECHO=GPIO24 with a 1k resistor (just in case of errors, prevents shorts). Then simplified the code you provided and write a 1 to TRIG gpio, read the ECHO and check if its 1. Then write a 0 to TRIG and check ECHO for 0. Works perfect.
Recommendation: run this test on your machine. Then check your breadboard connections, contacts are sometimes bad. Perhaps wires are broken. Especially check the ground connection of your voltage divider.
Test code:
Code:
"""test code, connect ECHO and TRIG gpio with a 1k resistor validate that written values to TRIG are read correctly by ECHO"""import lgpio as GPIOimport time# Set pinsTRIG = 23 # Associate pin 23 to TRIGECHO = 24 # Associate pin 24 to ECHO# Open the GPIO chip and set the GPIO directionh = 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(0.1) res = GPIO.gpio_read(h, ECHO) if res != 0: print("nope 0") time.sleep(0.1) GPIO.gpio_write(h, TRIG, 1) time.sleep(0.1) res = GPIO.gpio_read(h, ECHO) if res != 1: print("nope 1") time.sleep(0.1)# Main programif __name__ == '__main__': try: while True: get_distance() # Reset by pressing CTRL + C except KeyboardInterrupt: print("Measurement stopped by User") GPIO.gpiochip_close(h)Statistics: Posted by ghp — Tue Jun 03, 2025 8:54 am