Inaccurate IR Sensor Reading

[Mod edit:
Please note that @zelreedy1 has 2 topics open about the same subject. They are linked in the sense that they are about the same project, but are different questions. The other one can be found here Ball & Beam System Target Landing ]

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.

how do you power the motor?

I'm powering the motor as per the datasheet to the 3.3V port on the Arduino and the IR Sensor to the 5V one. @killzone_kid

???

@killzone_kid My bad. The operating voltage is actually 4.8 to 7.2 V, as per the datasheet here:

However, when I tried powering it from the 5V port on the Arduino, the servo motor jerks as if it is jammed and doesn't move, so I tried the 3.3V one and it operated nicely

it says it needs min 4.8v. Anyway that is a poor excuse for a data sheet. it does not give you current rating but says it is high torque, so all reasons to believe it will be current hungry that arduino cannot provide. I also expect Vcc has to go to a separate power supply that can provide current

I see, so should I separately power the servo? If so, would 3 AAA batteries do the job, or do I need a higher current supply, such as from a D size cell?

i can’t say there is no data in data sheet. try and see. Well as for voltage, 4.5 is less minimum required 4.8, so go figure

The Arduino cannot be used to power motors or servos.

A 4xAA battery pack will work for one or two small servos. Don't forget to connect the grounds.

Powering your servo from the Arduino is a great way to end its life.

@jremington I have attempted that but the servo just operates at 3.3 V. 5V produces a jerky movement as if the servo is jammed, and so does anything higher than that. Additionally, as the servo seems to draw around 200 mA I'm unsure what power source to use for it.

Time to:
Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

200mA will kill your Arduino.

:scream_cat:
What kind of Arduino board are you using? If you do this with a Nano, you're using the CH340G USB-Serial chip to power a motor. That's just insane.

Uhm. No. That's nonsense.

Get yourself a decent 5V power supply, one that can output a few A. Let's say 2A or more.
Then get yourself a DC-DC buck converter that can step down 5V to 3.3V.
Done.

Google tells me that the stall current of an MG995 is 1500mA at 4.8volt.
The supply must be able to provide that current if you want a stable operation of the servo.
Leo..

How can I achieve this? By that I'm more talking about the wiring, since all the ones I found have single, female plugs as this one:

image

Can you guide me further on the wiring?

@Wawa As the issue seems to be more about the current than the voltage, can I use a transistor to amplify the current to the required 2A, while using 4 AAA batteries as the terminals of both the transistor and the servo (The operating voltage is apparently 4.8V to 7.2V)?

No, you need magic to make what you haven't got.

Just try four AA batteries to power the servo, and USB power for the rest.
A mains power supply is of course a better longer lasting option.
There are adapters available to go from DC plug to screw terminal.
Leo..

Cut wire.
Strip back.
Insert into screw terminal or solder to whatever.

Do not cut the wire
get a compatible female plug
solder 2 wires
use it to power your project