In this article we connect an Ultrasonic ranging module to a raspberry Pi Pico
Ultrasonic ranging module HC – SR04 provides 2 cm – 400 cm non-contact measurement function, the ranging accuracy can reach to 3 mm. The modules includes ultrasonic transmitters, receiver and control circuit.
The basic principle of work:
(1) Using IO trigger for at least 10 us high level signal,
(2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
(3) If the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning.
Test distance = (high level time×velocity of sound (340M/S) / 2,
Working Voltage – DC 5 V
Working Current – 15mA
Working Frequency – 40 Hz
Max Range – 4 metres
Min Range – 2 centimetres
Measuring Angle – 15 degrees
Trigger Input Signal – 10uS TTL pulse
Echo Output Signal – Input TTL lever signal and the range in proportion
Dimension – 45*20*15 mm
The Timing diagram is shown below. You only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo.
The Echo is a distance object that is pulse width and the range in proportion .
You can calculate the range through the time interval between sending trigger signal and receiving echo signal.
Formula: uS / 58 = centimeters or uS / 148 =inch; or: the range = high level time * velocity (340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
|Pico||Raspberry Pi Pico Development Board|
|Connecting cables||Aliexpress product link|
To connect the display to your Raspberry PI Pico requires just 4 wires, you just need to wire the Vcc and GND PINs from display to VBuS and a GND PINs of RPI Pico, then Echo and Trig PINs from the module to suitable SDA and SCL PINs from Raspberry PI Pico
Here is a layout in fritzing to show this
I use thonny for all development
# The distance is captured by a Sharpir sensor and displayed on the screen # Insert HC-SR04 to CN6 import utime import os from machine import Pin trigger = Pin(16, Pin.OUT) echo = Pin(17, Pin.IN) # creates a function while True: print("start") # turns off trigger, then waits 2 microseconds trigger.low() utime.sleep_us(2) # turns on trigger for 5 microseconds trigger.high() utime.sleep_us(5) trigger.low() # creates a loop that checks the echo pin - if nothing is received, updates a variable called signaloff while echo.value() == 0: signaloff = utime.ticks_us() # creates another loop that checks echo pin - if something is received, updates a variable called signalon while echo.value() == 1: signalon = utime.ticks_us() # creates a variable called timepassed which stores the value of the time for the pulse to return as an echo timepassed = signalon - signaloff # creates a variable called distance to store value of timepassed as a distance in centimetres distance = (timepassed * 0.0343) / 2 # prints distance in console print("The distance from object is ", distance, "cm") utime.sleep_ms(2000)
In the repl window I saw this is
The distance from object is 35.96355 cm start The distance from object is 23.78705 cm start The distance from object is 25.57065 cm start The distance from object is 15.5379 cm start The distance from object is 15.79515 cm start The distance from object is 11.85065 cm start The distance from object is 5.64235 cm