0% found this document useful (0 votes)
72 views6 pages

04 - RPi Pico - Measure Distance With Ultrasonic Sensor HC-SR04

This document provides instructions for measuring distance using an ultrasonic sensor (HC-SR04) connected to a Raspberry Pi Pico. It includes a diagram of the sensor connections, an explanation of how ultrasonic distance measurement works, example code to initialize the sensor and calculate distance, and code for an optional LCD display to output results. The code samples show how to trigger ultrasonic pulses from the sensor, measure the echo return time to calculate distance, and print or display distances measured between 2-400cm.

Uploaded by

nuryadin2307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

04 - RPi Pico - Measure Distance With Ultrasonic Sensor HC-SR04

This document provides instructions for measuring distance using an ultrasonic sensor (HC-SR04) connected to a Raspberry Pi Pico. It includes a diagram of the sensor connections, an explanation of how ultrasonic distance measurement works, example code to initialize the sensor and calculate distance, and code for an optional LCD display to output results. The code samples show how to trigger ultrasonic pulses from the sensor, measure the echo return time to calculate distance, and print or display distances measured between 2-400cm.

Uploaded by

nuryadin2307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Raspberry Pi Pico: Measure Distance with Ultrasonic Sensor

HC-SR04

Required Components
▶ Required Hardware
◼ Raspberry Pi Pico
◼ 4 jumper wires
◼ HC-SR04 Ultrasonic Sensor

▶ LCD related requirements (optional)


◼ See video #3 (16x2 LCD with I2C Protocol)

Diagram

Without LCD Display

Raspberry Pi Pico HC-SR04

VCC VCC

GND GND

Trig GP14

Echo GP15
With LCD Display

Raspberry Pi Pico LCD 16x2 with I2C

VCC VCC

GND GND

SDA GP0

SCL GP1
HC-SR04 consists of two ultrasonic transducers.
One of them transmits ultrasonic sound pulse, while the other listen for the transmitted
pulses reflected by an object.

This sensor can measure distance to an object ranging from 2 to 400 centimeters.

Code

def calcDistance():
trigger.low()
utime.sleep_us(2)

trigger.high()
utime.sleep_us(10)
trigger.low()

while echo.value() == 0:
signaloff = utime.ticks_us()

while echo.value() == 1:
signalon = utime.ticks_us()

timepassed = signalon - signaloff

# speed of sound travel in air is approximately 343 meters/second


# that's equals to 34300 cm/second or 0.034300 cm/microsecond
distance = (timepassed * 0.0343) / 2

if (distance < 2) or (distance > 400):


distance = None

return distance

while True:
led.toggle()
d = calcDistance()
if (d):
print ("Distance : {:.1f} cm".format(d))
utime.sleep(1)

Final Version

from machine import Pin, I2C


import utime
from pico_i2c_lcd import I2cLcd

# Internal led init


led = Pin(25, Pin.OUT)
led.high()

# LCD section ------------------------------------


I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1))
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
def lcdPrint(col, row, s):
lcd.move_to(col, row)
lcd.putstr(s)

def lcdClear():
lcd.clear()

# HC-SR04 section --------------------------------


trigger = Pin(14, Pin.OUT)
echo = Pin(15, Pin.IN)

# Distance function
def calcDistance():
trigger.low()
utime.sleep_us(2)

trigger.high()
utime.sleep_us(10)
trigger.low()

while echo.value() == 0:
signaloff = utime.ticks_us()

while echo.value() == 1:
signalon = utime.ticks_us()

timepassed = signalon - signaloff


distance = (timepassed * 0.0343) / 2

if (distance < 0.1) or (distance > 450):


distance = None

return distance

# Main program -----------------------------------


def main():
lcdPrint(0, 0, "Hello ...")
lcdPrint(0, 1, "Simple Tutorials")
utime.sleep(2)

lcdClear()
lcdPrint(0, 0, "I'm counting...")

while True:
led.toggle()
d = calcDistance()
if (d):
print ("Distance : {:.1f} cm".format(d))
lcdPrint(0, 1, " ")
lcdPrint(0, 1, "d = {:.1f} cm".format(d))
utime.sleep(1)

if __name__ == "__main__":
main()

You might also like