1

I'm controlling the position of a servo with Python and an Arduino. I type in the angle in python and it sends to the Arduino which then moves the servo to that specific angle. Arduino code:

#include <Servo.h>

Servo servo;

void setup() {
  servo.attach(9);
  Serial.begin(9600);
}

void loop() {
  while(Serial.available())
  {
    int pos = Serial.parseInt();
    if(pos >= 0)
    {
      servo.write(pos);  
    }
    else
    {
      continue;
    }
  }
}

python code:

import serial

port = serial.Serial('COM3',9600)

while(port.isOpen()):
    int_data = int(input("Enter servo position: "))
    str_data = str(int_data)
    byte_data = str_data.encode()

    port.write(byte_data)

Everything works fine for like 30 seconds and I can control the servo, but then it suddenly gives me the error: WriteFile failed (PermissionError(13, 'the device does not recognize the command ', None, 22))

LUGG4S
  • 45
  • 5

1 Answers1

0

What I did find is that it might be because of a long serial cable and that the servos are causing a drop in the voltage while turning that causes the Arduino to read/send faulty commands with corrupted data.

Try and add an extra power source like below. Tt doesn't need to be 9v. 5v worked with my servos. Just remember to connect the grounds.

Extra power source

  • Hey, thank you for your answer! I found out my servo was just needing way too much power - crashing the Arduino. Check the data sheet of your servo and make sure it needs less than 5V. – LUGG4S Jun 24 '22 at 21:59