troubleshooting with a 9g servo

I am trying to control a 9g servo in a project that i am still working on, but whenever I run the code the servo just flicks in between what i assume the 2 positions are or it moves a bit then freezes. I know that it freezes in this certain part of the code because i have leds that will turn off when it runs through the servos.

anyways here is the part of the code where it freezes…



this is the full code…

#include <Servo.h>

Servo servo;

//rgb leds r in d7 d11 and d12
int redPin= 5;
int greenPin =11;
int bluePin =3;

int pos = 0; // variable to store the servo position
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 3;//needs PWM pins

long duration;//the time diffrence in bettween the trigger and echo
int distance;//the distance of the object

void setup() {

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);//letting the pin know it will be used to send signals
pinMode(echoPin, INPUT);//letting it know it will be used to receive signals
Serial.begin(9600);//lets the serial monitior know that we r watching
pinMode(buzzer, OUTPUT);

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin,LOW);//turns off trig pin
delayMicroseconds(2);//short delay

digitalWrite(trigPin,HIGH);//turns on trig pin
digitalWrite(trigPin,LOW);//turns off trig pin
//sends the package of signal for a short time

duration = pulseIn(echoPin,HIGH);//read the echo pin and return the sound wave travel time in microsecond

distance = duration* 0.034/2;//converts the duration into the distance to the object

Serial.print("distance: "); Serial.println(distance);//printing the value of the distance from the sensor to the object

if (distance <20)




void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);

See Read this before posting a programming question and follow the instructions on how to post code

Please post code between code tags

[code] and [/code]


Please provide a wiring diagram (photo (between 100 and 200 kB max) of hand-drawn one is OK). Specifically how you power everything is important.

95% of problems people have with servos are due to inadequate powering of the servo. How are you powering your servo?

MarkT: 95% of problems people have with servos are due to inadequate powering of the servo. How are you powering your servo?

i am powering it using the arduino

creatingaccountjusttoaskaquestio: i am powering it using the arduino

That won't work 99% of the time. I have made that work with a micro servo - but that was just for testing. I would never put that into a project.

creatingaccountjusttoaskaquestio: i am powering it using the arduino

From the look of your code you are also powering quite a few other things through the Arduino including probably a buzzer. Powering servos that way is never a good idea and powering loads of other things as well makes it worse. And how is the Arduino powered? USB or external?

Try 4 x AA batteries for the servo power. + to servo +, - to servo - AND to Arduino GND.


Hi, Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum". OR,148850.0.html. Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? Please include how you are powering your project.

Thanks.. Tom... :)