Servo motor turns 360 degrees when turned on

Hello there. I am currently working on my fire extinguisher project. I am using a servo and 5 fire sensors. Everything works just fine for a few times. However after a few on and off, whenever I restarted it, the servo turns 360 degrees and it goes to all direction even though there is no fire. Can anyone help me with this issue?

Here's my code:

#include<Servo.h>
Servo servo1;
int pos = 0;

int flame[5];

int relay = 13;
void setup() {
  Serial.begin(9600);
  servo1.attach(9);
  pinMode(13, OUTPUT);
}

void loop() {
  flame[0] = analogRead(A0);

  flame[1] = analogRead(A1);

  flame[2] = analogRead(A2);

  flame[3] = analogRead(A3);

  flame[4] = analogRead(A4);

  
  Serial.print("0 = ");
  Serial.println(flame[0]);
  Serial.print("1 = ");
  Serial.println(flame[1]);
  Serial.print("2 = ");
  Serial.println(flame[2]);
  Serial.print("3 = ");
  Serial.println(flame[3]);
  Serial.print("4 = ");
  Serial.println(flame[4]);
  Serial.println(" ");  
  Serial.println(" ");  

  if(flame[0] >= 500){
    servo1.write(20);
    delay(2000);
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
  }
  else if(flame[1] >= 500){
    servo1.write(60);
    delay(2000);
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
  }
  else if(flame[2] >= 500){
    servo1.write(90);
    delay(2000);
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
  }
  else if(flame[3] >= 500){
    servo1.write(120);
    delay(2000);
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
  }
  else if(flame[4] >= 500){
    servo1.write(160);
    delay(2000);
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
  }
  else if(flame[0] <500 && flame[1] <500 && flame[2] <500 && flame[3] <500 && flame[4] <500){
    servo1.write(90);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
  }
  delay(1000);
}

Hi beni,

which microcontroller-board are you using?
how is the servo powered?

servos pull quite a lot of current and can't be powered from the 5V-pin of an arduino Uno
the servo will start to move but it pulls so much current that the voltage-regulator will overheat.

A usual RC-servo can't turn 360 degress. maybe 210 degrees most of them 170 to 180 degrees.

There are continious-rotation-servos but those can't be controlled by the commands you use.

So please draw a picture of the wiring and write a much more detailed description of what your system is doing

best regards Stefan

When the problem happens, what does the Serial Monitor say?

I'm currently using Arduino Uno. Should I use external battery for the servo?

I'll attach the screenshot of the serial monitor on this post.

Hi benedict,

yes of course even small rc-servos must be powered from an external power-supply.
The GND of the external power-supply and the GND of the arduino must be connected to make the servo work properly.

There are some strange characters inbetween that occur at different places.
But there is nothing inside your code that could cause printing out these characters.

One possible reason ist that if the servo is powered from the Arduino the voltage-regulator has a almost break-down which causes the Arduino to a short malfunction.

So it is highly recommended to use servos from an external power-supply.

You seem to know only a few things about electronics. So it is even more important to **hand-**draw a schematic of your hardware. Or as an alternative posting pictures which are taken from vertical above so that it is very easy to recognise which wire is plugged in to which socket.

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.