Run Servo with Accelerometer strange problem


I’m new to arduino, but not necessarily to programming so I’m guessing the code is written okay, just the idea might be wrong.
I am now facing a problem with my ADXL335 accelerometer, which is supposed to control a servo.

I wrote code which averages 10 values and maps that average value to the servo. Also puts the servo value out on the serial monitor.

Now the strange problem is:

Serial monitor shows perfectly averaged, non jumpy, values, even when accelerometer is moved quickly. But as soon as I plug the servo to the Arduino Uno and Digital 9 (PWM), the accelerometer values become jumpy on the serial monitor and you can see that in erratic servo movement.

Can anyone help with this issue? I have seen the ADXL335 work nicely on videos, but can’t get it to work.

If you have time on your hand, you could look at my code.

Thank you!


#include <Servo.h>

Servo myservo;

const int xPin = 0;
int val;
int xRead[10];
int i =0;
int total=0;

void setup(){

void loop(){
//set total to zero when restarting loop:


 //loop to get 10 values and average them
  xRead[i] = analogRead(xPin);
  //throw out bad values:
 //if bad value bigger than last value+20
  //if bad value smaller than last value-20
  total= total + xRead[i];
 //calculate average:
 val= map(val,269,404,0,179);
 //limit servo:
} cannot power the servo from arduino, use a separate power source with enough current. Servo needs a lot of power..

Hi and thanks for your quick reply!

I have a BEC now, which connects to servos ground and v-pin with 5V. Signal connected to D-Out 9, like before, but servo doesn't move anymore at all now, not even if I set a fix value for it in the program.

As soon as I connect ground and v-pin to the Uno again, it works like before, very jittery and the values jump.


Can you help me sort this out? Anyone had the same trouble?

Thank you again!


I do not know how the setup looks like, but gnd of arduino and gnd of servo and gnd of the power source for the servo must be connected together - common ground..


Thanks for your answer.

Having a common ground didn't help!

Anyone an idea?


show schematics

Here it is: not power the servo from arduino, use separate power source for the servo instead - that wiring (the servo powered from an external source) needs to be seen so we can help you..

Alright, I didn't know how to show that in fritzing.

1) As I said I have hooked up the servo like shown above. 2) Only difference servo Voltage pins both hooked up to external BEC which outputs 5 Volts and definitely works, cause I use it for other stuff. 3) Additionally I connected the BEC ground to the arduino ground and the servo ground.