Raspberry Pi - Ultrasonic Sensor - LCD

This tutorial instructs you how to use Raspberry Pi to obtain the distance from an ultrasonic sensor and display it on an LCD I2C.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LCD I2C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Ultrasonic Sensor and LCD

If you are unfamiliar with ultrasonic sensor and LCD (including pinout, how it works, and how to program), the following tutorials can help you learn:

Wiring Diagram

The wiring diagram between Raspberry Pi and Ultrasonic Sensor LCD

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install lcddriver
  • Create a Python script file ultrasonic_lcd.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-ultrasonic-sensor-lcd from gpiozero import DistanceSensor from lcddriver import lcd from time import sleep # GPIO pins for ultrasonic sensor TRIGGER_PIN = 16 ECHO_PIN = 20 # I2C address 0x27, 16 columns, 2 rows LCD = lcd() # Create ultrasonic sensor instance ultrasonic_sensor = DistanceSensor(trigger=TRIGGER_PIN, echo=ECHO_PIN) def display_distance(distance): LCD.lcd_clear() LCD.lcd_display_string(f"Distance: {distance:.2f} m", 1) try: while True: distance = ultrasonic_sensor.distance display_distance(distance) sleep(0.5) # Sleep for 500 milliseconds except KeyboardInterrupt: pass finally: LCD.lcd_clear()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 ultrasonic_lcd.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

※ NOTE THAT:

  • If the LCD is not displaying anything, take a look at Troubleshooting on LCD I2C.
  • The code provided is for educational purposes. The ultrasonic sensor is very sensitive to noise, so if you plan to use it in a real-world application, you should filter the noise from the ultrasonic sensor. See how to filter noise for ultrasonic sensor for more information.

Video Tutorial