Pages: [1]   Go Down
Author Topic: Issues Controlling a Digital Servo  (Read 1390 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello: I'm having no luck trying to control a digital servo, HiTech HSR-5995 that I purchased six years ago. I just need basic code to test with at this point. When using the code below (that I wrote 6 years ago) the servo moves about 30 degrees back and forth while making a very high pitched and disturbing buzz. I would like to move it 180 degrees back and forth. I suspect the pulse width I'm using is flawed.

The servo specs:
Width of Neutral Pulse: 1500uSec
Pulse Variation: +/- 400uSec
Pulse Cycle: 12 - 26mSec
Pulse Wave Voltage: 3.3 - 7.4V

The Code
Code:
int servoPin = 9;            // R/C  Servo connected to digital pin
int myAngle;                 // angle of the servo (roughly in degrees) 0-180
int pulseWidth;              // function variable

 
void servoPulse(int servoPin, int myAngle) {
  pulseWidth = (myAngle * 2.2) + 1500;  // converts angle to microseconds
  digitalWrite(servoPin, HIGH);       // set servo high
  delayMicroseconds(pulseWidth);      // wait a very small amount
  digitalWrite(servoPin, LOW);        // set servo low
  delay(2.5);                          // refresh cycle of typical servos (20 ms)
}

void setup() {
  pinMode(servoPin, OUTPUT);          // set servoPin pin as output
}

void loop() {
  // cycle through every angle (rotate the servo 180 slowly)
  for (myAngle=0; myAngle<=180; myAngle++) { 
    servoPulse(servoPin, myAngle);
  }
  delay(0);
}

I've also tried the Arduino sample servo sweep code. In this case the servo moves to 0, then rotates about 120 degrees, then stops and makes the high pitched buzz with no further movement.

Code:
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
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(50);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }
}

Any recommendations are greatly appreciated.
Logged

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

Some digital servos have a high pitched whine. Your servo may need to be reprogrammed to rotate the 180 deg. Below is some simple servo code you can test with.

Code:
// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 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.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray);
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  }
}
Logged

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've also tried the Arduino sample servo sweep code. In this case the servo moves to 0, then rotates about 120 degrees, then stops and makes the high pitched buzz with no further movement.

It's probably hitting a mechanical stop. Not all servo are designed to move 180 degrees, 120 degrees is a common range for R/C servos. Send a 1000usec and then a 2000 usec pulse to determine the designed normal travel range. Most servos will have a little 'over-travel' above and below 1000 and 2000 respectively, but how much over-travel varies from servo to servo. Don't force it to a position where it's buzzing continuously, it's not showing you love when it does that.

Lefty
Logged

Pages: [1]   Go Up
Jump to: