1

I was testing out my new Servo FS5106B. Here is the code I used:

#include <Servo.h>

Servo servo;
void setup()
{
    servo.attach(10); 
}

void loop()
{
    servo.write(0); 
    delay(1000); 
    servo.write(180);
    delay(1000);
}

After I upload the sketch, the console says:

Sketch uses 2872 bytes (1%) of your device memory. Total available 253952 bytes. Global variables use 161 bytes (1%) of the dynamic memory, leaving 8031 bytes for local variables. Maximum 8192

The delay between the rotations wasn't 1 second (as I was expecting), but 13 seconds (when I connect only a LED and use the delay function, everything works perfectly). Why is that happening?

P.S The power source for the servo is my Arduino-Mega 2560, wich outputs 5V. The model I use for wiring is: enter image description here

Edit 1: I tried to execute the program that @ChrisStratton suggested, here an updated code

#include <Servo.h>
#define led 22

Servo servo;

void setup()
{
    Serial.begin(9600);
    while(!Serial)
        delay(10);

    Serial.println("Start!");
    pinMode(led, OUTPUT);
    servo.attach(10);
}

void loop()
{
    digitalWrite(led, HIGH);
    servo.write(0);
    delay(1000);
    digitalWrite(led, LOW);
    servo.write(180);
    delay(1000);
}

When this program loads, servo does 2 rotations in 0.4 seconds and the LED goes high and low really fast, then anything changes for another 10 seconds, then again the LED goes high and low and the Servo does 2 rotations

  • 2
    Please give a small but working sketch. I can not compile this. The stall current of the FS5106B is about 1A, the Arduino 5V pin can not supply that. – Jot Jul 6 '17 at 14:15
  • It is critical that your post contain the exact code tested. Obviously the posted code cannot build, which means it could not have been tested. You need to use the "edit" button to post the exact code you tested, and you need to include a description of your wiring and the power source for the servo. But before you do any of that, make an experimental version which blinks an LED at 1 second intervals without any connection to a servo, and make sure that interval is approximately correct. – Chris Stratton Jul 6 '17 at 14:24
  • @Jot Changed my code – Philippe Jul 6 '17 at 14:38
  • @ChrisStratton Changed my code to the one I was testing with exactly – Philippe Jul 6 '17 at 14:39
  • And the rest of what is missing? – Chris Stratton Jul 6 '17 at 14:42
2

In the official Arduino examples, a servo motor is powered by the 5V pin of the Arduino board. In real life, that is a bad idea.
Your FS5106B has specified about 1A stall current. The average current is less than that, but the start peak current could indeed be 1A.
Could you try to use a seperate power supply of 5V (or 6V) for the servo motor ?

To check if the Arduino resets, you could do a Serial.begin(9600); in setup, directly followed by Serial.println("the sketch has started");

Some servo motors require different timing. That would not explain the 13 seconds delay, so I assume that your servo motor has normal timing.

Do you use a breadboard ? Sending a current peak through a breadboard (with bad contacts) can cause a lot of voltage spikes.

(To the other problem solvers: I'm ignoring the 13 seconds and assume that once in a while the servo motor is able to start, just a guess, I could be wrong).

[ADDED]
@ChrisStratton is trying to tell you this:

void setup()
{
  pinMode( LED_BUILTIN, OUTPUT); 
}

void loop()
{
  digitalWrite( LED_BUILTIN, HIGH);
  delay(1000); 
  digitalWrite( LED_BUILTIN, LOW);
  delay(1000);
}

The next step would be:

#include <Servo.h>

Servo servo;

void setup()
{
  Serial.begin(9600);
  Serial.println("The sketch has started");
  pinMode( LED_BUILTIN, OUTPUT);
  servo.attach(10); 
}

void loop()
{
  servo.write(0);
  digitalWrite( LED_BUILTIN, HIGH);
  delay(1000);

  servo.write(180);
  digitalWrite( LED_BUILTIN, LOW);
  delay(1000);
}

[ADDED2]
Or even more fun, with a counter (as @ChrisStratton wrote):

#include <Servo.h>

Servo servo;

unsigned int counter;

void setup()
{
  Serial.begin(9600);
  Serial.println("The sketch has started");
  pinMode( LED_BUILTIN, OUTPUT);
  servo.attach(10); 
}

void loop()
{
  servo.write(0);
  digitalWrite( LED_BUILTIN, HIGH);
  delay(1000);

  servo.write(180);
  digitalWrite( LED_BUILTIN, LOW);
  delay(1000);

  Serial.print("Still alive after ");
  Serial.print(counter);
  Serial.println(" seconds");
  counter += 2;   // two seconds per loop iteration
}
  • The serial output on start is a good idea, but if the servo motor is causing the arduino to brown out, depending on if the USB port / chip are also involved, the serial device may keep dissapearing and so subsequent output may not be obtained. It might be better to have the Arduino print out "still alive after " and a running count - that way the messages would indicate success in the case where they could be seen. This could then be run with the motor removed from the circuit, with it powered by an external supply having only a common ground, or (dubiously) with Arduino/USB power. – Chris Stratton Jul 6 '17 at 15:17
  • @ChrisStratton Tried your suggestion, modified my question. – Philippe Jul 6 '17 at 15:32
  • You were supposed to try it first without the servo connected. Also you don't seem to be monitoring the serial output Jot suggested, but it would be even better if you generated output in between each operation in the loop. – Chris Stratton Jul 6 '17 at 16:06
  • @ChrisStratton I tried it without the servo connected and it worked. I actually wrote it in my question > (when I connect a LED and use the delay function, everything works perfectly) – Philippe Jul 6 '17 at 17:09
  • Sounds like you have a power problem. As we've been saying, you shouldn't try to power a servo through an Arduino. – Chris Stratton Jul 6 '17 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.