Hello and welcome back. In this tutorial, we will learn how to use an Ultrasonic Sensor with the Raspberry PI Pico board. Also, I used an LCD display for displaying those values. Therefore, we can also learn how to use an LCD display with Raspberry Pi. For that, I used python language and Thony ide for that. If you are a beginner with this raspberry pi, use this link for that.
Mainly we can use this sensor to measure distance. For that, there are two parts. That is the transmitter and the receiver. The transmitter transmits the wave, and when this wave hits an obstacle and returns, the wave is captured by the receiver. With this sensor, we can see how long it takes for this wave to hit an obstacle and come back. If you want to know more about this sensor, please use this link.
We can print text and characters on these displays. Also, we can buy these in different sizes and a 16 * 2 display is used in this tutorial. Also, I used the I2C module to drive this display. Therefore, we can operate this display using only 4 wires. If you want to get more information about these displays, please use this link.
OK, let’s do this project step by step. The required components are given below.
- Raspberry Pi Pico board x 1 — Amazon / Our Store
- Ultrasonic sensor x 1 — Amazon / Our Store
- LCD display x 1 — Amazon / Our Store
- I2C module x 1 — Amazon / Our Store
- Breadboard x 1 — Amazon / Our Store
- Jumper wires — Amazon / Our Store
Firstly, identify these components.
Secondly, connect the Raspberry Pi Pico board and the ultrasonic sensor to the breadboard.
Thirdly, connect the ultrasonic sensor to the Raspberry Pi board using the jumper wires. The circuit diagram is below.
Now, solder the I2C module to the LCD display. Then connect it to the Raspberry Pi board.
Next, connect the Raspberry Pi board to your computer. Now, let’s create the program for this project. It is as follows.
#Import the library files from i2c_lcd import I2cLcd from lcd_api import LcdApi from machine import Pin,I2C import time Trig = Pin(2,Pin.OUT)#Include the Trig pin Echo = Pin(3,Pin.IN)#Include the Echo pin i2c = I2C(0,scl=Pin(5), sda=Pin(4), freq=100000)#Include the I2C pins I2C_ADDR = i2c.scan()#Scan I2C address lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)#Initialize the I2C module #Get the distance def distance(): Trig.value(0) time.sleep_us(4) Trig.value(1) time.sleep_us(10) Trig.value(0) while Echo.value() == 0: low = time.ticks_us() while Echo.value() == 1: high = time.ticks_us() t = high - low return t #Starting text def startText(): lcd.move_to(0,0) lcd.putstr("Distance Loading") for i in range(0,15): lcd.move_to(i,1) lcd.putstr(".") time.sleep(0.3) startText()#Starting text lcd.clear()#Clear the display while True: dis = distance()#Get the distance cm = dis/29/2#Convert time to "cm" cm = int(cm) inch = dis/74/2#Convert time to "inch" inch = int(inch) lcd.move_to(0,0)#Set the cursor lcd.putstr("Distance - ") lcd.putstr(str(cm)+ "cm ")#Print the distance lcd.move_to(0,1)#Set the cursor lcd.putstr("Distance - ") lcd.putstr(str(inch)+ "inch ")#Print the distance
- Now, open this code and library files using the Thony IDE. Then, save these codes on the Raspberry Pi Pico board one by one. You must remember to save the main code as main.py. Also, save the library files with their own file names. You can download this code and library files using the links above.
Finally, run this code. Now you can use this project without a computer. OK, enjoy this project. The full video guide is below. So, see you in the next project or tutorial.