I'm new to Arduino, but I really would like to figure out how to use use a servo with the board. I have an example code that I've been running, and when I try it, the servo seems to tick like a clock. It moves every second in a little jerking move towards a point. When it reaches that point, it freeze for a bit and vibrates then kicks to another point and ticks back again, repeating the same thing over and over. This is not whats supposed to happen according to the code (I'm pretty sure its supposed to move 90 degrees through the full range of motion, then move slowly) Is my chip damaged, or the motor damaged, or am i just not doing something properly? I've attached a video of whats going on. Here's the code I'm using:
#include <Servo.h>
Servo servo1;
void setup()
{
servo1.attach(9);
}
void loop()
{
int position;
servo1.write(90);
delay(1000);
servo1.write(180);
delay(1000);
servo1.write(0);
delay(1000);
for(position = 0; position < 180; position += 2)
{
servo1.write(position);
delay(20);
}
for(position = 180; position >= 0; position -= 1)
{
servo1.write(position);
delay(20);
}
}
click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.
Try this forum tutorial on servos, then look at the code in the IDE example below (file is attached)
After trying these examples go back and look at your code.
/*
Arduino Starter Kit example
Project 5 - Servo Mood Indicator
This sketch is written to accompany Project 5 in the
Arduino Starter Kit
Parts required:
servo motor
10 kilohm potentiometer
2 100 uF electrolytic capacitors
Created 13 September 2012
by Scott Fitzgerald
http://arduino.cc/starterKit
This example code is part of the public domain
*/
// include the servo library
#include <Servo.h>
Servo myServo; // create a servo object
int const potPin = A0; // analog pin used to connect the potentiometer
int potVal; // variable to read the value from the analog pin
int angle; // variable to hold the angle for the servo motor
void setup() {
myServo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); // open a serial connection to your computer
}
void loop() {
potVal = analogRead(potPin); // read the value of the potentiometer
// print out the value to the serial monitor
Serial.print("potVal: ");
Serial.print(potVal);
// scale the numbers from the pot
angle = map(potVal, 0, 1023, 0, 179);
// print out the angle for the servo motor
Serial.print(", angle: ");
Serial.println(angle);
// set the servo position
myServo.write(angle);
// wait for the servo to get there
delay(15);
}
FYI, I just tested the ServoMoodIndicator example on my UNO and the servo tracked the position of the potentiometer. If yours doesn't do the same thing then there is something wrong with your servo or your setup.
I tried powering it from a 6v AA battery pack (4 1.5v batteries) and it just turns all the way to one side and stops. Maybe its something with my 9 pin? or the chip?
I think it would help if you draw a clear diagram of how everything is connected and post a photo of your drawing here.
When you are using the AA battery back have you the ground from the battery connected to the Arduino ground? That is essential.
It shouldn't matter which Arduino pin is used for the servo control signal but it will do no harm to try another one.
Small servos will run from the Arduino 5v supply but it is very difficult to explain to a Newbie how to distinguish between those that will and those that won't. Many problems here are obviously due to servos or motors drawing too much power and causing the Uno to reset. The most productive debugging step is to give the servo or motor its own power supply.
Since it goes to one extreme and then stops, that implies that it thinks it's getting an input at one extreme, probably a constant zero. Have you checked that you're writing to the correct pin? Pin set as output? Square-wave visible on the output pin (if you have no CRO, connect an LED and see if the brightness changes)?
and it just turns all the way to one side and stops.
That often when there is a malformed or bad control signal, or a loss of servo ground. Some servo test code you can try with your servo.
// 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, 500, 2500); //the pin for the servo control, and range if desired
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);
}
Serial.print("Last servo command position: ");
Serial.println(myservo.read());
readString=""; //empty for next input
}
}
Power the servo from 6V+, not 5V, if possible - some are fussy about this, but you get
better performance anyway. Nearly every servo is designed for 6 to 7.2V
Ensure the supply can supply enough current for the peaks, which are typically 1 to 2A
for most small/medium servos. High torque servos may need more. No Arduino board
has a built-in regulator rated high enough to be reliable. 90% of issues with servos
reported here are due to inadequate power supply. There is also a small but real risk
of trashing your Arduino if the servo puts inductive spikes back onto the 5V supply - cheap
servos are cheap for a reason, you have been warned. Note that as a servo ages its brushes
can wear out (more sparking), and electrolytic caps dry out (no decoupling).