Forum

How to use un ultra...
 
Notifications
Clear all

[Solved] How to use un ultrasonic sensor HC-SR04 with Raspberry Pi 4

2 Posts
2 Users
0 Likes
95 Views
Posts: 1
 Gerry
Guest
Topic starter
(@Gerry)
New Member
Joined: 2 months ago

Hi

Could somebody tell me how to use an ultrasonic sensor with a Raspberry Pi 4?

1 Reply
Posts: 5
(@raspi)
Active Member
Joined: 2 months ago

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)
Reply

Leave a reply

Author Name

Author Email

Title *

 
Preview 0 Revisions Saved
Share:
Scroll to Top