# Servo Writing late

**URL:** <https://forum.arduino.cc/t/servo-writing-late/1190895>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [November 19, 2023, 9:46am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895 "2023-11-19T09:46:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 9:46am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/1 "2023-11-19T09:46:32Z")

</div>

Hello 👋  
I have connected 4 Servos with Ultrasonic sensor by using Arduino UNO.  
PROBLEM : **When a object is detected by Ultrasonic sensor, 3 servos rotate fast and then other remaining one rotates at the last**  
I am using MG995 Servo Moters.  
PLEASE HELP ME

The code is ⏬

```arduino
#include <Servo.h>

const int TRIG_PIN = 6;  
const int ECHO_PIN = 7;  
const int SERVO_PIN = 9; 
const int DISTANCE_THRESHOLD = 34; 

Servo servo; 

float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);  
  servo.attach(SERVO_PIN);   
  servo.write(0);
}

void loop() {
  
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  
  duration_us = pulseIn(ECHO_PIN, HIGH);
  
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    servo.write(180); 
  else
    servo.write(0);  

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 19, 2023, 9:49am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/2 "2023-11-19T09:49:14Z")

</div>

> [@parththakurscsh2008](#):
>
> I have connected 4 Servos with Ultrasonic sensor by using Arduino UNO.

Only a single servo is defined in your code. Please post the code that controls 4 servos and shows your problem

Where do the servos get their power from ?

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 9:50am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/3 "2023-11-19T09:50:57Z")

</div>

I don't have a code of 4 servo and I don't know coding  
the servos get power from a 6V 10Ah DC Battery

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 9:52am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/4 "2023-11-19T09:52:59Z")

</div>

they were working earlier

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 19, 2023, 9:53am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/5 "2023-11-19T09:53:41Z")

</div>

When you said

> [@parththakurscsh2008](#):
>
> 3 servos rotate fast and then other remaining one rotates at the last

I thought that you were describing a problem but seems that it is a requirement

Do you want 3 servos to move through the same angle then for a 4th one to move through the same angle ?

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 9:54am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/6 "2023-11-19T09:54:26Z")

</div>

no  
the all 4 servos should rotate 180 at one time  
no servo should be late

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 19, 2023, 9:56am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/7 "2023-11-19T09:56:21Z")

</div>

> [@parththakurscsh2008](#):
>
> no servo should be late

but you said

> [@parththakurscsh2008](#):
>
> then other remaining one rotates at the last

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 9:57am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/8 "2023-11-19T09:57:29Z")

</div>

its the problem bro 🤣

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 19, 2023, 9:58am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/9 "2023-11-19T09:58:58Z")

</div>

Firstly, I am not your "bro"

I don't know what you want the code to do. Please try describing it again

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [November 19, 2023, 10:00am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/10 "2023-11-19T10:00:42Z")

</div>

Hello [parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)

Post a timing diagram to show what happends and when.

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:05am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/11 "2023-11-19T10:05:20Z")

</div>

OK  
I am using 4 Servo and 2 Ultrasonic sensors in my project.  
The project was working before :  
When a Object comes in front of the Ultrasonic sensor, the 4 servos rotate 180 at a time. No servo was late.

The problem in project :  
When a Object comes in front of the Ultrasonic sensor, the 3 servos rotate 180 but 1 servo is late. It rotates when 3 servos are rotated.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 19, 2023, 10:07am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/12 "2023-11-19T10:07:42Z")

</div>

In order to do what you describe you must have some code

**HOLD ON !**  
Have you, by any chance got all 4 servos connected to pin 9 of the Arduino ?

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:07am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/13 "2023-11-19T10:07:43Z")

</div>

and I dont think its a servo problem  
When all the servos comes to 0 degree, the servo which was rotating late, it come s back 0 normally, but a different servo comes at 0 late.

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:08am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/14 "2023-11-19T10:08:30Z")

</div>

I have connected all servos signal pin in the 9 pin

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:10am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/15 "2023-11-19T10:10:18Z")

</div>

![WIN_20231119_15_39_22_Pro](https://europe1.discourse-cdn.com/arduino/original/4X/c/a/3/ca349d415996b02b3c549459f096b0d45469f237.jpeg)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 19, 2023, 10:12am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/16 "2023-11-19T10:12:13Z")

</div>

Then no wonder you have a problem  
Connect each servo to its own pin (any except pins 0 or 1)  
Define 3 more servos in the same way that the first one is but with different names  
attach() each of the new servos using their pin numbers  
write() to each of the servos when you want them to move

There are neater ways to do this but get the clumsy method working first

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:13am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/17 "2023-11-19T10:13:11Z")

</div>

ok I am doing

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:21am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/18 "2023-11-19T10:21:20Z")

</div>

Hey  
I have made the code But I am having error  
**Compilation error: 'else' without a previous 'if'**

```arduino
#include <Servo.h>

const int TRIG_PIN = 6;  
const int ECHO_PIN = 7;   
const int DISTANCE_THRESHOLD = 34; 

Servo servo1; 

Servo servo2;

Servo servo3;

Servo servo4;

float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);  
  servo1.attach(8);
  servo2.attach(9);
  servo3.attach(10);
  servo4.attach(11);   
  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);

}

void loop() {
  
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  
  duration_us = pulseIn(ECHO_PIN, HIGH);
  
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    servo1.write(180);
    servo2.write(180);
    servo3.write(180);
    servo4.write(180); 
  else
    servo1.write(0); 
    servo2.write(0); 
    servo3.write(0);
    servo4.write(0);

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

```

---

<div class="post-metadata">

**Author:** ![parththakurscsh2008](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@parththakurscsh2008](https://forum.arduino.cc/u/parththakurscsh2008)\
**Post date:** [November 19, 2023, 10:40am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/19 "2023-11-19T10:40:53Z")

</div>

I done this and uploaded the code  
The problem of servos while writing to 180 degree is not solved  
but, the problem of servos while writing to 0 is solved.  
🙂

I noticed one thing that when I changed the code that only 3 servos will rotate,  
THEY WORKED PERFECTLY

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [November 19, 2023, 11:24am UTC](https://forum.arduino.cc/t/servo-writing-late/1190895/20 "2023-11-19T11:24:50Z")

</div>

Hi, @parththakurscsh2008

> [@parththakurscsh2008](#):
>
> I have connected all servos signal pin in the 9 pin

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?  
Hand drawn and photographed is perfectly acceptable.  
Please include ALL hardware, power supplies, component names and pin labels.

Please show us how you have all this connected together.

Thanks.. Tom... 😀 👍 ☕ 🇦🇺

[Next page](https://forum.arduino.cc/t/servo-writing-late/1190895.md?page=2)
