Go Down

Topic: Servo help (Read 394 times) previous topic - next topic

Tesla22

Hello.  I am trying to drive a Hitec standard HS 311 servo with an Arduino Mega 2560.  I have uploaded many different sketches related to servo control, but there is absolutely no result.  I have tried this code with 3 other servo of the same type. 

I have tried the same procedure with 3 of the exact same servo, with the same result.  I have connected the Mega to a wall outlet while it was connected to the USB port on my laptop.  The wall outlet ad/dc adapter provides a little under 9 volts to the Arudino. 

I have connected the servo directly to the 5V, GND pin and the PWM port 8 on the Arduino.  No result.  I then used a breadboard to run a 6 volt battery pack to the servo, no result. 

I have tried the following servo sketches with no results: all servo examples that are included with the latest version, the software servo sketch, servo sketches from the Arduino Companion phone app, and all servo code in the Arduino Cookbook.  Again, no results.

Please help if you can.
Thanks. 

zoomkat

Typical servo wiring with external power supply for the servo. Note the servo power supply and arduino grounds need to be connected. Servo test code bottom.



Code: [Select]

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

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

Go Up