Example Code for Arduino-Ultrasonic and Servo Debugging
Last revision 2026/01/05
Debug the ultrasonic sensor (for distance measurement) and servo motor (for sweeping motion) to ensure they function correctly before integrating into the obstacle-avoidance system.
Hardware Preparation
- Ultrasonic sensor: 1pc (included in the kit)
- Servo motor: 1pc (included in the kit)
- Romeo BLE controller: 1pc (included in the kit)
- Wires: Included in the kit
- Upper plate and sensor plate: Included in the kit
Software Preparation
- Arduino IDE (download from click here)
- Metro library: Download from Metro library
- Servo library: Included with the Arduino IDE.
Wiring Diagram
Connect the ultrasonic sensor and servo to the Romeo BLE controller as shown:

Other Preparation Work
- Install the upper plate and secure the ultrasonic sensor/servo as per STEP3: Install Upper Plate.
- Download the Metro library and add it to the Arduino IDE (Sketch → Include Library → Add .ZIP Library).
Sample Code
#include <Servo.h>
#include <Metro.h>
Metro measureDistance = Metro(50);
Metro sweepServo = Metro(20);
unsigned long actualDistance = 0;
Servo myservo; // create servo object to control a servo
int pos = 60;
int sweepFlag = 1;
int URPWM = 3; // PWM Output 0-25000US,Every 50US represent 1cm
int URTRIG= 10; // PWM trigger pin
uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01}; // distance measure command
void setup(){ // Serial initialization
myservo.attach(9);
Serial.begin(9600); // Sets the baud rate to 9600
SensorSetup();
}
void loop(){
if(measureDistance.check() == 1){
actualDistance = MeasureDistance();
// Serial.println(actualDistance);
// delay(100);
}
if(sweepServo.check() == 1){
servoSweep();
}
}
void SensorSetup(){
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]);
}
}
int MeasureDistance(){ // 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 distance=pulseIn(URPWM,LOW);
if(distance==50000){ // the reading is invalid.
Serial.print("Invalid");
}else{
distance=distance/50; // every 50us low level stands for 1cm
}
return distance;
}
void servoSweep(){
if(sweepFlag ){
if(pos>=60 && pos<=120){
pos=pos+1; // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
if(pos>119) sweepFlag = false; // assign the variable again
}else {
if(pos>=60 && pos<=120){
pos=pos-1;
myservo.write(pos);
}
if(pos<61) sweepFlag = true;
}
}
Result
- The servo will sweep smoothly from 60° to 120° and back.
- The ultrasonic sensor will measure distance every 50ms. Invalid readings will print "Invalid" to the serial monitor; valid readings will print the distance in centimeters.
Additional Information
- If the servo position is misaligned:
- Reinstall the servo wheel to adjust the starting position.
- Modify the
posvariable in the code to change the sweep range.
Was this article helpful?
