Arduino Forum

Using Arduino => General Electronics => Topic started by: Micahchetrit on Feb 11, 2013, 01:14 am

Title: Servo Issues
Post by: Micahchetrit on Feb 11, 2013, 01:14 am
I just bought an arduino, and am doing my first project.  It is very simple, it takes a digital input, and tells a servo to go to one of the extremes based on the input. The sensor is a npn photoswitch (allan bradly, I can give you way more details if necessary).  I used the internal pullup resistor to read it.  It requires 12v, and the servo I am using is a Hitec HS-311(rated for 6v).  I used 8 AAs to power the Arduino Uno R3 and photoswitch, and also fed a dc-to-dc downstepper to go from 12v to 6v. 
The problem:

I used a multimeter to make sure the servo is receiving 6v
I tested a known good servo to eleminate the servo as the problem. similar issues
I tried different pins on the arduino and breadboard. nothing changed.

The code:
int ServoPin = 10;
int SensorPin = 12;
int SensorVal;

void setup()
    pinMode (SensorPin,INPUT);
    pinMode (ServoPin,OUTPUT);
    SensorVal = LOW;

void loop()
    digitalWrite(SensorPin, HIGH);          //pullup resistor
    SensorVal = digitalRead(SensorPin);     //gets sensor
      analogWrite(ServoPin, 240);           //255 is max, 240 in case it wasn't reading the pulses at 255
      analogWrite(ServoPin,2);              //0 is min, 2 is same reason as mentioned above

I can get you details if you need more, unsure of what else you'd want.
Thanks for the help!
Title: Re: Servo Issues
Post by: Krodal on Feb 11, 2013, 01:33 am
The servo is a servo. And a servo requires a pulsed signal.
But don't worry, a library takes care of the pulses.
You only need three functions:
Declare the class before the setup() function with: Servo myServo;
Set the used pin in the setup() function with: myServo.attach( ServoPin);
Set the rotation angle in the loop() function with: myServo.write(50);

The servo should have some time after the myServo.write().
Add a delay(500); at the end of the loop() function.

Writing to an input for the pull-up resistor is no longer needed.
But you need the newest Arduino 1.0.3
You can specify the pull-up resistor with the pinMode() function:

If you paste your sketch, you can use the code tags. It is the button with a '#' above the input text field.
It looks like this:
Code: [Select]

pinMode(SensorPin, INPUT_PULLUP);
Title: Re: Servo Issues
Post by: billroy on Feb 11, 2013, 01:39 am
Have you tried the Servo library examples?

File -> Examples -> Servo

Title: Re: Servo Issues
Post by: retrolefty on Feb 11, 2013, 01:46 am
Yes, a servo must use the servo library to operate and servo.write() commands to position the servo.

You can't use analogWrite() commands for a servo as analogWrite() PWM commands use a 500Hz switching speed and servos expect a 40Hz switching speed.


Title: Re: Servo Issues
Post by: Micahchetrit on Feb 11, 2013, 01:54 am
Thanks for all the help guys! Man you are fast.
I didn't realize there was a servo library, or that it needed 40hz. I simply guessed that it used the pwm signal.
Problem Solved!