Inaccurate IR Sensor Reading

Always, every time, I'd never share motor/servo power with logic power - logic requires stable glitch-free power. And make sure the power source can provide enough current for the worst-case instantaneous current draw of the servo, which will be measured in amps - and alas is often not documented.

@Wawa @koraks I've followed your suggestions and gotten a 5V 2A power adapter and connected the power pins to it, while keeping the signal pin attached to the Digital pin in Arduino. However, the servo is now not responding to any commands and only jerks when I nudge the female plug. I've checked the voltage across it and it is a stable 5V. Any suggestions?

Sounds like a bad contact in the plug then.

It doesn't seem so. Since it only does that when the signal pin is not connected. When the signal pin is connected to the Arduino, it doesn't move at all. I'm starting to worry that the problem is with one of the components, either the Arduino or the servo. How can I verify where the issue is?

You did of course connect servo ground both to supply(-) and Arduino ground.

Yes I have. I actually encountered more confusing news. First, I went to the store to check on my servo and the guy said that it is malfunctioned and so I got another one which he tested in front of me and worked. Then, I tested it at home and it gave an initial rotation then stopped. I decided to check the voltage reading across the power adapter terminals and it was decreasing, even after removing the servo. Apparently, the socket is malfunctioned as well. I switched to another working socket but the servo did not respond. I tried another servo I had for another project and it surprisingly worked but in an erratic way and did not follow the code.

First, I once read that supplying voltage lower than the rated voltage to the servo may burn it. Is this true? If so, is it possible that the malfunctioned socket has burned both servos?

Second, as I said earlier, the third servo was working (a huge step!), but was very erratic in its rotation. What may be causing this?

That is certainly possible. Manufacturers state device power supply tolerances for truly excellent reasons.

Almost all of the "servo malfunctions" reported on this forum are due to inadequate servo power supplies, or failure to connect the grounds.

Fortunately, the issue has been solved. Apparently, there was an issue with the servo motor I was using. After replacing it with another one, following your suggestions to buy a separate 5V 2A adapter, and finally connecting its grounds both to Arduino and the adapter terminals, it worked fine. Thank you all for your help!