user66829: I'm using the GP2Y0A21YK Sharp IR Sensor in a ball and beam system where I'm trying to balance the ball in the center of the beam using the IR Sensor as a distance locator and a servo motor to tilt the beam. I'm using a PID Controller for the system. However, something odd happens when I apply the code. When the servo motor is disconnected, I'm getting accurate readings of the location of the ball. However, as soon as I plug in the servo motor, the distance reading is completely off and inaccurate. I've read that powering the Arduino using both the USB port and Plug will make the readings more stable. However, I've tried that and it only improved the readings by a small bit. What can be causing this problem? Since the code and the schematic would take a bit of a space, kindly let me know if they are needed and I'm happy to provide.

user20245: It is likely your power source supplies insufficient current to power both the servo and the Arduino reliably.
It is also possible that the motor in the servo is creating noise on the power rails and inducing unexpected behavior in the Arduino.  But the former is more likely.
Consider using a power source which can supply more current.  For instance, D size cell batteries can supply much more current than a small 9 volt battery.
Also consider using a separate power source for the Arduino and the motor.  Many resort to this option for such projects as you describe here.

66829: <p>I'm using the GP2Y0A21YK Sharp IR Sensor in a ball and beam system where I'm trying to balance the ball in the center of the beam using the IR Sensor as a distance locator and a servo motor to tilt the beam. I'm using a PID Controller for the system. However, something odd happens when I apply the code. When the servo motor is disconnected, I'm getting accurate readings of the location of the ball. However, as soon as I plug in the servo motor, the distance reading is completely off and inaccurate. I've read that powering the Arduino using both the USB port and Plug will make the readings more stable. However, I've tried that and it only improved the readings by a small bit. What can be causing this problem? Since the code and the schematic would take a bit of a space, kindly let me know if they are needed and I'm happy to provide.</p>
 20245: <p>It is likely your power source supplies insufficient current to power both the servo and the Arduino reliably.</p>
<p>It is also possible that the motor in the servo is creating noise on the power rails and inducing unexpected behavior in the Arduino.  But the former is more likely.</p>
<p>Consider using a power source which can supply more current.  For instance, <a href="https://en.wikipedia.org/wiki/D_battery" rel="nofollow noreferrer">D size cell</a> batteries can supply much more current than a <a href="https://en.wikipedia.org/wiki/Nine-volt_battery" rel="nofollow noreferrer">small 9 volt battery</a>.</p>
<p>Also consider using a separate power source for the Arduino and the motor.  Many resort to this option for such projects as you describe here.</p>
