Let's Make Robots!

Servo coding (solved)

It is working now, thanks to everyone who helped me out!!!

 

 

I am trying to make my servo work,but if I start my arduino, with code and servo attached, it just goes to one position and stays there twitching. Can someone help me?

second try

I changed the delay to two seconds, but the servo only goes to the first 100 degree and does not move after that unless if I turn the servo. Could it be the servo? I use:http://www.dealextreme.com/p/sg90-mini-servo-with-gears-and-parts-2kg-torque-35764

Code:

 

#include <Servo.h> 

 

Servo myservo;  // create servo object to control a servo 

 

void setup() 

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

 

void loop() 

 myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(2000);  

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(2000);                        // waits for the servo to get there 

   myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(2000);    

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(2000);                        // waits for the servo to get there 

 

 

First try

I used a part of the knob controlled servo example.

Code:

 

 

#include <Servo.h> 

 

Servo myservo;  // create servo object to control a servo 

 

 

void setup() 

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

 

void loop() 

 myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(20);  

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(20);                        // waits for the servo to get there 

   myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(20);    

 

AttachmentSize
arduino_servo figure 119.97 KB
figure 2_a_or_b28.92 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

i am thinking of buying those servos from dealx. are they good? i want to make a quadruped of those motors.

I bought these to learn how to controll servos, because they are cheap, but they are not really strong.

For things like stearing a rc car they are fine, but for a robot I would invest in stronger onces.

Every ground should be connected to every ground, everywhere.

thanks a lot 

you were wright, it was the power source. 

Guido

Usually the arduino should be able to power the servo by itself from usb.

that is something the arduino does not in my case

I read a bit further up that you had switched to an external power supply going to your servo. Did you tie the ground of this external supply to the ground of your brain? You should do this.

do you mean: to connect the negative servo leed to A or B on figure 2

or both

the external power supply black(ground, - )  needs to be connected to the arduino ground(GND)

Try this steps:

1. Make sure the servo is plugged in correctly. The orange/white should be connected to arduino pin 9, the red to 5v, and the white/brown to G. If the servo doesn't move at all, look into powering it externally, but the board should be able to handle a single servo without a problem.

2. Connect a button or temporary switch. If you don't have a button, you can just use two wires, one to the arduino and the other to ground and touch them together for a second. If you don't have any sort of button, try Serial.

3. Try the following code. This code will make the servo go to the center at first. Then every time you push the button, it should swing to either side.

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

boolean ServLeft=false;  //this variable tells the servo which direction to go. not sure if the name is accurate.

void setup() 

myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

pinMode(12,INPUT);  //YOU NEED TO ATTACH A BUTTON OR SWITCH BETWEEN THIS PIN AND GROUND

digitalWrite(12,HIGH); //this activates the atmega's internal pullup resistor

myservo.write(90);

delay(2000);

void loop() 

{

if (digitalRead(12)==LOW){    //this button tells the arduino to change servo direction

  (ServLeft)=(!ServLeft); //toggles which direction the servo will

 if (ServLeft==true){

   myservo.write(0);          // sets the servo position according to the scaled value 

  delay(2000);  

 }

  if (ServLeft==false){

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(2000);                        // waits for the servo to get there 

  }