Servo Motor Twitching

I built a machine to turn on and off my bedroom lights with a servo motor. The controls I have setup to turn this servo are an ultrasonic sensor (for up close) and an IR receiver (for far away). I originally had it set up as an IR receiver and 2 push buttons. That worked great except I wanted it to be easier to use. The problem with this new set up is when I use the IR remote to turn my lights on or off, it works correctly initially, but after about 30 seconds, the servo motor starts twitching rapidly, turning my lights on and off. If somebody could help me fix this problem, that would be greatly appreciated. Here is my code:

Light_Switch_Ultrasonic.ino (1.98 KB)

I forgot to mention. It only twitches when the arduino is hooked up to my 6 AA external battery holder with a barrel jack. When it is plugged into my computer through micro usb, it works fine. I need to power it with the battery holder.

I know it's a simple, but make sure your barrel jack's connections are good. My servos always twitch when I initially apply power to them and if the wiring is lose causing it to constantly short itself and then go back to normal could cause the twitching to occur.

It's not 100% clear how the servo is powered: is it from the Arduino 5V or direct from the battery with a common ground?

The latter is always better.

kenwood120s: It's not 100% clear how the servo is powered: is it from the Arduino 5V or direct from the battery with a common ground?

The latter is always better.

Except that 9V (6 x AA) is too much for many/most small servos.

But it is quite likely that the problem is lack of power. rregan, what type of servo are you using and what sort of batteries are they? Some consumer AAs can only deliver very low currents.

Steve

slipstick: Except that 9V (6 x AA) is too much for many/most small servos.

Well spotted, I missed that.

So in that case the implication is the servo is powered from the Arduino.

slipstick: Except that 9V (6 x AA) is too much for many/most small servos.

But it is quite likely that the problem is lack of power. rregan, what type of servo are you using and what sort of batteries are they? Some consumer AAs can only deliver very low currents.

Steve

I have the 6 AAs wired to my arduino, and i have a separate 4 AAs hooked up to my Futaba standard servo. it requires 4.5 to 6V. They share a common ground. I am using duracell AAs.