Click on the components to start interacting it with in the connection area
Hover over the raspberry Pi connections to get informations about various connections pin
Double click on various connections points to make connections
Double click on the empty space in the connection area to extend and bend wire connections
How To Connect
Connect the Vcc of Ultrasonic Sensor to pin to 5V PWR of Raspberry Pi
Connect the trig pin of Ultrasonic Sensor to GPIO 18 of Raspberry Pi
Connect the Gnd of Ultrasonic Sensor to Ground of Raspberry Pi
Connect the Echo pin of Ultrasonic Sensor to 1K Resistor
Connect 1K Resistor pin which is connected with GPIO 24 to Ground
Connect 2K Resistor pin which is connected with 1K Resistor to Ground
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
Hover over a component to see its description.
CONNECTOR INFO
import RPi.GPIO as GPIOimport time# Set GPIO modeGPIO.setmode(GPIO.BCM)# Define GPIO pinstrig_pin = 18echo_pin = 24# Setup GPIO pinsGPIO.setup(trig_pin, GPIO.OUT)GPIO.setup(echo_pin, GPIO.IN)# Trigger pulse functiondef send_trigger_pulse(): GPIO.output(trig_pin, True) time.sleep(0.00001) GPIO.output(trig_pin, False)# Wait for echo functiondef wait_for_echo(value, timeout): count = timeout while GPIO.input(echo_pin) != value and count > 0: count -= 1# Calculate distance functiondef 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 looptry: while True: distance = get_distance() print(f"Distance: {distance} cm") time.sleep(1)except KeyboardInterrupt: GPIO.cleanup()