Example Code for Raspberry Pi-Distance and Temperature Measurement
Last revision 2026/01/07
Passive measurement of distance and temperature on Raspberry Pi. This example sets the I2C device number, configures the module in passive mode, sends a ranging command, and reads distance and temperature data to print on the terminal.
Software Preparation
Download URM09 library. (About how to install the library?)
Other Preparation Work
Ensure the Raspberry Pi has I2C enabled (via raspi-config), the module is connected correctly to the Raspberry Pi's I2C pins (VCC to 3.3V/5V, GND to GND, C to SCL (GPIO 3), D to SDA (GPIO 2)), and the URM09 library is installed.
Sample Code
# -*- coding:utf-8 -*-
'''
# PassiveMeasure.py
#
# Connect board with raspberryPi.
# Make board power and URM09 connection correct.
# Run this demo.
#
# Set the i2c communication device number.
# Set test mode and distance.
# Get temperature and distance data.
#
# Copyright [DFRobot](http://www.dfrobot.com), 2016
# Copyright GNU Lesser General Public License
#
# version V2.0
# date 2019-7-09
'''
import sys
import time
import URM09
sys.path.append("../..")
''' Create a URM09 object to communicate with I2C. '''
URM09 = URM09.DFRobot_URM09()
''' Set the i2c device number '''
URM09.begin(0x11)
time.sleep(0.1)
'''
# The module is configured in automatic mode or passive
# _MEASURE_MODE_AUTOMATIC // automatic mode
# _MEASURE_MODE_PASSIVE // passive mode
# The measurement distance is set to 500,300,100
# _MEASURE_RANG_500 // Ranging from 500
# _MEASURE_RANG_300 // Ranging from 300
# _MEASURE_RANG_150 // Ranging from 100
'''
URM09.SetModeRange(URM09._MEASURE_MODE_PASSIVE, URM09._MEASURE_RANG_500)
while(1):
''' Write command register and send ranging command '''
URM09.SetMeasurement()
time.sleep(0.1)
''' Read distance register '''
dist = URM09.i2cReadDistance()
''' Read temperature register '''
temp = URM09.i2cReadTemperature()
print("Temperature is %.2f .c " % (temp / 10))
print("Distance is %d cm " % dist)
time.sleep(0.1)
Result
Was this article helpful?
