How To Connect

diagram
  1. Connect the Vcc of Ultrasonic Sensor to pin to 5V PWR of Raspberry Pi
  2. Connect the trig pin of Ultrasonic Sensor to GPIO 18 of Raspberry Pi
  3. Connect the Gnd of Ultrasonic Sensor to Ground of Raspberry Pi
  4. Connect the Echo pin of Ultrasonic Sensor to 1K Resistor
  5. Connect 1K Resistor pin which is connected with GPIO 24 to Ground
  6. Connect 2K Resistor pin which is connected with 1K Resistor to Ground
  7. After completing the circuit connection, click the "Code" button, submit the code and you can change the position of object and observe the result in the display.

Connections Logs

    Raspberry Pi
    2K Resistor
    1k Resistor
    Ultrasonic Sensor
    Object

    CONNECTOR INFO
            import RPi.GPIO as GPIO
            import time
            # Set GPIO mode
            GPIO.setmode(GPIO.BCM)
            # Define GPIO pins
            trig_pin = 18
            echo_pin = 24
            # Setup GPIO pins
            GPIO.setup(trig_pin, GPIO.OUT)
            GPIO.setup(echo_pin, GPIO.IN)
            # Trigger pulse function
            def send_trigger_pulse():
                GPIO.output(trig_pin, True)
                time.sleep(0.00001)
                GPIO.output(trig_pin, False)
            # Wait for echo function
            def wait_for_echo(value, timeout):
                count = timeout
                while GPIO.input(echo_pin) != value and count > 0:
                    count -= 1
            # Calculate distance function
            def get_distance():
                send_trigger_pulse()
                wait_for_echo(True, 10000)
                start_time = time.time()
                wait_for_echo(False, 10000)
                end_time = time.time()
                pulse_duration = end_time - start_time
                distance = pulse_duration * 17150  # Speed of sound is approximately 343 meters/second
                distance = round(distance, 2)
                return distance
            # Main loop
            try:
                while True:
                    distance = get_distance()
                    print(f"Distance: {distance} cm")
                    time.sleep(1)
            except KeyboardInterrupt:
                GPIO.cleanup()