Servo causes other devices to work erroneous.

Hello everyeone.
I’m working on a little robot project and I recently added a claw to my project. To control this claw I’m using the 5V output from my Arduino Uno. I also use this output for some other devices (2 distance sensors). Normally when I do testing with these sensor they give accurate results for distances even farther than 100cm. Also when I use some simple code on the servo it’s working without a problem. But if I want to combine the two in something like the code below the sensors can only measure to around 8cm.

#define ENABLE_A 0
#define ENABLE_B 1
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define trigpin 6
#define echopin 7
#define trigpin2 8
#define echopin2 9
#define MAX_DISTANCE 154
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 

void setup() {
   Serial.begin(115200);
    pinMode(trigpin, OUTPUT);
    pinMode(echopin, INPUT);
    pinMode(trigpin2, OUTPUT);
    pinMode(echopin2, INPUT);
    pinMode(ENABLE_A, OUTPUT);
    pinMode(ENABLE_B, OUTPUT);
    pinMode(IN1,OUTPUT);
    pinMode(IN2,OUTPUT);
    pinMode(IN3,OUTPUT);
    pinMode(IN4,OUTPUT);
    myservo.attach(10); 
    myservo.write(0); 
}
void loop() {  
long duration, distance;  
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration/2)/29.1;


if (distance < 10)
{
 myservo.write(45);
} 
else
{
myservo.write(0);
}
   Serial.print(distance);
     Serial.println(" cm");
}

Is this a problem related to power consumption? And more importantly, how do I fix this?

Thanks for your help in advance!

Charles

Is this a problem related to power consumption?

Whether it is, or isn't, will only be determined when you power the servo correctly - NOT having the Arduino power the servo.

Use 4 AA batteries, with ground from the battery pack also connected to the Arduino.

Thanks for your reply. I have a six AA battery pack and LM2596 DC-DC 3-35V adjustable step-down power Supply module . I use the battery pack to power my Arduino Uno direcly and the module for a Motor controller board. Can I use these to power my servo too?

Thanks in advance!

Can I use these to power my servo too?

Yes.

PaulS: Yes.

Yes - but first you should tell us what voltage you are using the module to step down to. If it is 6 volts or less, you should be good to go; if it is more, then you might damage your servo, depending on how much more it is. Most standard servos expect a voltage of 4.8-6.0 volts DC - and need to be budgeted about 1 amp of current.

So - if your module is currently outputting 4.8 to 6 volts, and can supply 1 amp of current (and has the extra capacity for your current motors and driver shield) - then you should be ok; otherwise, you will need to get another step-down module or some other regulator, and connect it in parallel to the battery, and use it to power the servo.