HC-SR04 + ServO

Hello guys, i am facing a problem when i tried using the servo and the ultrasonic together. It is giving me only 1 value all the time.

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define TRIGGER_PIN  51
#define ECHO_PIN     49

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
 
  myservo.attach(53);
  
  
  
  
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
    
  myservo.write(30);
  unsigned int uS = sonar.ping();
  
  delay(50);    // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
   uS;// Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm1");
  delay(2000);
  
  myservo.write(75);
  
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  uS;
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm2");
  delay(2000);
  
myservo.write(130);
  

   delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
   uS;
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm3");
  delay(2000);
}

   uS;// Send ping, get ping time in microseconds (uS).

Does this line do what the comment says ?

Is the distance correct if the program does not use the servo library and just does pings ?

Can you confirm that you are running the program on a Mega ?

The code for Ultrasonic and Servo, work without any issue independently. But when i added them together, it started giving me a single value. I added a delay (up to 10s) between two independent pings, yet it did not work as the servo changed direction.

yes, it is on a MEGA.

It is giving me only 1 value all the time

It should be no surprise that the value is the same for cm1, cm2 and cm3 because you only call the sonar.ping() method once each time through loop() and use the value returned in each distance calculation. I assume though that the reported distance does not vary at all.

The code for Ultrasonic and Servo, work without any issue independently.

The obvious inference is that the 2 libraries are conflicting but I don't think they use the same timers and you are not using PWM which is a common problem when using servos.

Do servos require PWM pins?

I guess the problem was fixed using 3 variables instead of one.

Thank you. :slight_smile:

Do servos require PWM pins?

No, but PWM uses timers and the Servo library user timer 1 which stops PWM working on some pins.

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

I guess the problem was fixed using 3 variables instead of one.

You only need one variable but you must call the ping() method 3 times to get 3 values. Presumably left, right and centre distances.

I needed to program the system which is a robot to turn right or left; so adding several variables would allow me to compare the data easily and from there, the robot should take the decision.

Thank you once again.