Tom's Hardware

2022-08-20 07:09:20 By : Ms. Winnie zheng

Tom's Hardware is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission. Here’s why you can trust us.

By Les Pounder published 29 January 21

Take distance measurements with your Pico.

There are many ways to detect movement and distances with your electronics. In another tutorial, we showed how to use a PIR motion sensor with Raspberry Pi Pico. Used in alarm systems, PIRs employ Infrared light to detect moving bodies in a room and are good for projects where, for example, you want users to wave their hands in front of a device to activate it. Our tutorial on how to build a Raspberry Pi handwashing timer uses a PIR.

Ultrasonic sensors, on the other hand, use pulses of sound and a simple calculation to determine the distance between themselves and the objects in front them. They are often used in robots to make sure the bot doesn’t walk or roll into an obstacle. We also used one in our Raspberry Pi Toilet paper reminder to tell when we’re running out of rolls.  In this tutorial we will use an ultrasonic distance sensor, an HC-SR04+, to quickly determine the distance of an object from our Raspberry Pi Pico. 

This build exclusively uses an ultrasonic sensor compatible with the 3V logic used on the Raspberry Pi Pico GPIO. The HC-SR04P and HC-SR04+ are compatible with 3V and 5V logic making them ideal for Raspberry Pi Pico, Pi and Arduino projects. 

2. Insert the HC-SR04P ultrasonic sensor into the breadboard. 

3. Connect the 3V3 pin of the Raspberry Pi Pico to the VCC pin of the ultrasonic sensor using a male to male jumper wire. 

4. Connect a GND pin on the Raspberry Pi Pico to the GND pin of the ultrasonic sensor using a jumper wire. 

5. Connect the Trigger pin of the ultrasonic sensor to GPIO pin 3 of the Raspberry Pi Pico. 

6. Connect the Echo pin from the ultrasonic sensor to GPIO pin 2 of the Raspberry Pi Pico. 

With the circuit built, connect your Raspberry Pi Pico and open the Thonny application.

1. Import the Pin class from the machine library and then import the utime library. The former is used to control GPIO pins, the latter is a library of time based functions.

2. Create two new objects, trigger and echo. These objects configure the GPIO pins of the Pico to be used with the ultrasonic sensor. For example our trigger pin is used to send a pulse of current, as such it is an output pin. The echo pin receives the reflected pulse, so echo is an input.

3. Create a function, ultra(), which will contain the code necessary to take a reading.

4. Pull the trigger pin low, to ensure that it is not active, then pause for two microseconds.

5. Pull the trigger pin high for five microsends before pulling the trigger pin low. This will send a short pulse from the ultrasonic sensor and then turn off the pulse.

6. Create a while loop to check the echo pin. If no echo pulse is received, update a variable, signaloff so that it contains a timestamp in microseconds.

7. Create another while loop, this time to check if an echo has been received. This will store the current timestamp in microseconds to the signalon variable.

8. Create a new variable, timepassed, which will store the value total time taken for the pulse to leave the sensor, hit the object and return back to the sensor as an echo.

9. Create a new variable, distance. This variable will store the answer of the equation. We multiply the journey time (timepassed) by the speed of sound (343.2 m/s, which is 0.0343 cm per microsecond) the product of that equation is divided by two as we do not need the total journey distance, just the distance from the object to the sensor.

10. Print a message to the Python Shell showing the distance.

11. Moving out of the function we now create a loop that will run the function every second.

Here’s the complete code:

Save the code to the Raspberry Pi Pico as code.py and click on the green arrow to run the code. In the Python Shell you will see the distance printed every second.

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

Get instant access to breaking news, in-depth reviews and helpful tips.

Thank you for signing up to Tom's Hardware. You will receive a verification email shortly.

There was a problem. Please refresh the page and try again.

Tom's Hardware is part of Future US Inc, an international media group and leading digital publisher. Visit our corporate site (opens in new tab) .

© Future US, Inc. Full 7th Floor, 130 West 42nd Street, New York, NY 10036.