Example Code for Arduino-Distance and Temperature Measurement

Last revision 2026/01/07

Passive measurement of distance and temperature. This example is the ultrasonic passive measurement of distance and module temperature and printing on the serial port.

Software Preparation

Download URM09 library. (About how to install the library?)

Wiring Diagram

Connect the module to UNO via I2C interface, shown as below.

Connection

Other Preparation Work

Ensure the module is connected correctly to the Arduino UNO via the I2C interface (VCC to 5V, GND to GND, C to SCL, D to SDA) and the URM09 library is installed in the Arduino IDE.

Sample Code

/*!
 * @file PassiveMesure.ino
 * @brief Passive measurement of distance and temperature
 * @n This example is the ultrasonic passive measurement of distance and module temperature and printing on the serial port
 * @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license The MIT License (MIT)
 * @author ZhixinLiu([email protected])
 * @version version  V1.2
 * @date 2020-9-30
 * @url https://github.com/DFRobot/DFRobot_URM09
 */
#include "DFRobot_URM09.h"

/* Create a URM09 object to communicate with IIC. */
DFRobot_URM09 URM09;

void setup() {
  Serial.begin(115200);
  /**
   * I2c device number 1-127
   * When the i2c device number is not passed, the default parameter is 0x11
   */
  while(!URM09.begin()){
    Serial.println("I2C device number error ");
    delay(1000);
  }
  /**
   * 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,150 
   *  MEASURE_RANG_500             Ranging from 500 
   *  MEASURE_RANG_300             Ranging from 300 
   *  MEASURE_RANG_150             Ranging from 150 
  */
  URM09.setModeRange(MEASURE_MODE_PASSIVE ,MEASURE_RANG_500);   
  delay(100);
}

void loop() {
  URM09.measurement();                         // Send ranging command 
  delay(100);
  int16_t dist = URM09.getDistance();          // Read distance
  float   temp = URM09.getTemperature();       // Read temperature
  Serial.print(dist, DEC); Serial.print(" cm------"); 
  Serial.print(temp, 1); Serial.println(" C");
  delay(100);
}

Result

Result

Was this article helpful?

TOP