Does Servo and NewPing libraries hate each other?


I’m having a curious problem with my current project. I am moving a HC SR04 ultrasonic sensor mounted on a servo, acquiring the distance to the surrounding objects and finding the minimum distance and angle.

When testing the code, I unmount the sensor from the servo, so the servo moves freely and the sensor is fixed.

In the following code, when I comment the line in the loop that writes the position of the servo, the distances stored in data are constant and correct. However, if uncomment the line, the distances in data are no longer constant, some of them are correct but many others are wrong, notably lower than the correct value.

Can anybody cast some light on what can be happening?


#include <NewPing.h> // Ultrasound library to use with HC SR04 and
// In case of using this library and piezoelectrics beware of this issue with Multiple Definition of "__vector_7" Error
// If not using the ping_timer() method 
// There's an easy solution as long as you're not using ping_timer() and running v1.6 or later of NewPing. In the NewPing.h file, 
// TIMER_ENABLED is defined as "true" by default. Setting TIMER_ENABLED to "false" will disable all the timer functions from NewPing and the "_vector_7" error will be no more.

#define TRIGGER_PIN  7 // Pin defined as the trigger of HC SR04
#define ECHO_PIN     8 // Pin defined as the echo of HC SR04
#define MAX_DISTANCE 200 // Maximum range distance in cm
#define SERVO_PIN 3 // The servo is attached on this pin
#define GIRO 30
#define STEP 1

#include <Servo.h>

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Define sonar with the trigger, echo and maximum distance arguments.
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int uS = 0; // flight time in microseconds
int range = 0; // flight time in centimeters
int data[GIRO];
int min_dist = 0;
int index = 0;

void setup() {
  myservo.attach(SERVO_PIN);  // attaches the servo on pin SERVO_PIN to the servo object

void loop() {

  int ct = 0;
  for (pos = 0; pos <= GIRO; pos += STEP) {
    ct = ct + 1;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits for the servo to reach the position
    uS =; // Get the distance from the ultrasound sensor in ms
    range = uS / US_ROUNDTRIP_CM; // Convert to cm
    data[ct] = range;
//    Serial.print(pos);
//    Serial.print(":");
    Serial.print(" ");
//  minimo(data,&index,&min_dist);
//  Serial.print(index);
//  Serial.print("n");
//  Serial.print(min_dist);
//  Serial.print("n"); 

//  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
//    myservo.write(pos);              // tell servo to go to position in variable 'pos'
//    delay(15);                       // waits 15ms for the servo to reach the position
//  }

It is quite possible - they may both be using the same hardware Timer.

Try the ServoTimer2 library


Thank you Robin2, I'll try that.