Ultrasonic sensor is not working with servo motor

Hi everyone,

I am currently developing a program that measures the distance from a person using an ultrasonic sensor and if the distance is less than 1.5m, I sound an alarm.

The program is working well; however, I want to add a servo motor so that the sensor can check multiple directions. The problem is that when I add the servo, the ultrasonic sensor stops working as well. In addition, if I increase the speed of the motor, then there is constant beeping.

Is this a hardware problem or is there an error in my code?

Thanks in advance

#include <Servo.h>
Servo myServo;
int pos = 0;
int trigPin=9;
int echoPin=8;
int pingTravelTime;
float distanceInCm;
int buzzer = 2;

void myFunction(){
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pingTravelTime= pulseIn(echoPin, HIGH);
}

void reaction (){
if (pingTravelTime < 9600){
  if (pingTravelTime < 5600){
    tone(buzzer, 1700);
    delay(50);
    noTone(buzzer);
  }else{
  tone(buzzer, 1600);
  delay(100);
  noTone(buzzer);
  }
}
}



void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
myServo.attach(7);
}




void loop() {

    myFunction();
    reaction();
    // waits 15ms for the servo to reach the position
  
  }
  // put your main code here, to run repeatedly:

Does the ultrasound sensor stop working, or the buzzer?

pulseIn returns an unsigned long, not a float.

Why don’t you add debug prints to find out what is going wrong?

This is the code with the servo motor.

#include <Servo.h>
Servo myservo;
int pos = 0;
int trigPin=9;
int echoPin=8;
int pingTravelTime;
float distanceInCm;
int buzzer = 2;

void myFunction(){
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pingTravelTime= pulseIn(echoPin, HIGH);
Serial.println(pingTravelTime);
}

void reaction (){
if (pingTravelTime < 9600){
  if (pingTravelTime < 5600){
    tone(buzzer, 1700);
    delay(50);
    noTone(buzzer);
  }else{
  tone(buzzer, 1600);
  delay(100);
  noTone(buzzer);
  }
}
}



void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
myservo.attach(7);
}




void loop() {
    for (pos = 0; pos <= 180; pos += 2) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15); 
    myFunction();
    reaction();  
    // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 2) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);      
    myFunction();
    reaction();// waits 15ms for the servo to reach the position
  }
    
    // waits 15ms for the servo to reach the position
  
  }
  // put your main code here, to run repeatedly:

The problem is definitely the ultrasound sensor. When the servo motor is at this speed, it randomly causes the buzzer to beep every 10 seconds (roughly), even though the readings are the same.

When I increase the speed even more, the readings become completely wrong, causing the buzzer to start beeping like crazy.

What happens when you run the code, but without having the servo connected?

How are you powering the servo?

Why haven’t you got more debug prints?

pulseIn does not return an int.

I changed the power supply and it works now!!!!

Thank you so much :))))))

alexoort:
I changed the power supply and it works now!!!!

Thank you so much :))))))

Good to know it worked for you and yup servo motors draw a lot of power. Btw I have once provided power from Arduino to small servo motor and it worked great.