Go Down

Topic: Servo Issues (Read 874 times) previous topic - next topic


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:
Video http://youtu.be/A8jij6bPTLg

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!


Feb 11, 2013, 01:33 am Last Edit: Feb 11, 2013, 02:00 am by Krodal Reason: 1
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: http://arduino.cc/en/Reference/pinMode

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);


Have you tried the Servo library examples?

File -> Examples -> Servo



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.



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!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131