Hi, Just getting into robotics. Nothings ever straightforward is it. I am using servo motor sg90. I have used the sweep motor code. This gives you 0-180 degrees movement in steps of 1 degree. Measuring voltages on the control yellow wire you get .154 -.586 at the extremities of the movement.
servo_test.ino (232 Bytes)
Not sure if my code has uploaded properly. Anyway I just use 3 steps for the servo. 60,120 and 180 degrees. I am only getting a 90 degrees movement whereas in the sweep code I get 180 degrees.
Another difference. I am measuring voltages on the yellow line of , 1.11, 2.358 and 3.495. Much higher than in the sweep example. Is there an explanation for this? The servo works well with my code but again I can only get a 90 degrees movement.
Don't attach the code, just copy for the forum and paste in line using code tags. Most helpers won't download the .ino and open it on their machine...
See How to get the best out of this forum for best practices and practical details about code tags, what info to provide etc.
--
how do you measure voltage ? your servo is driven through a PWM signal.
//servo_test.ino
int val;
int pin=9;
void setup() {
pinMode(pin, OUTPUT);
}
void loop() {
val=60;
analogWrite(pin,val);
delay(2000);
val=120;
analogWrite(pin,val);
delay(2000);
val=180;
analogWrite(pin,val);
delay(2000);
}
Are you seriously using this program for servo control? If so, then you have no idea what you're doing. Check out the servo control examples.
Measuring voltage with a multimeter. This will give the average voltage for what is a duty cycle output from the pwm pin.
perhaps I better explain the code a little. in the sweep example you have 180 degrees movement in 1 degree steps. a delay of 15ms is in the code to allow time for the servo to move to the new position. because the delay is so short the servo appears to have a continuous movement over the whole 180 degrees. multiply 15ms by 180 gives the time for the complete movement 2.7seconds. which checks out.
You are probably confused by the 2 second delay. this is so I can see each step which you can't in the 15ms delay example. of course you don't really need it in a real life situation. Come to think of it the 2 second delay may be the problem with the voltage readings.
If I use 15ms with my code I can't see it working properly as the motor will be operating at too high speed. I did say I am new to servos so I probably have no idea what I am doing as you say.
Please let me know where I can find these servo control examples. I don't like using libraries because you can't really understand the coding then.
They're included in the IDE. You've already mentioned one, the sweep example. Let's take a look at it and compare with what you wrote.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
In addition to the comments, there is also a page on this site that explains this code. Note that it includes the servo library which is used to drive servos and then creates a servo object called myservo
and then uses that object with myservo.write
and the degrees they want the servo to move to.
The servo library creates the correct pulse for a servo. It's not just a PWM signal. It's the pulse length that controls the servo position.
Your code on the other hand tries to use analogWrite to control the servo. The example does not do anything like this so I'm not sure why you tried it that way. analogWrite creates a pwm signal, it's basically turning the pin HIGH and LOW really fast to create a square wave with an average voltage you specify. This has absolutely nothing to do with how you control a servo.
No, the program does not do what you described. You've described what you want the program to do, but that's a whole other thing. There is nothing wrong with using libraries even if you are a very good programmer, but if you are a beginner libraries are simply invaluable. You have no idea what signal the servo is expecting to run normally. If you are measuring a PWM signal, let your multimeter have a frequency and duty cycle measurement. That way you'll at least have some idea of what's going on unlike when you measure voltage. As @Delta_G has mentioned, there are a lot of examples of whatnot in the Arduino IDE but you haven't even looked at them.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.