Servo arm disrupting MIC/AMP sensor circuit

Hello all!
I tried creating my first bit of automation about a week ago. The project is to turn on a lightswitch with a servo arm when a certain sound level is met, all hardware is in place but I’m a newbie with the programming so I’m getting an error I cant understand.
When I use the mic/amp circuit by itself i get a good expected reading in my serial port, it picks up music or claps, from about 0.05 to about 1.1 with a clap. When I try integrate my servo arm in the circuit it goes to the top possible value, 2.37 and stays there for some reason.
My code that I am using is as follows
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
//#include <Servo.h>
//Servo myservo;
//int pos = 0;
void setup()
// myservo.attach(9);
void loop()
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow) {
sample = analogRead(0);

if (sample < 1024) // toss out spurious readings


if (sample > signalMax)


signalMax = sample; // save just the max levels


else if (sample < signalMin)


signalMin = sample; // save just the min levels


peakToPeak = signalMax - signalMin; // max - min = peak-
peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
if (volts > 0.65)
// for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to
180 degrees
{ // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable ‘pos’
// while(true){}

I’ve commented out the servo parts as to get the MIC/AMP sensor working, I am completely out of my depth here, is it a software problem or could it be a hardware problem due to the servo drawing current from the arduino?

Any help is greatly appreciated!

Are you by any chance trying to share 5V rail between Arduino, Mic amp and servo?

Definitely you need a separate supply for the servo - the only common point should be
ground between servo power supply ground and Arduino ground.

Do you have an adequate power supply for the servo?

Do you have any decoupling on that supply?

What is this "mic/amp" circuit precisely, and how have you wired everything up?