Raspberry Pi Pico - Ultrasonic Sensor - LCD

This guide shows you how to measure distance with an ultrasonic sensor and display it on an LCD I2C using a Raspberry Pi Pico.

Raspberry Pi Pico ultrasonic Sensor LCD I2C

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×LCD I2C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Ultrasonic Sensor and LCD

Discover how to use the Ultrasonic sensor and LCD, exploring their pin connections, how they work, and how to program them in these guides.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Ultrasonic LCD

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-ultrasonic-sensor-lcd """ from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C from DIYables_MicroPython_Ultrasonic_Sensor import UltrasonicSensor import time # LCD configuration I2C_ADDR = 0x27 # I2C address of your LCD LCD_ROWS = 2 LCD_COLS = 16 # Initialize I2C for LCD i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) lcd.backlight_on() lcd.clear() # Ultrasonic sensor configuration sensor = UltrasonicSensor(trig_pin=27, echo_pin=26) sensor.set_detection_threshold(140) # Set detection threshold to 140 cm sensor.enable_filter(num_samples=20) # Enable filtering and set number of samples to 20 # Main loop function while True: sensor.loop() # Perform measurement cycle distance = sensor.get_distance() # Clear the LCD and display the distance lcd.clear() lcd.set_cursor(0, 0) lcd.print("Distance:") if not distance: lcd.set_cursor(0, 1) lcd.print("No object") else: # Display the distance formatted to two decimal places lcd.set_cursor(0, 1) lcd.print("{:.2f} cm".format(distance))

※ NOTE THAT:

Different manufacturers may use different I2C addresses for the LCD. In our example, we used the address 0x27, following the recommendation from the manufacturer DIYables.

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the ultrasonic sensor and LCD display according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-LCD-I2C”, then find the LCD I2C library created by DIYables.
  • Click on DIYables-MicroPython-LCD-I2C, then click Install button to install LCD I2C library.
Raspberry Pi Pico LCD I2C library
  • Search “DIYables-MicroPython-Ultrasonic-Sensor”, then find the Ultrasonic Sensor library created by DIYables.
  • Click on DIYables-MicroPython-Ultrasonic-Sensor, then click Install button to install Ultrasonic Sensor library.
Raspberry Pi Pico Ultrasonic Sensor library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the message in the Shell at the bottom of Thonny.
  • Place an object in front of the ultrasonic sensor
  • Check out the LCD display.

Code Explanation

Look at the comments in the code to understand each line.

※ NOTE THAT:

If the LCD displays nothing, check this guide: Troubleshooting LCD I2C

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!