Example Code for Arduino-Read Detected Distance

Last revision 2025/12/18

This code tests the range finder function of the URM15 ultrasonic sensor.

Hardware Preparation

Software Preparation

  • Development tools: Arduino IDE
  • Library installation: Open Library Manager(Ctrl+Shift+I) in Arduino IDE, find and install DFRobot_RTU library.

Wiring Diagram

  • Diagram of Connecting to LEONARDO:
  • Diagram of Connecting to UNO:
  • Diagram of Connecting to MEGA:

Other Preparation Work

  • Connected table for sensor and MCU:
    | sensor pin | MCU | Leonardo/Mega2560/M0 | UNO | ESP8266 | ESP32 | microbit |
    | VCC | 3.3V/5V | VCC | VCC | VCC | VCC | X |
    | GND | GND | GND | GND | GND | GND | X |
    | RX | TX | Serial1 RX1 | 5 |5/D6(TX) | D2 | X |
    | TX | RX | Serial1 TX1 | 4 |4/D7(RX) | D3 | X |
  • Note: To run this demo, you must know the serial port configuration of the device (baud rate, data bit, check bit, stop bit).

Sample Code

/**************************************************************************************************************
 * @This code tests the range finder function of the URM15 ultrasonic sensor
 * @brief Change device ID of modbus slave. Each modbus slave has a unique device ID number in the range of 0x0000~0x00F7(0~247).
 * @ And there are two ways to change the device ID:
 * @n 1: If you don't know the device ID, you can change slave ID address by broadcast
 * @n address 0x00, this command will change the address of all the slaves on the bus to the ID to be set(when 
 * @n changing address with 0x00, it is better to connect only one device on the bus)
 * @n 2: If you know the device ID, change it directly 
 * @n note:To run this demo, you must know the serial port configuration of the device (baud rate, data bit, check bit, stop bit)
 * @n connected table
 * ---------------------------------------------------------------------------------------------------------------
 * sensor pin |             MCU                | Leonardo/Mega2560/M0 |    UNO    | ESP8266 | ESP32 |  microbit  |
 *     VCC    |            3.3V/5V             |        VCC           |    VCC    |   VCC   |  VCC  |     X      |
 *     GND    |              GND               |        GND           |    GND    |   GND   |  GND  |     X      |
 *     RX     |              TX                |     Serial1 RX1      |     5     |5/D6(TX) |  D2   |     X      |
 *     TX     |              RX                |     Serial1 TX1      |     4     |4/D7(RX) |  D3   |     X      |
 * ---------------------------------------------------------------------------------------------------------------
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @ author : [email protected]
 * @ data   : 26.10.2021
 * @ version: 1.0
**************************************************************************************************************/
#include "DFRobot_RTU.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif

#define   SLAVE_ADDR                ((uint16_t)0x0F)

#define   TEMP_CPT_SEL_BIT          ((uint16_t)0x01)
#define   TEMP_CPT_ENABLE_BIT       ((uint16_t)0x01 << 1)
#define   MEASURE_MODE_BIT          ((uint16_t)0x01 << 2)
#define   MEASURE_TRIG_BIT          ((uint16_t)0x01 << 3)

typedef enum{ 
  ePid,
  eVid,
  eAddr,
  eComBaudrate,
  eComParityStop,
  eDistance,
  eInternalTempreture,
  eExternTempreture,
  eControl
}eRegIndex_t;//Sensor register index

#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_RTU modbus(/*s =*/&mySerial);
#else
  DFRobot_RTU modbus(/*s =*/&Serial1);
#endif

volatile uint16_t cr = 0;
void setup() {
  Serial.begin(9600);
  while(!Serial){                                                     //Waiting for USB Serial COM port to open.
  }

#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
    mySerial.begin(19200);
#elif defined(ESP32)
  Serial1.begin(19200, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
  Serial1.begin(19200);
#endif
  delay(1000);
  cr |= MEASURE_MODE_BIT;//Set bit2 , Set to trigger mode
  cr &= ~(uint16_t)TEMP_CPT_SEL_BIT;//Select internal temperature compensation
  cr |= (uint16_t)TEMP_CPT_ENABLE_BIT;//enable temperature compensation
  modbus.writeHoldingRegister(/*id =*/SLAVE_ADDR, /*reg =*/ eControl, /*val =*/cr);
  delay(1000);
}

float  dist;
void loop() {
  cr |= MEASURE_TRIG_BIT;//Set trig bit
  modbus.writeHoldingRegister(/*id =*/SLAVE_ADDR, /*reg =*/ eControl, /*val =*/cr);
  delay(300);
  dist = modbus.readHoldingRegister(SLAVE_ADDR, eDistance);
  if(dist == 65535){
    Serial.println("out of range!");
  }else{
    Serial.print("distance = ");
    Serial.print(dist / 10, 1);
    Serial.println("cm");
  }
}

Result

Serial port output screenshot:

Was this article helpful?

TOP