Pages: [1]   Go Down
Author Topic: Servo and Analog Question  (Read 627 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 139
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to use analogWrite() to control a servo? Can you use an analog pin instead of a digital PWM pin to control the position of a servo and if so how? I have read the PWM tutorial but am still a confused.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25764
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can, but you shouldn't.
PWM from an analogWrite runs at just short of 500Hz, but the servo expects 50Hz.
Even if you do get it down to 50Hz, eight bit resolution isn't really good enough to give fine enough control.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 139
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Will it damage the servo or Arduino? Do you think it would be fine to run continuos servos off of them if I did not care about the speed. Really all I want them to do is be able to run forward and backward.
Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 158
Posts: 2887
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What you need to control a servo are pulses 1000-2000 usec long which repeat every 20-msec,
and ideally with 1-usec resolution. IE, 1000 usec, 1001 usec, etc. As AWOL indicated, that's
difficult to get using analog.write(). You notice it's only a 5-10% duty-cycle on a PWM signal,
assuming you can get one to go as slow as 50-hz.

BTW, 1000-2000 usec moves the servo +/-45 degrees from center. And in actuality, most servos
can be driven close to +/-90 deg off center with a wider pulsewidth range, approx 750-2250 usec,
but this varies with the specific servo.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not use the Servo library? What you want to do (make a servo move) is exactly what it was designed for.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25764
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The servo will probably consume more current at 500Hz than it would at 50Hz, and it could overheat.
Why do you want to do this?
Why not just use the Servo library?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33362
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It was often said by some members here that PWM will damage a servo because it needs PPM, pulse position modulation.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25764
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In multi-channel R/C, the multiplexed signal carrying all channels between transmitter and receiver is PPM, but by the time it is demultiplexed and reaches the servo or ESC, it really is PWM, but very low duty cycle.
However, the default PWM frequency of the Arduino is far too high for common R/C equipment.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 139
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If I use the servo library and have myservo.attach(9) that is saying it is on digital pin 9. How could I change that to analog pin A1?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Will it damage the servo or Arduino? Do you think it would be fine to run continuos servos off of them if I did not care about the speed. Really all I want them to do is be able to run forward and backward.

With continous rotation servos, the servo probably won't be damaged, it just won't operate as you expect. With a regular servo bad control signals can make the servo rotate until it is against the hard stop, where damage then can occurr. Give it a try and see how it works.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 158
Posts: 2887
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The servo will probably consume more current at 500Hz than it would at 50Hz, and it could overheat.

The internal control loops in standard analog servos will not operate at 500Hz. They will operate to
about 100Hz [10-msec update], but any faster than that and the servos goes bonkers under loaded
conditions because the control loops crash. Digital servos can accept faster control updates, but
I'm not up on the details.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
   The Servo library is very good and allows you to control a large number of servos on more or less any pin.

Here is some background -

http://rcarduino.blogspot.com/2012/01/can-i-control-more-than-x-servos-with.html

There are some aliases in Arduino which allow you to refer to the analog pins in digital functions, I haven't tried this myself, but the following may work for you

myServo.attach(A1); // A1 is the alias for analog pin 1, use An for whichever analog pin you want where n = 0 to 5

Duane B

rcarduino.blogspot.com
Logged


Pages: [1]   Go Up
Jump to: