Example Code for Arduino-Ultrasonic Scanner (Servo Control)
Last revision 2026/01/22
This article offers example code for using Arduino to control an ultrasonic scanner with servo control, detailing setup, operating modes, and precautions.
Hardware Preparation
- Arduino board: Quantity1
- URM37 V5.0 ultrasonic sensor (SKU:SEN0001): Quantity1
- Servo motor: Quantity1
- Jumper wires: Quantity several
Software Preparation
- Arduino IDE: Download from Arduino official website
- Servo library (included in Arduino IDE by default)
Wiring Diagram
No specific wiring diagram provided in original except connection instructions:
Pin1 VCC (URM V3.2) → VCC (Arduino)
Pin2 GND (URM V3.2) → GND (Arduino)
Pin4 PWM (URM V3.2) → Pin3 (Arduino)
Pin6 COMP/TRIG (URM V3.2) → Pin5 (Arduino)
Servo signal pin → Pin9 (Arduino)
Other Preparation Work
- Ensure the sensor's serial level is set to TTL mode (default) using the button method described in Getting Started section.
- Connect the hardware as per the wiring instructions.
- Break the secondary jumper on the left hand side of the sensor to avoid damage.
Sample Code
// # Editor : Jiang from DFRobot
// # Data : 24.07.2012
// # Product name:ultrasonic scanner Kit
// # Product SKU:SEN0001
// # Version : 0.2
// # Description:
// # The Sketch for scanning 180 degree area 4-500cm detecting range
// # Connection:
// # Pin 1 VCC (URM V3.2) -> VCC (Arduino)
// # Pin 2 GND (URM V3.2) -> GND (Arduino)
// # Pin 4 PWM (URM V3.2) -> Pin 3 (Arduino)
// # Pin 6 COMP/TRIG (URM V3.2) -> Pin 5 (Arduino)
// # Pin mode: PWM
// # Working Mode: PWM passive control mode.
// # If it is your first time to use it,please make sure the two jumpers to the right hand
// # side of the device are set to TTL mode. You'll also find a secondary jumper on
// # the left hand side, you must break this connection or you may damage your device.
#include <Servo.h> // Include Servo library
Servo myservo; // create servo object to control a servo
int pos=0; // variable to store the servo position
int URPWM=3; // PWM Output 0-50000us,every 50us represent 1cm
int URTRIG=5; // PWM trigger pin
boolean up=true; // create a boolean variable
unsigned long time; // create a time variable
unsigned long urmTimer = 0; // timer for managing the sensor reading flash rate
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x22,0xbb,0x01}; // distance measure command
void setup(){ // Serial initialization
Serial.begin(9600); // Sets the baud rate to 9600
myservo.attach(9); // Pin 9 to control servo
PWM_Mode_Setup();
}
void loop(){
if(millis()-time>=20){ // interval 0.02 seconds
time=millis(); // get the current time of programme
if(up){ // judge the condition
if(pos>=0 && pos<=179){
pos=pos+1; // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
if(pos>179) up= false; // assign the variable again
}
else {
if(pos>=1 && pos<=180){
pos=pos-1;
myservo.write(pos);
}
if(pos<1) up=true;
}
}
if(millis()-urmTimer>50){
urmTimer=millis();
PWM_Mode();
}
}
void PWM_Mode_Setup(){
pinMode(URTRIG,OUTPUT); // A low pull on pin COMP/TRIG
digitalWrite(URTRIG,HIGH); // Set to HIGH
pinMode(URPWM, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
void PWM_Mode(){ // a low pull on pin COMP/TRIG triggering a sensor reading
digitalWrite(URTRIG, LOW);
digitalWrite(URTRIG, HIGH); // reading Pin PWM will output pulses
unsigned long DistanceMeasured=pulseIn(URPWM,LOW);
if(DistanceMeasured==50000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
Distance=DistanceMeasured/50; // every 50us low level stands for 1cm
}
Serial.print("Distance=");
Serial.print(Distance);
Serial.println("cm");
}
Result
The servo scans from0 to180 degrees and back, measuring distance at each position. The distance data is sent to the serial port.

Additional Information
Servo Rotation Reference Table
| DEC | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| HEX | 0 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 0A | 0B | 0C | 0D | 0E | 0F |
| Degree | 0 | 6 | 12 | 18 | 24 | 29 | 35 | 41 | 47 | 53 | 59 | 65 | 70 | 76 | 82 | 88 |
| DEC | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
| HEX | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F |
| Degree | 94 | 100 | 106 | 112 | 117 | 123 | 129 | 135 | 141 | 147 | 153 | 159 | 164 | 170 | 176 | 182 |
| DEC | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | |
| HEX | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 2A | 2B | 2C | 2D | 2E | |
| Degree | 188 | 194 | 200 | 206 | 211 | 217 | 223 | 229 | 235 | 241 | 247 | 252 | 258 | 264 | 270 |

Was this article helpful?
