Example Code for Arduino-Distance Measurement

Last revision 2025/12/26

This article provides a detailed guide to measuring distance with Arduino using the URM09 Ultrasonic Sensor, complete with preparation steps, a wiring diagram, and sample code to help convert sensor output into accurate distance data.

Wiring Diagram

Connnect the module to UNO via I2C interface, shown as below:

Connection Diagram.png

Sample Code

#define  MAX_RANG      (520)//the max measurement vaule of the module is 520cm(a little bit longer than  effective max range)
#define  ADC_SOLUTION      (1023.0)//ADC accuracy of Arduino UNO is 10bit

int sensityPin = A0;    // select the input pin 
void setup() {
  // Serial init
  Serial.begin(9600);
}
float dist_t, sensity_t;
void loop() {
  // read the value from the sensor:
 sensity_t = analogRead(sensityPin);
  // turn the ledPin on

 dist_t = sensity_t * MAX_RANG  / ADC_SOLUTION;//
 Serial.print(dist_t,0);
 Serial.println("cm");
 
 delay(500);

}

Result

The module will output analog voltage proportional to distance. After ADC sampled and output these data, the distance value can be obtained with a simple processing step.
Read Distance

Was this article helpful?

TOP