Go Down

Topic: analogWrite makes my servo turn 0 degrees or 180 and nothing inbetween, help? (Read 2174 times) previous topic - next topic

Addez

Hi,

I'm trying to learn PWM with servos, I got the fading led to work. But I need to manage my servo HXT900 through PWM because I want to use ATtiny85 as a controller.

At the moment I'm using Arduino Duemilanove.
I can't get the servo to work and I think it's because arduinos frequency doesn't match the 50Hz servo..
Eitherway heres my full code:
Code: [Select]
const int pin = 3;

void setup() {
}

void loop() {
  analogWrite(pin, 1);
}


This will rotate the servo as much clockwise as possible, it will even overheat if I leave it on.
If I instead set analogWrite(pin,0);
Then I will get no reaction from the servo.

Idk where to go from here so help is greatly appriciated!


The servo datasheet is here:
http://sainquake.com/wp-content/uploads/2013/03/Hextronik_HXT900_datasheet.pdf


MarkT

The Servo library is the one to use unless you absolutely have to use timer1 for
other purposes (timer1 controls PWM on pins 9 and 10 on the Uno).  The Servo
library can drive upto 12 servos on the Uno using any pins.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Addez


bombasticbob

You might try writing a value of 128 instead of 1.  This should center your servo.  The analog PWM output has a range of values of 0 through 255 .  Maybe your servo is getting 'stuck' near the edges, and the motor is trying hard to go past the stop.  It's probalby best NOT to set values close to the ends, maybe start at 16 and go up to 240, at least while you test it.

In effect, 255 should be one exreme of rotation, and 0 the other.  0 may not work properly, nor may 255 since they don't modulate at all (they stay at 'high' or 'low' respectively).  Servos typically want a PWM signal modulated at the proper clock rate, which is what the Arduino (fortunately) uses on most of its pins.  But not all.  I think pins 5 and 6 are the ones that use a different clock for the PWM.  In any case, you SHOULD be able to drive most servos directly from pin 3.  Just try and avoid the edges, especially if it's burning your servo up.

zoomkat

Quote
This will rotate the servo as much clockwise as possible, it will even overheat if I leave it on.


Malformed control signals cause servos to do that. You may eventually strip the gears.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

PeterH


You might try writing a value of 128 instead of 1.  This should center your servo.


The analogWrite pwm output just produces a square wave with a duty cycle that can be controlled between 0% and 100%. This is not the sort of signal that a servo needs, so trying to control a servo using analogWrite is basically not going to work. You need to produce a pulse in the range of around 1ms to 2ms repeating at about every 20ms. This is what the servo library produces. The servo library doesn't say whether it works on an ATtiny85 so I would guess it probably does unless proved otherwise, but if it doesn't I suspect it will still be possible to generate a servo control signal on that platform and if it's possible then it's very likely that somebody has already created a library to do it. Google would be the way to find that.

Go Up