0

Arduino newbie here, so this question might sound too simple. I am trying to run a parallax continuous rotation servo using Arduino Uno. From the reference, I learned that the servo needs to be "attached" to one of digital pins with PWM capability, and the servo's speed can be controlled by write(0~180, 90=stop).

But I am having a hard time understanding the role of the PWM digital pin. If the servo's speed and direction of rotation is determined by write(), what is the role of attach(pin#)?

Thanks in advance!

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Sep 2, 2017 at 21:00

3 Answers 3

1

attach tells the servo library which pin it should be writing to, this is stored somewhere in ram and write will look it up and communicate with the correct pin.

It's possible that you are using the other pwm capable pins for other purposes and suddenly having that library interfere with that is not a good thing.

answered Sep 2, 2017 at 21:05
0

If you dont use the Servo library and instead connect the sertvo signal wire to a pin then define the pin as output and set the pin low. Then set the pin high for a set number of microseconds the servo will move to the appropriate position. The pin is then set to low it should stay at low for 20milliseconds ( servos are happy at 50Hz i.e one pulse every 20ms.

 #define servopin 9
 t=1000;
 int step=10;
 Setup() {
 pinmode(servopin,OUTPUT);
 digitalWrite(servopin,LOW);
 }
 loop() { 
 while (t<2001) {
 digitalWrite(servopin,HIGH); // with the delay sends a timed pulse
 delayMicroseconds(t);
 digitalWrite(servopin,LOW);
 t+=step;
 delay(19); // upto next next pulse
 }
 while (t> 999) {
 digitalWrite(servopin,HIGH);
 delayMicroseconds(t);
 t-=step; 
 delay(19);
 }
 }

This will cause the the servo to sweep 90deg back and worth continuously. Changing step will alter the time it takes, reduce it to 1 and it will take much longer. Exactly what Attach does is unclear but it registers a variable and links it to a timer and to the pin used. The above code uses the built in Millis and Micros timers. The microsecond would be difficult to make unblocking with out affecting the timing. The off delay can be made unblocking so for example you could make two servos to work seemingly together.

Don

answered Oct 12, 2024 at 22:09
-1

First of all, you don't need to connect it to one of the PWM pins. Any digital output pin will work just fine.

Judging by the Arduino reference, attaching the servo to a pin is kind of like to declare a variable. By using myservo.attach(pin) you also give the servo a name (myservo in this case), and you can also add maximum and minimum values to the servo's rotation by adding two extra values to the end: myservo.attach(pin, min, max)

When you then use the myservo.write() function, the Arduino will "remember" what pin myservo is connected to, as well as the maximum/minimum settings for it.

answered Sep 2, 2017 at 21:14
2
  • Servos need the pwm on the control line, they compare the duty cycle value to the internal potentiometer and apply torque to correct any discrepancy Commented Sep 2, 2017 at 22:05
  • 1
    @ratchetfreak yes, but what I meant is that you don't have to use one of the pins with PWM labeled onto them. On the Arduino Uno, pin 3, 5, 6, 9, 10 and 11 are PWM pins, but you don't have to use one of these. Commented Sep 3, 2017 at 6:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.