2

I'm trying to programme a servo motor to reach certain positions. I have a 360 degrees servo motor, so I can not use the Servo library and I can only control the speed. Therefore I want to regulate the time for the pulse. The setup is simple, 5 V, GND and pin 9 from the Arduino to the motor.

int servo1 = 9;

void setup{
    Serial.begin(9600);
    pinMode(servo1, OUTPUT);
}
void loop{
    digitalWrite(servo1, HIGH);
    delay(5000);
    Serial.println(digitalRead(servo1));
    digitalWrite(servo1, LOW);
    delay(5000);
    Serial.println(digitalRead(servo1));
}

Expected: I expect the code to run the servo motor at full speed for 5 seconds.

Actual: The motor runs at the first impulse and pauses for 10 seconds.

How can I improve the code to do what I expect?

4
  • what are your hardware connections and why are you reading an output pin?
    – Vaibhav
    Commented May 31, 2019 at 18:07
  • My hardware connections are the three wires from the servo directly to the Arduino. I wanted to see what signals were sent.
    – Ate8
    Commented May 31, 2019 at 18:21
  • 1
    Why do you think you cannot use the Servo library? With continuous rotation servos it generally works where 0 spins one way 180 spins the other and something close to 90 stops it.
    – Delta_G
    Commented Jul 2, 2019 at 20:48
  • 1
    Also, to control that servo you don't just set the pin HIGH. You have to send a specific pulse length that tells the servo how fast and what direction to rotate. It is the same sort of signal that tells a regular servo what angle to go to. That's why the Servo library is normally used to control these.
    – Delta_G
    Commented Jul 2, 2019 at 20:50

2 Answers 2

1

Apply some impulse which you need your servo to work on

0

Look at how servo motors are typically controlled. As far as I know there are no servos that can just be "enabled to rotate" like you seem to expect. Usually a servo's position is set by applying a pulse of a specific length. This means you can apply a PWM signal with proper frequency. Then the duty cycle should be proportional to the servos position. From my experience timing is quite critical when controlling a servo, so going the way you do (timing a gpio's output state with delay()s) will not work well. You have to use the atmega's hardware modules (which already is the case when using the arduinos digital write) to achieve adequate timing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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