1

I am working with two servomotors and both are having the same problem: right after i connect them to the arduino and upload the code, they do as the code says for a really small amount of time (0.1s maybe), and then they simply stop. If i keep connecting and reconnecting it again the servo answer to the code for a really small amount of time and then stop, as I said before.

#include <Servo.h>

#define SERVO 6 // Porta Digital 6 PWM

Servo s; // Variável Servo
int pos; // Posição Servo

void setup ()
{
  s.attach(SERVO);
  Serial.begin(9600);
  s.write(0); // Inicia motor posição zero
}

void loop()
{
  for(pos = 0; pos < 90; pos++)
  {
    s.write(pos);
  delay(15);
  }
delay(1000);
  for(pos = 90; pos >= 0; pos--)
  {
    s.write(pos);
    delay(15);
  }
}

I am using the code above, and it looks totally fine. Does anyone know what might be causing this problem?

  • 2
    In case it's a hardware problem, add a photo of setup + circuit schematic or reference to how you wired stuff up. Add model numbers of the servos and specs of the power supply that's powering the servos. Indicate what actually happens, rather than saying "they do as the code says", because nobody but you knows what you think the code does. Also report results that occur with only one or the other servo attached, and results when you change delay(15) to eg delay(150) – James Waldby - jwpat7 Aug 23 '16 at 3:35
1

Your code is correct. Check the following:

  • Arduino works from 5V, but most servos need 6V.
  • Even if your servo works from 5V, don't ever connect your servo power directly to Arduino pins. These pins are not able to provide adequate power for servo.
  • Check that grounds of both power supply for Arduino and power supply for servo are connected together.
| improve this answer | |
  • would it work if I attach it to a shield, instead of directly in the arduino pin? – Matheus Danella Aug 25 '16 at 1:03
  • 1
    it will work only if shield has its own power supply AND is dedicated for providing power its to servo – Dmitri Aug 25 '16 at 14:17

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.