Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Raspberry Pi
100 min
Share

How to use Ultrasonic Sensor with Raspberry pi 5 ?

The ultrasonic sensor is a device that can measure distance by sending out sound waves and calculating how long it takes for them to bounce back. It can be used to measure the distance between two objects or to detect whether an object is in the way.

Project Video

Overview

In this tutorial, we will learn how to use the ultrasonic sensor to measure distance and print the values on the display with Raspberry Pi 5 .

Getting the Items

Raspberry Pi 5 Single Board Computer
Get Item
Ultrasonic Sensor (HC-SR04)
Get Item
Half-size Breadboard
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the Ultrasonic Sensor and the Raspberry pi 5 as shown in the image below.

Connections from the Raspberry pi 5 to the breadboard:

• Raspberry pi 5 GPIO GND pin → Breadboard ground line

• Raspberry pi 5 GPIO 5V pin → Breadboard 5V line

Connections from the Arduino to the Ultrasonic Sensor:


• Raspberry pi 5 GPIO VCC pin → Ultrasonic Sensor VCC pin (+ pin)


• Raspberry pi 5 GPIO pin 23 → Ultrasonic Sensor Trig pin


• Raspberry pi 5 GPIO pin 24 → Ultrasonic Sensor Echo pin


• Raspberry pi 5 GPIO GND pin → Ultrasonic Sensor GND pin (- pin)

Coding

Now on your Raspberry Pi, click on the menu, then choose programming, then open the Thonny ide program.

Now copy the code below into it. The function of this code is to make the Raspberry Pi 5 board measures the distance using the ultrasonic sensor and print the value on a window, and the word ("Hi") will be printed if the object in front of the sensor is closer than 20 cm, and the window will print the word ("Bye") if the object is more than 30 cm away from the sensor .

from gpiozero import DistanceSensor  # Import the DistanceSensor class from the gpiozero library

import tkinter as tk  # Import the tkinter library for creating the GUI

from tkinter import font  # Import the font module from tkinter for customizing the font

from time import sleep  # Import the sleep function from the time module for delay

# Initialize the ultrasonic sensor

sensor = DistanceSensor(echo=24, trigger=23, max_distance=5)

# Initialize the Tkinter window

window = tk.Tk()

window.title("Distance Measurement")

custom_font = font.Font(size=30)  # Create a custom font object with size 30

window.geometry("800x400")  # Set the dimensions of the window

distance_label = tk.Label(window, text="Distance: ", anchor='center', font=custom_font)

# Create a label to display the distance, centered text, and use the custom font

distance_label.pack()  # Add the label to the window

def measure_distance():

   distance = int(sensor.distance * 100)  # Measure the distance and convert it to an integer

   #distance_label.config(text="Distance: {} cm".format(distance))  # Update the distance label with the new distance

   

   if distance < 20:

       distance_label.config(fg="red", text="Distance: {} cm\nHi!".format(distance))

       # If the distance is less than 20, set the label text to display "Hi!" in red

   elif distance > 30:

       distance_label.config(fg="blue", text="Distance: {} cm\nBye!".format(distance))

       # If the distance is greater than 30, set the label text to display "Bye!" in blue

       

   window.after(1000, measure_distance)  # Schedule the next measurement after 1 second

   

# Start measuring distance

measure_distance()

# Run the Tkinter event loop

window.mainloop()

Testing it Out

Now run the code, you will find that the Raspberry Pi 5 board measures the distance using the ultrasonic sensor and displays the value on a window, and the word ("Hi") will be printed if the object in front of the sensor is closer than 20 cm, and the window will print the word ("Bye") if the object is more than 30 cm away from the sensor.

Resources

No items found.