Hi
Could somebody tell me how to use an ultrasonic sensor with a Raspberry Pi 4?
Hello
Project: Measure distances with an ultrasonic sensor and Raspbery Pi 4
Hardware required:
1 HC-SR04 ultrasonic sensor
1 breadboard
1 1K ohm resistor
1 2k ohm resistor
Wiring Circuit:
First place the ultrasonic sensor on the bredboard (see image).
Next, connect the four pins of the sensor to the pins of the Raspberry Pi through the breadboard.
Connect the GND pin with the GND pin.
Connect Trigger pin to GPIO pin 25
Connect the Echo pin to GPIO pin 23 using a voltage divider. For this connect one end of the 1K ohm resistor to the Echo pin, and the other end to a 2k resistor. Connect the other end of the 2k ohm resistor to the Raspberry Pi's GND.

Script
from gpiozero import DistanceSensor from time import sleep sensor=DistanceSensor(23,25) while True: print('Distance: ', sensor.distance*100, 'cm') sleep(1)