Servo motor affecting MIC Analog read data

Hello! I am working on a project where audio levels activate the servo motor. I am using the sparkfun MEMS mic breakoutboard ( and a servo similar to the Hitec HS-55. When reading the data without the servo motor I get the range I need but when the servo is wired in the data just continually rises and then maxes out. I am very lost at what the problem is. If anybody could help me that would be great.

#include <Servo.h>
Servo myservo;

int pins = 0; // pin counter
int pos = 1;

const int middleValue = 512; //the middle of the range of analog values
const int numberOfSamples = 128; //how many readings will be taken each time

int sample; //the value read from microphone each time
long signal; //the reading once you have removed DC offset
long averageReading; //the average of that loop of readings

long runningAverage=0; //the running average of calculated values
const int averagedOver= 22; //how quickly new values affect running average
//bigger numbers mean slower

const int threshold=512; //at what level the light turns on

void setup() {



void loop() {

  // Thank you Elio for the help with audio sampling code below
  long sumOfSquares = 0;
  for (int i=0; i<32; i++) { //take many readings and average them
    sample = analogRead(0); //take a reading
    signal = (sample - middleValue); //work out its offset from the center
    signal *= signal; //square it to make all values positive
    sumOfSquares += signal; //add to the total

averageReading = sumOfSquares/numberOfSamples; //calculate running average

int range;
range = map(runningAverage, 0, 6500, 0, 45);
Serial.print("Running: \t");
Serial.print("Servo: \t");

 for(pos = 0; pos < range; pos = pos + 10)  // 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(15);                       // waits 15ms for the servo to reach the position 
      for(pos = range; pos>=1; pos-=10)     // goes from 180 degrees to 0 degrees 
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);                       // waits 15ms for the servo to reach the position 



ex of data

Running: 69
Servo: 0
Running: 65
Servo: 0
Running: 62
Servo: 0
Running: 59
Servo: 0
Running: 56
Servo: 0
Running: 53
Servo: 0
Running: 50
Servo: 0
Running: 48
Servo: 0
Running: 2647
Servo: 18
Running: 5394
Servo: 37
Running: 8102
Servo: 56
Running: 10120
Servo: 70
Running: 12314
Servo: 85
Running: 13860
Servo: 95
Running: 13794
Servo: 95
Running: 14555
Servo: 100
Running: 14515
Servo: 100
Running: 16124
Servo: 111
Running: 18010
Servo: 124
Running: 18762
Servo: 129

Would appreaciate any help! thanks, Levi

is it similar to this:,85559.0.html ?

r there long unshielded cables? multiple power supplies?

no just using jumper wires. and no I am not using a second power source, but When i do use a secondary power source for the servo (4 AAA batteries), it gives me the right range but the servo makes a quiet buzzing noise and no movement.

Hi, I would suggest -

1) always powering the servo from a separate power supply

2) Make sure that the ground is common between the Arduino and the separate power supply

3) Use a current limiting resistor on the signal wire between the Arduino pin and the servo input

4) If this doesn't help, you could replace the servo with something else just to make sure the problem is not somewhere else in your circuit or code, a 1K resistor connected between the signal and ground would do. Only try this if you are not using the servo state as an input, if the servo is just an output, your code will not notice that it is connected to a resistor instead.

I haven't looked at your code, but 1-4 are a good starting point before getting into the code.

Duane B