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