Example Code for Raspberry Pi – URM09 Ultrasonic Sensor

Last revision 2026/01/20

This article offers example code for using the URM09 ultrasonic sensor with Raspberry Pi, detailing its features such as temperature compensation and dual-probe technology for accurate distance measurement, and showcasing its application in robotics and environmental sensing.

Introduction

This is an open dual-probe ultrasonic distance measurement module. Equipped with Gravity standard PH2.0-3P vertical mount interface, it outputs analog voltage, compatible with arduino, Raspberry Pi, and other controllers with logic level of 3.3 V or 5 V.

This module comes with temperature compensation to avoid ambient temperature from affecting its measurements. With its analog voltage value output, it can directly read the temperature value by ADC conversion, simplifying the operation and reducing the difficulty. Besides, this sensor has been tested that its effective measurement range for flat walls is 2 – 500 cm, its resolution is 1cm, and the error is about ±1%. Its dual-probe greatly reduces the detection blind area. And the on-board status indicator light is convenient to check the test progress.

Thanks to its small size, this convenient sensor that allows plug and play has strong environmental applicability, high accuracy, wide measurement range, quite suitable for outdoor environments, especially those with rapid temperature changes. It is also an excellent choice for robots to avoid obstacles automatically, car reversing alarms, doorbells, warning alarms, subway safety line prompts, bank and cash machine one-meter line prompts, and so on.

URM09 ultrasonic sensor adopts analog voltage output, so we can get the corresponding test distance through conversion. In this section, we will use Thonny Python IDE basic usage and routine adc to do a simple ultrasonic distance measurement.

Hardware Preparation

Wiring Diagram

  • Connect the Raspberry Pi correctly to devices such as the screen, power, keyboard and mouse.

  • Connect this module to analog port A0 on Raspberry Pi expansion board.

  • Find this position to connect the sensor correctly.

When we move the ultrasonic sensor, we can receive feedback to detect different distance values

Sample Code

  • Install Python dependency libraries and git, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’

sudo apt-get install build-essential python-dev python-smbus git

  • Install the drive library and program.

git clone https://github.com/DFRobotdl/111.git

  • Find SEN0307.py in the file below, open and run it.

  • Get result.

Was this article helpful?

TOP