SerialException: write failed: [Errno 5] Input/output error

Hello,
I am controlling a standard servo motor using python 2.7(pySerial) and arduino mega 2560(IDE 1.0.5). Until yesterday, my code was working perfectly well, without any errors. And then all of a sudden, python started giving the error “SerialException: write failed: [Errno 5] Input/output error”.
So, I changed my arduino code to just print “Hello” when a particular key on keyboard is pressed. That works perfectly well. But as soon as arduino tries to write to the servo when a particular key on keyboard is pressed, it gives me that error. The code is as follows:

Arduino code:

#include <Servo.h>
Servo s;

void setup() {
  Serial.begin(9600);
  s.attach(2);
}

void loop () {
  if (Serial.read()=='s') {
    s.write(5);
  } 
}

Python code:

import serial
port="/dev/ttyACM2"
ser=serial.Serial(port, 9600)

def key(event):
    if event.keysym=='s':
        ser.write('s')

root=tk.Tk()
root.bind_all('<Key>', key)
root.mainloop()

Also, once I get this error, arduino is not connected to the same port anymore. As in, if initially it was connected to ACM2, after the error it gets connected to ACM3.

Any help is appreciated. Thank you in advance.

How is the servo powered?

[quote author=Coding Badly link=topic=214691.msg1571340#msg1571340 date=1391238201]

How is the servo powered? [/quote]

From the arduino, 5V supply.

If the servo is disconnected (power and signal), does the problem occur?

[quote author=Coding Badly link=topic=214691.msg1571351#msg1571351 date=1391238895]

If the servo is disconnected (power and signal), does the problem occur?

[/quote]

No. So, the problem is with the servo?

Thanks.

saurabh_58: No. So, the problem is with the servo?

Possibly. It is much more likely that the servo simple needs to have a separate power supply.

You are welcome.